Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, Request | |
| import gradio as gr | |
| import requests | |
| import os | |
| API_TOKEN = os.environ.get('HUGGINGFACE_API_KEY') | |
| HF_API_KEY = API_TOKEN | |
| TOKEN = os.environ.get('TELEGRAM_BOT_TOKEN') | |
| BASE_URL = f"https://api.telegram.org/bot{TOKEN}" | |
| CUSTOM_PATH = "/gradio" | |
| app = FastAPI() | |
| async def read_main(): | |
| return {"message": "/webhook is for Telegram webhook and works. But respond message cannot be sent because Huggingface space does not support connection to https://api.telegram.org/."} | |
| async def read_main(): | |
| return {"message": "This is for dummy webhook"} | |
| async def webhook(req: Request): | |
| data = await req.json() | |
| chat_id = data['message']['chat']['id'] | |
| text = data['message']['text'] | |
| msg = f"{BASE_URL}/sendMessage?chat_id={chat_id}&text={text}" | |
| # Sending message to api.telegram.org fails for "No address associated with hostname" | |
| #response = requests.get(msg) | |
| return data | |
| async def webhook_get(): | |
| return {"message": "/webhook for Telegram webhook is running. It accepts POST request from Telegram server."} | |
| def flip_text(x): | |
| return x[::-1] | |
| with gr.Blocks() as demo: | |
| gr.Markdown( | |
| """ | |
| # Flip Text! | |
| Start typing below to see the output. | |
| """ | |
| ) | |
| input = gr.Textbox(placeholder="Flip this text") | |
| output = gr.Textbox() | |
| input.change(fn=flip_text, inputs=input, outputs=output) | |
| gr.mount_gradio_app(app, demo, path=CUSTOM_PATH) | |