pythontelegrampy-telegram-bot-apitelebot

Python telebot not working properly while executing scripts from my server


I've created a telegram bot using telebot or pyTelegramBotAPI and created a command in it that let's users execute a python script from my server the script actually scrapes information from lots of websites and then display it back to user usingrequests library. This is my approach to do it

import telebot
import requests

from functions.scrape import scrape

API_KEY = 'MY_KEY'

bot = telebot.TeleBot(API_KEY)

@bot.message_handler(commands=['start', 'help'])

def help(message):
    bot.send_message(message.chat.id, """
        ℹ️ **Command Menu** ℹ️
       /help - Displays the command menu
       /run - scrape information
""")

@bot.message_handler(commands=['run'])

def run(pm):
    values = scrape()
    bot.send_message(pm.chat.id, f"scraped values: {values}")

bot.polling()

The script works fine and does execute the scrape function however some requests from my script automatically fails (out of 300 only 170 requests succeed)

Note - I know the problem lies with telebot itself and not my script cause when i run the same script on my server directly it works flawless without any failed requests i wanted to know what possible reasons might be causing it and how can i fix it or if there any other alternative solutions.


Solution

  • I use to run some telegram bots but I only used the python-telegram-bot API (that I recommend, by the way) and I don't know about the telebot API.

    Considering that it works out of the telebot context, most likely your decorator @bot.message_handler might add some kind of a timeout on the function to be used as a message handler (which makes sense)...

    Something you could try is to launch the compute (scrapping) in another thread, and directly return the message_handler function. Then later on the thread would send your result with bot.send_message, avoiding any timeout