Add basic telegram sending to myself.

This commit is contained in:
Joe Tretter
2022-01-11 19:33:18 -06:00
parent 61ac649fb1
commit bb39aab3d0
4 changed files with 78 additions and 9 deletions

40
api1.py
View File

@@ -5,6 +5,7 @@ from flask_apscheduler import APScheduler
from datetime import datetime,timedelta,timezone from datetime import datetime,timedelta,timezone
import sqlite3 import sqlite3
import smtplib import smtplib
from telethon.sync import TelegramClient
# testing: # testing:
# start dummy web server for dev with root: python3 -m smtpd -c DebuggingServer -n localhost:25 # start dummy web server for dev with root: python3 -m smtpd -c DebuggingServer -n localhost:25
@@ -25,6 +26,43 @@ class Config(object):
SCHEDULER_API_ENABLED = True SCHEDULER_API_ENABLED = True
def sendSelfTelegram(text):
print(f"sending telegram message to myself")
# get your api_id, api_hash, token
# from telegram as described above
api_id = '2452309'
api_hash = '4cc6a58946508e59547d15d530a421ae'
# your phone number
phone = '+13616553044'
# creating a telegram session and assigning
# it to a variable client
client = TelegramClient('session', api_id, api_hash)
# connecting and building the session
client.connect()
# in case of script ran first time it will
# ask either to input token or otp sent to
# number or sent or your telegram id
if not client.is_user_authorized():
client.send_code_request(phone)
# signing in the client
client.sign_in(phone, input('Enter the code: '))
try:
schlingelId=5070349790
client.send_message(schlingelId, "test message", parse_mode='html')
except Exception as e:
# there may be many error coming in while like peer
# error, wwrong access_hash, flood_error, etc
print(e);
# disconnecting the telegram session
client.disconnect()
def sendMail(topic,toAddr): def sendMail(topic,toAddr):
print(f"sending mail to {toAddr} for topic {topic}") print(f"sending mail to {toAddr} for topic {topic}")
port = 25 port = 25
@@ -32,9 +70,9 @@ def sendMail(topic,toAddr):
sender_email = "deadswitch@kawomi.com" sender_email = "deadswitch@kawomi.com"
receiver_email = toAddr receiver_email = toAddr
message = f"""Subject: deadSwitch not pushed on topic {topic}\n\nNotification of deadSwitch not pushed on topic {topic}.""" message = f"""Subject: deadSwitch not pushed on topic {topic}\n\nNotification of deadSwitch not pushed on topic {topic}."""
sendSelfTelegram(message)
server.sendmail(sender_email, receiver_email, message) server.sendmail(sender_email, receiver_email, message)
def expiryCheck(): def expiryCheck():
print('.',end='') print('.',end='')
db=sqlite3.connect(DATABASE,detect_types=sqlite3.PARSE_DECLTYPES) db=sqlite3.connect(DATABASE,detect_types=sqlite3.PARSE_DECLTYPES)

Binary file not shown.

View File

@@ -1,4 +1,3 @@
import telebot
from telethon.sync import TelegramClient from telethon.sync import TelegramClient
from telethon.tl.types import InputPeerUser, InputPeerChannel from telethon.tl.types import InputPeerUser, InputPeerChannel
from telethon import TelegramClient, sync, events from telethon import TelegramClient, sync, events
@@ -36,17 +35,14 @@ if not client.is_user_authorized():
try: try:
# receiver user_id and access_hash, use # receiver user_id and access_hash, use
# my user_id and access_hash for reference # my user_id and access_hash for reference
print(client.get_me().stringify()) #print(client.get_me().stringify())
print("A") # send message to self...
#receiver = InputPeerUser('user_id', 'user_hash') receiver = InputPeerUser( str(client.get_me().id), str(client.get_me().access_hash))
receiver = InputPeerUser('1463709677', '4835783730631176464')
print("B ") #print(receiver)
print(receiver)
# sending message using telegram client # sending message using telegram client
#client.send_message(receiver, message, parse_mode='html') #client.send_message(receiver, message, parse_mode='html')
client.send_message(1463709677, "test message", parse_mode='html') client.send_message(1463709677, "test message", parse_mode='html')
print("C")
except Exception as e: except Exception as e:
# there may be many error coming in while like peer # there may be many error coming in while like peer

35
telegramtest2.py Normal file
View File

@@ -0,0 +1,35 @@
from telethon.sync import TelegramClient
# get your api_id, api_hash, token
# from telegram as described above
api_id = '2452309'
api_hash = '4cc6a58946508e59547d15d530a421ae'
# your phone number
phone = '+13616553044'
# creating a telegram session and assigning
# it to a variable client
client = TelegramClient('session', api_id, api_hash)
# connecting and building the session
client.connect()
# in case of script ran first time it will
# ask either to input token or otp sent to
# number or sent or your telegram id
if not client.is_user_authorized():
client.send_code_request(phone)
# signing in the client
client.sign_in(phone, input('Enter the code: '))
try:
schlingelId=5070349790
client.send_message(schlingelId, "test message", parse_mode='html')
except Exception as e:
# there may be many error coming in while like peer
# error, wwrong access_hash, flood_error, etc
print(e);
# disconnecting the telegram session
client.disconnect()