I'm trying to set up a basic Telegram Bot using webhooks that returns the same message everytime it receives a message. I can't get the JSON file from Telegram that I normally get via getUpdates. I get a 200 Status code when i post a message on my bot so the link between the API, ngrok tunnel and localhost is working.
import telegram
import json
import requests
import flask
import urllib
from flask import Flask
from flask import request
from flask import Response, abort
# For reference:
TOKEN = "XXX"
API_URL= 'https://api.telegram.org/bot{}/'.format(TOKEN)
BOT_URL = 'http://8e0958c3b243.ngrok.io'
app = Flask(__name__)
@app.route('/', methods=['POST'])
def respond():
if request.method =='POST':
print('YES')
**data = requests.get(url=API_URL)**
print(data)
# chat_id = data.message.chat.id
# text = data.message.text.encode('utf-8'.decode())
# print("got text message :", text)
# bot.sendMessage(chat_id=chat_id, text = text)
# data = data.content
# print(data)
# data = json.loads(data)
# print(data)
# chat_id = get_chat_id(data)
# message = 'I got a message'
# data = {'text': text}
# send_message(chat_id=chat_id, text=data)
return Response ('Ok', status=200) # stops from spamming bot
else:
print('An error has occured')
@app.route('/')
def index():
return '.'
def get_chat_id(data):
chat_id = data['message']['chat']['id']
return chat_id
def send_message(chat_id, text):
message_url = BOT_URL + 'sendMessage'
r = requests.post(url = message_url, data=json.dumps(text))
return r
if __name__== '__main__':
app.run(port=5000, debug=True)
Any help is really appreciated! Sorry if this is really basic
question from:
https://stackoverflow.com/questions/65882274/basic-telegram-echobot-with-webhook 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…