minetest-telegram-bridge/bot/server.py

38 lines
831 B
Python

#!/usr/bin/env python3
from flask import Flask, request
import sqlite3
def get(db):
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
def post(db):
message = request.form['message']
cursor = db.cursor()
query = 'INSERT INTO messages values(?)'
cursor.execute(query, (message,))
db.commit()
return ""
app = Flask(__name__)
db = sqlite3.connect(":memory:", check_same_thread=False)
# 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))
app.add_url_rule('/post', 'post', lambda: post(db), methods=["POST"])