X
X
xefimovgmail2021-12-13 19:15:39
Python
xefimovgmail, 2021-12-13 19:15:39

Why does it give an error when trying to use the conversationhandler?

Hello! I'm trying to add registration to the bot using the conversationhandler, but it gives an error:

SOURCE: [MessageHandler(Filters.text, self.reg_go_two)]
NameError: name 'SOURCE' is not defined


Here is a piece of code:
import telegram

import config

from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, ReplyKeyboardMarkup
from telegram.ext import Updater, CommandHandler, MessageHandler, CallbackQueryHandler, ConversationHandler, CallbackContext, Filters
from Service.Database import DatabaseService
from Service.User import User
from Service.Message import Message
from Service.Keyboard import Keyboard

class TelegramBot:
  updater = None
  database_service =  None

  def __init__(self, token):

    try:
      self.database_service = DatabaseService(config.bd_name, config.bd_user, config.bd_pass, config.bd_host)

    except Exception as e:
      print(e)

    else:
      print("Connect database {}".format(config.bd_name))

    self.updater = Updater(token)
    self.handler()
    self.updater.start_polling()
    self.updater.idle()

  def handler(self):
    ''' event handler '''
    dispatcher = self.updater.dispatcher

    dispatcher.add_handler(CommandHandler('start', self.start))
    dispatcher.add_handler(ConversationHandler(

      entry_points = [
        CallbackQueryHandler(self.reg_go_one, pattern="reg_go_one")
      ],

      states = {
        SOURCE: [MessageHandler(Filters.text, self.reg_go_two)]
      },
      fallbacks=[CommandHandler('cancel', cancel)],
    ))

  def reg_go_one(self, update, context):
    ''' registration of an application, step one '''
    context.bot.delete_message(message_id=update.callback_query.message.message_id, chat_id=update.callback_query.message.chat.id)
    context.bot.send_message(chat_id=update.effective_chat.id, text=Message.registration_text()[1])

    return SOURCE

  def start(self, update, context):
    # check user in database
    if not self.database_service.user_bd(update.effective_chat.id):
      # check username from user
      if update.message.chat.username is None:
        # talk about no username
        context.bot.send_message(chat_id=update.effective_chat.id, text=Message.NoneUsername_text())
      else:
        context.bot.send_message(chat_id=update.effective_chat.id, text=Message.registration_text()[0], reply_markup=InlineKeyboardMarkup(Keyboard.registration_keyboard()[0]))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xefimovgmail, 2021-12-13
@xefimovgmail

Solved the problem by adding SOURCE, LEVEL = range(2), but still interesting how it works

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question