0

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your help though could you share some code snippet on how to do that and i'll be sure to look at python-telegram-bot . Thanks a ton for your help again
One example of how to achieve this : from threading import Thread on top And then : @bot.message_handler(commands=['run']) def run(pm): t = Thread(target=compute, args=[pm.chat.id]) t.setDaemon(True) t.start() def compute(chat_id): values = scrape() bot.send_message(chat_id, f"scraped values: {values}") (EDIT) Sorry, comment seem to remove line breaks...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.