Add /send and /sendCode endpoints
This commit is contained in:
parent
47592d5c44
commit
489dccaa4e
|
@ -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("<", "<", 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"])
|
||||||
|
|
Loading…
Reference in New Issue