2019-01-22 11:36:54 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2019-01-22 13:16:00 +01:00
|
|
|
from flask import Flask, request
|
2019-01-22 11:36:54 +01:00
|
|
|
import sqlite3
|
2019-01-28 17:05:27 +01:00
|
|
|
import config
|
|
|
|
import telegram
|
|
|
|
import re
|
|
|
|
from emoji import emojize
|
|
|
|
|
|
|
|
|
|
|
|
def escape_html(message):
|
|
|
|
return re.sub("&", "&",
|
|
|
|
re.sub(">", "&rt;",
|
|
|
|
re.sub("<", "<", message)))
|
2019-01-22 11:36:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get(db):
|
2019-01-22 13:16:00 +01:00
|
|
|
cursor = db.cursor()
|
|
|
|
query = "SELECT * FROM messages ORDER BY ROWID"
|
|
|
|
cursor.execute(query)
|
|
|
|
text = ""
|
|
|
|
for row in cursor:
|
|
|
|
text += row[0] + "\n"
|
|
|
|
cursor.execute("DELETE FROM messages")
|
|
|
|
db.commit()
|
|
|
|
return text
|
2019-01-22 11:36:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
def post(db):
|
2019-01-22 13:16:00 +01:00
|
|
|
message = request.form['message']
|
|
|
|
cursor = db.cursor()
|
|
|
|
query = 'INSERT INTO messages values(?)'
|
|
|
|
cursor.execute(query, (message,))
|
|
|
|
db.commit()
|
|
|
|
return ""
|
2019-01-22 11:36:54 +01:00
|
|
|
|
|
|
|
|
2019-01-28 17:05:27 +01:00
|
|
|
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 ""
|
|
|
|
|
|
|
|
|
2019-01-22 11:36:54 +01:00
|
|
|
app = Flask(__name__)
|
|
|
|
db = sqlite3.connect(":memory:", check_same_thread=False)
|
2019-01-28 17:05:27 +01:00
|
|
|
bot = telegram.Bot(token=config.api_key)
|
2019-01-22 11:36:54 +01:00
|
|
|
|
|
|
|
# Set up tables
|
|
|
|
cursor = db.cursor()
|
|
|
|
with open('schema.sql') as fp:
|
|
|
|
cursor.executescript(fp.read())
|
|
|
|
|
|
|
|
app.add_url_rule('/get', 'get', lambda: get(db))
|
2019-01-22 12:24:57 +01:00
|
|
|
app.add_url_rule('/post', 'post', lambda: post(db), methods=["POST"])
|
2019-01-28 17:05:27 +01:00
|
|
|
app.add_url_rule('/send', 'send', lambda: send(bot, False), methods=["POST"])
|
|
|
|
app.add_url_rule('/sendCode', 'sc', lambda: send(bot, True), methods=["POST"])
|