P
P
Popchik2018-03-06 23:51:54
Python
Popchik, 2018-03-06 23:51:54

Why telegram bot crashes after some time of inactivity?

The bot crashes after half a minute of inactivity with an error

Mistake
Traceback (most recent call last):
  File "C:/Users/1/PycharmProjects/untitled3/Bot.py", line 132, in <module>
    main()
  File "C:/Users/1/PycharmProjects/untitled3/Bot.py", line 75, in main
    last_update = analyt_bot.get_last_update()
  File "C:\Users\1\PycharmProjects\untitled3\BotHelper.py", line 28, in get_last_update
    last_update = get_result[len(get_result)]
IndexError: list index out of range
Here is the code
import requests
import datetime

class BotHelper:
    def __init__(self, token):
        self.token = token
        self.api_url = "https://api.telegram.org/bot{}/".format(token)

    def get_updates(self, offset=None, timeout=30):
        method = 'getUpdates'
        params = {'timeout': timeout, 'offset': offset}
        resp = requests.get(self.api_url + method, params)
        result_json = resp.json()['result']
        return result_json

    def send_message(self, chat_id, text):
        params = {'chat_id': chat_id, 'text': text}
        method = 'sendMessage'
        resp = requests.post(self.api_url + method, params)
        return resp

    def get_last_update(self):
        get_result = self.get_updates()

        if len(get_result) > 0:
            last_update = get_result[-1]
        else:
            last_update = get_result[len(get_result)]

        return last_update

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tschin, 2018-03-07
@tschin

Array out of bounds in line 28

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question