Finite state machine what is the error?

Good day, trying to implement a state machine to track the user's position. While trying to start gives an error name 'STATE_DICT' is not defined, tell me how to fix

import config
import telebot

bot = telebot.TeleBot(config.token)


@bot.message_handler(commands=['start'])
def start(message):
 key = telebot.types.ReplyKeyboardMarkup(True,False)
key.row("Button1")
key.row("кнопка2")
 bot.send_message(message.chat.id, "Select menu:", reply_markup=key)


@bot.message_handler(func=lambda a mess: "Button1" == a mess.text, content_types=['text'])
def handle_text(message):
 key = telebot.types.ReplyKeyboardMarkup(True, False)
key.row("кнопка5")
key.row("back")
 chat_id = message.chat.id
 STATE_DICT[chat_id] = '1'
 bot.send_message(message.chat.id, "click", reply_markup=key)

@bot.message_handler(func=lambda a mess: "кнопка2" == a mess.text, content_types=['text'])
def handle_text(message):
 key = telebot.types.ReplyKeyboardMarkup(True, False)
key.row("кнопка7")
key.row("back")
 bot.send_message(message.chat.id, "click", reply_markup=key)


@bot.message_handler(unc=lambda mess: "back" == a mess.text, content_types=['text'])
def start_finite_machine(message):
 if STATE_DICT[chat.id] == '1':
start(message)

if __name__ == '__main__':
 bot.polling(none_stop=True)


Did the example of How to make the correct conclusion when the button is pressed in the telegram. bot in Python?
June 5th 19 at 21:47
1 answer
June 5th 19 at 21:49
Solution
You are obavili STATE_DICT )

In Your case :
import config
import telebot

bot = telebot.TeleBot(config.token)
STATE_DICT = {}


Secondly it is not necessary everywhere handleth. For this you need a state machine - it monitors the status and throws himself to the desired function.

Below is a full example. Should work(not sure as I wrote memory, but the logic is correct)

import config
import telebot
from telebot import types

bot = telebot.TeleBot(config.token)
STATE_DICT = {}


@bot.message_handler(commands=['start'])
def start(message):
 markup = types.ReplyKeyboardMarkup(
one_time_keyboard=False,
resize_keyboard=True
)
markup.add('Button1')
markup.add('Кнопка2')
 question = 'Select'
 chat_id = message.chat.id
 STATE_DICT[chat_id] = 'START'
 bot.send_message(chat_id, question, reply_markup=markup)


def start_work(message):
 chat_id = message.chat.id
 markup = types.ReplyKeyboardMarkup(
one_time_keyboard=False,
resize_keyboard=True
)
 if message.text == 'Button1':
 STATE_DICT[chat_id] = 'VAR1'
markup.add('Кнопка3')
markup.add('Кнопка4')
 bot.send_message(chat_id, 'You selected option 1', reply_markup=markup)
else:
 STATE_DICT[chat_id] = 'VAR2'
markup.add('Кнопка5')
markup.add('Кнопка6')
 bot.send_message(chat_id, 'You chose the answer 2', reply_markup=markup)


def var1(message):
 chat_id = message.chat.id
 if message.text == 'Кнопка3':
 bot.send_message(chat_id, 'End with кнопкой3')
else:
 STATE_DICT[chat_id] = 'VAR2'
 bot.send_message(chat_id, 'End with кнопкой2')


def var2(message):
 chat_id = message.chat.id
 if message.text == 'Кнопка5':
 bot.send_message(chat_id, 'End with кнопкой5')
else:
 STATE_DICT[chat_id] = 'VAR2'
 bot.send_message(chat_id, 'End with кнопкой6')


@bot.message_handler(func=lambda message: True)
def start_finite_machine(message):
 states = {
 'START': start_work,
 'VAR1': var1,
 'VAR2': var2,
}
 chat_id = message.chat.id
states[STATE_DICT[chat_id]](message)


if __name__ == '__main__':
 bot.polling(none_stop=True)
Already sorted, thanks. - nikita.Stracke commented on June 5th 19 at 21:52

Find more questions by tags BotsTelegram