-
-
Save Hammer2900/6c4e741292828d9f7c65f381d6558f91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StateMachine: | |
states = ["NO STATE"] | |
user_states = {} | |
def __init__(self,states): | |
if states: | |
self.states.extend(states) | |
else: | |
raise ValueError("StateMachine must have at least one state") | |
def next(self,user_id): | |
if self.user_states.setdefault(user_id,0)<len(self.states): | |
self.user_states[user_id]+=1 | |
else: | |
raise IndexError("StateMachine reached last state") | |
def previous(self,user_id): | |
if self.user_states.setdefault(user_id,0)>0: | |
self.user_states[user_id]-=1 | |
else: | |
raise IndexError("StateMachine is in initial state") | |
def check(self, message): | |
return self.states[self.user_states.setdefault(message.from_user.id,0)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from telebot import TeleBot,apihelper | |
from telebot.types import Message | |
from statemachine import StateMachine | |
TOKEN = 'TOKEN_GOES_HERE' | |
bot = TeleBot(TOKEN, skip_pending=True) | |
UserState = StateMachine(['First Name','Last Name','Age','Gender',"Finish"]) | |
apihelper.proxy = { | |
"http": "socks5://fr11.friproxy.biz:1080", | |
"https": "socks5://fr11.friproxy.biz:1080" | |
} | |
@bot.message_handler(commands=['start']) | |
def start_handler(m: Message): | |
bot.send_message(m.chat.id, 'Welcome. What is your first name?') | |
UserState.next(m.from_user.id) | |
@bot.message_handler(func=lambda m: UserState.check(m)=='First Name') | |
def first_name_handler(m: Message): | |
bot.send_message(m.chat.id, 'What is your last name?') | |
UserState.next(m.from_user.id) | |
@bot.message_handler(func=lambda m: UserState.check(m)=='Last Name') | |
def last_name_handler(m: Message): | |
bot.send_message(m.chat.id, 'What is your age?') | |
UserState.next(m.from_user.id) | |
@bot.message_handler(func=lambda m: UserState.check(m)=='Age') | |
def age_handler(m: Message): | |
bot.send_message(m.chat.id, 'What is your gender?') | |
UserState.next(m.from_user.id) | |
@bot.message_handler(func=lambda m: UserState.check(m)=='Gender') | |
def gender_handler(m: Message): | |
bot.send_message(m.chat.id, 'You are done') | |
UserState.next(m.from_user.id) | |
@bot.message_handler(func=lambda m: UserState.check(m)=="Finish") | |
def finite_handler(m: Message): | |
bot.send_message(m.chat.id, 'You are done dude') | |
if __name__ == '__main__': | |
bot.polling() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment