From 489dccaa4e33c433d379639280a7377bb0c108b0 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Mon, 28 Jan 2019 21:35:27 +0530 Subject: [PATCH] Add /send and /sendCode endpoints --- bot/server.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/bot/server.py b/bot/server.py index fc32f3f..012b2bb 100644 --- a/bot/server.py +++ b/bot/server.py @@ -2,6 +2,16 @@ from flask import Flask, request import sqlite3 +import config +import telegram +import re +from emoji import emojize + + +def escape_html(message): + return re.sub("&", "&", + re.sub(">", "&rt;", + re.sub("<", "<", message))) def get(db): @@ -25,8 +35,23 @@ def post(db): return "" +def send(bot, code): + message = request.form["message"] + if code: + message = escape_html(message) + message = "" + message + "" + for i in config.groups: + bot.send_message(chat_id=i, text=emojize(message), + parse_mode="HTML") + else: + for i in config.groups: + bot.send_message(chat_id=i, text=emojize(message)) + return "" + + app = Flask(__name__) db = sqlite3.connect(":memory:", check_same_thread=False) +bot = telegram.Bot(token=config.api_key) # Set up tables cursor = db.cursor() @@ -35,3 +60,5 @@ with open('schema.sql') as fp: app.add_url_rule('/get', 'get', lambda: get(db)) app.add_url_rule('/post', 'post', lambda: post(db), methods=["POST"]) +app.add_url_rule('/send', 'send', lambda: send(bot, False), methods=["POST"]) +app.add_url_rule('/sendCode', 'sc', lambda: send(bot, True), methods=["POST"])