F
F
Foxrey2020-08-20 11:55:18
Python
Foxrey, 2020-08-20 11:55:18

How can I make the chat bot automatically respond and not after I run the file with the code?

import requests
import os
API_link = 'https://api.telegram.org/bot1153670351:AAH7AOmF6Q4bMavkiRs-6Kq3cXXX-Swfg08'

updates = requests.get(API_link+'/getUpdates?offset=-1').json()
print(updates)


message = updates["result"][0]["message"]
chat_id = message["from"]["id"]
text = message["text"]
if text == "hi":
    send_message = requests.get(API_link + f"/sendMessage?chat_id={chat_id}&text=Hi Arsen")
else:
    send_message = requests.get(API_link + f"/sendMessage?chat_id={chat_id}&text=I don`t know this command")

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
soremix, 2020-08-20
@SoreMix

Make receiving updates in a loop? In general, it is better to use libraries, for example, PyTelegramBotAPI

K
kirillinyakin, 2020-08-20
@kirillinyakin

There are libraries for this, for example, pyTelegramBotApi, and you need to write a bot using hardcode if you need maximum performance, and never store the bot's TOKEN in the code, look at what environment variables are

E
ediboba, 2020-08-21
@ediboba

either use long pooling and poll the cart for updates and process, or (better and more reliable) set up a web server (aiohttp, flask, django, tornado, etc.), listen to the port and send all updates to your webhook and process

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question