简体   繁体   中英

Python Telegram bot too slow?

I've just started to make a telegram bot in python, and I've noticed one thing with the small piece of code I did: The bot takes too long to respond. Once I send a message to my bot, it takes almost 6-8 seconds to get a reply, which is just too long in a realistic situation. I'm sure it's not my internet being too slow. The bot runs from my laptop. This is my code:

from urllib.request import urlopen
import json
import time

token="xxxxxxx:9999999999999999999999999999999"
site="https://api.telegram.org/bot"+token
offset=110000001

while 1:
    time.sleep(0.1)
    content=(urlopen(site+"/getupdates?offset="+str(offset)).read()).decode('utf-8')
    info=json.loads(content)
    if(info['result']):
        incoming=info['result'][0]['message']['text']
        print(incoming)
        if(incoming=='Hi'):
            msg=(urlopen(site+"/sendmessage?chat_id=184044173&text=HI THERE").read()).decode('utf-8')
        offset=info['result'][0]['update_id']+1

What causes this problem and how can I solve this? I would like to continue using python to make the bot, and I also want to make one from scratch so please don't tell me to use an existing framework or switch languages.

I've heard of 'webhooks' a lot, but never really understood them. If it is relevant to the solution of this problem please explain webhooks and how to use them with python in detail.

Thanks.

I try to explain Webhook for you:

Webhook is the script on your server. You tell telegram, use this script for posting any messages.

After setwebhook calling telegram will send all messages to your script via POST. And you just need to processing messages that income to webhook.

I was write webhook on php, maybe some lines of php code will help you with understending webhooks concept:

$update_json = file_get_contents('php://input');
$update = json_decode($update_json, true);
// get variable;
$chatId = $update["message"]["chat"]["id"];
$userId = $update["message"]["from"]["id"];
$message = $update["message"]["text"];

In first line I read all data (telegram using only post to send messages) next I get message form user, chatId and userId.

next you can use it to send new messages

The problem with your code is there is no timeout period for your getUpdates method. Try setting a timeout of 10 seconds so that the urlopen will wait 10 seconds for a new update before sending another request.

Below is the edited code.

from urllib.request import urlopen
import json
import time

token="xxxxxxx:9999999999999999999999999999999"
site="https://api.telegram.org/bot"+token
offset=110000001

while 1:
    time.sleep(0.1)
    content=(urlopen(site+"/getupdates?timeout=10&offset="+str(offset)).read()).decode('utf-8')
    info=json.loads(content)
    if(info['result']):
        incoming=info['result'][0]['message']['text']
        print(incoming)
        if(incoming=='Hi'):
            msg=(urlopen(site+"/sendmessage?chat_id=184044173&text=HI THERE").read()).decode('utf-8')
        offset=info['result'][0]['update_id']+1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM