File size: 1,523 Bytes
fb839eb
 
7eff936
fb839eb
 
 
 
 
 
 
3dbedbf
fb839eb
 
 
 
 
3dbedbf
2b6bb50
fb839eb
 
3dbedbf
2b6bb50
fb839eb
 
 
 
 
 
 
0a55925
2b6bb50
 
fb839eb
3dbedbf
fb839eb
03a26dc
 
 
 
 
fb839eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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()

@app.get("/")
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/."}

@app.post("/")
async def read_main():
    return {"message": "This is for dummy webhook"}

@app.post("/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

@app.get("/webhook")
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)