Help for threading in telegram bot

by: Soroush76, 7 years ago

Last edited: 7 years ago

Hey guys. I wrote a telegram bot works for one user at a time. I want it to work for hundreds of users by multi threading. But I don't know which part of the code should be in each thread. I put the whole code in each thread and ran it for 100 times but it did nothing and just got close without any job. What should I do?! Which part I should put in each thread?!
<pre class='prettyprint lang-py'>
import requests
import bs4
import os
import telegram
from telegram.ext import *
updater = Updater(token='TOKEN')
dispatcher = updater.dispatcher

def start(bot, update):
    key = ['/start','/movie_list']
    custom_keyboard = [[item] for item in key]
    reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard)
    bot.sendMessage(chat_id=update.message.chat_id, text="bot started! Please give me a movie name or choose ;)", reply_markup=reply_markup)

def back2mainlist(bot, update):
    key = ['/start','/movie_list']
    custom_keyboard = [[item] for item in key]
    reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard)
    bot.sendMessage(chat_id=update.message.chat_id, text="Please give me a movie name or choose ;)", reply_markup=reply_markup)

def movie_list(bot, update):
    f = open("saved_movie_names.txt", "r")
    custom_keyboard = [[name.strip("n")] for name in f.readlines()]
    custom_keyboard.append(["/back"])
    reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard)
    bot.sendMessage(chat_id=update.message.chat_id, text="Please enter a name...", reply_markup=reply_markup)
    f.close()
    updater.start_polling()

def list(bot, update):
    bot.sendChatAction(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
    bot.sendMessage(chat_id=update.message.chat_id, text="/start : start the botn/movie_list : get the list of movies users have been searchedn/stop : stop the bot")
    updater.start_polling()

dispatcher.add_handler(CommandHandler('start', start))
dispatcher.add_handler(CommandHandler('back', back2mainlist))
dispatcher.add_handler(CommandHandler('list', list))
dispatcher.add_handler(MessageHandler(Filters.text, movie_list))

updater.start_polling()
</pre>



You must be logged in to post. Please login or register an account.