def __init__(self, file, poll_size=20): self.file = open(file) self.generator = MarkovChainText(self.file) # Enable logging logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) self.logger = logging.getLogger(__name__) self.message_poll = [] self.poll_size = poll_size
class MarkovBot(): def __init__(self, file, poll_size=20): self.file = open(file) self.generator = MarkovChainText(self.file) # Enable logging logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) self.logger = logging.getLogger(__name__) self.message_poll = [] self.poll_size = poll_size # Define a few command handlers. These usually take the two arguments bot and # update. Error handlers also receive the raised TelegramError object in error. def help(self, bot, update): bot.sendMessage(update.message.chat_id, text='Help! I need somebody') def talk(self, bot, update): generated_text = self.generator.sample_phrases() print(generated_text) bot.sendMessage(update.message.chat_id, text=generated_text) def echo(self, bot, update): if len(self.message_poll) < self.poll_size: self.message_poll.append(update.message.text) else: #self.generator.update(self.message_poll) self.message_poll.clear() def error(self, update, error): self.logger.warn('Update "%s" caused error "%s"' % (update, error)) def run(self): # Create the EventHandler and pass it your bot's token. updater = Updater("194949588:AAETiWnXkKaOipiu2jaKHWmcTWnPfHXLXf0") # Get the dispatcher to register handlers dp = updater.dispatcher # on different commands - answer in Telegram dp.addTelegramCommandHandler("help", self.help) dp.addTelegramCommandHandler("talk", self.talk) # on noncommand i.e message - echo the message on Telegram dp.addTelegramMessageHandler(self.echo) # log all errors dp.addErrorHandler(self.error) # Start the Bot updater.start_polling() # Run the bot until the you presses Ctrl-C or the process receives SIGINT, # SIGTERM or SIGABRT. This should be used most of the time, since # start_polling() is non-blocking and will stop the bot gracefully. updater.idle()