import flask import sys from flask import request, jsonify, g, make_response from flask_apscheduler import APScheduler from datetime import datetime,timedelta,timezone import sqlite3 import smtplib from telethon.sync import TelegramClient # testing: # start dummy web server for dev with root: python3 -m smtpd -c DebuggingServer -n localhost:25 # Request: # curl -X POST "http://localhost:5000/api/v1?topic=abc.def&secs=60&email=j.tretter@gmail.com" DATABASE='deadswitch.sqlite3' class Config(object): JOBS = [ { 'id': 'job1', 'func': 'api1:expiryCheck', 'trigger': 'interval', 'seconds': 10 } ] 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): print(f"sending mail to {toAddr} for topic {topic}") port = 25 server=smtplib.SMTP("localhost", port) sender_email = "deadswitch@kawomi.com" receiver_email = toAddr message = f"""Subject: deadSwitch not pushed on topic {topic}\n\nNotification of deadSwitch not pushed on topic {topic}. https://deadswitch.kawomi.com/api/v1?topic={topic} """ sendSelfTelegram(message) server.sendmail(sender_email, receiver_email, message) def expiryCheck(): print('.',end='') db=sqlite3.connect(DATABASE,detect_types=sqlite3.PARSE_DECLTYPES) cursor = db.cursor() for theRow in cursor.execute("select expiry,topic,email from entries where active=True and isNotified=False and expiry < datetime()"): print(theRow) for theEmailAddr in theRow[2].split(','): sendMail(theRow[1],theEmailAddr) db.execute("update entries set isNotified=True where topic=?",[theRow[1]]) db.commit() db.close() if __name__ == '__main__': app = flask.Flask(__name__) app.config["DEBUG"] = False app.config.from_object(Config()) scheduler = APScheduler() # it is also possible to enable the API directly # scheduler.api_enabled = True scheduler.init_app(app) scheduler.start() def get_db(): db = getattr(g, '_database', None) if db is None: db = g._database = sqlite3.connect(DATABASE,detect_types=sqlite3.PARSE_DECLTYPES) return db @app.teardown_appcontext def close_connection(exception): db = getattr(g, '_database', None) if db is not None: db.commit() db.close() @app.route('/', methods=['GET']) def home(): return """