Add /send and /sendCode endpoints

This commit is contained in:
Ceda EI 2019-01-28 21:35:27 +05:30
parent 47592d5c44
commit 489dccaa4e
1 changed files with 27 additions and 0 deletions

View File

@ -2,6 +2,16 @@
from flask import Flask, request from flask import Flask, request
import sqlite3 import sqlite3
import config
import telegram
import re
from emoji import emojize
def escape_html(message):
return re.sub("&", "&",
re.sub(">", "&rt;",
re.sub("<", "&lt;", message)))
def get(db): def get(db):
@ -25,8 +35,23 @@ def post(db):
return "" return ""
def send(bot, code):
message = request.form["message"]
if code:
message = escape_html(message)
message = "<code>" + message + "</code>"
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__) app = Flask(__name__)
db = sqlite3.connect(":memory:", check_same_thread=False) db = sqlite3.connect(":memory:", check_same_thread=False)
bot = telegram.Bot(token=config.api_key)
# Set up tables # Set up tables
cursor = db.cursor() 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('/get', 'get', lambda: get(db))
app.add_url_rule('/post', 'post', lambda: post(db), methods=["POST"]) 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"])