42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
import os
|
|
import asyncio
|
|
import telegram
|
|
from flask import Flask, request
|
|
import uvicorn
|
|
from asgiref.wsgi import WsgiToAsgi
|
|
|
|
async def main():
|
|
app = Flask(__name__)
|
|
|
|
chat = os.environ["CHAT_ID"]
|
|
bot = telegram.Bot(os.environ["TOKEN"])
|
|
async with bot:
|
|
|
|
@app.route("/")
|
|
def hello_world():
|
|
return "<p>Hello, World!</p>"
|
|
|
|
@app.route("/push", methods = ["POST"])
|
|
async def push():
|
|
title = request.form.get('title', None)
|
|
body = request.form.get('body', 'No body')
|
|
if title is None:
|
|
await bot.send_message(chat, text = body, parse_mode = 'html')
|
|
else:
|
|
await bot.send_message(chat, text = '<b>' + title + '</b>\n\n' + body, parse_mode = 'html')
|
|
return 'ok'
|
|
|
|
webserver = uvicorn.Server(
|
|
config=uvicorn.Config(
|
|
app=WsgiToAsgi(app),
|
|
port=os.environ.get("PORT", 8000),
|
|
use_colors=False,
|
|
host="0.0.0.0",
|
|
)
|
|
)
|
|
|
|
await webserver.serve()
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|