-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbot7.py
106 lines (73 loc) · 3.91 KB
/
bot7.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import logging
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, RegexHandler, ConversationHandler
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove
import settings
logging.basicConfig(level=logging.INFO, filename="bot.log", format='%(asctime)s - %(levelname)s - %(message)s')
def reply_to_start_command(bot, update):
reply_keyboard = [['Прислать котика', 'Узнать погоду', 'Заполнить анкету']]
update.message.reply_text(
"Привет! Я бот, который поможет заполнить простую анекту.",
reply_markup=ReplyKeyboardMarkup(reply_keyboard)
)
def start_anketa(bot, update, user_data):
update.message.reply_text("Напишите, как вас зовут.", reply_markup=ReplyKeyboardRemove())
return "name"
def get_name(bot, update, user_data):
user_name = update.message.text
if len(user_name.split(" ")) < 2:
update.message.reply_text("Пожалуйста, напишите имя и фамилию")
return "name"
else:
user_data["name"] = user_name
reply_keyboard = [["1", "2", "3", "4", "5"]]
update.message.reply_text(
"Понравился ли вам курс? Оцените по шкале от 1 до 5",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
)
return "attitude"
def attitude(bot, update, user_data):
user_data["attitude"] = update.message.text
reply_keyboard = [["1", "2", "3", "4", "5"]]
update.message.reply_text(
"Все ли было понтяно? Оцените по шкале от 1 до 5",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
)
return "understanding"
def understanding(bot, update, user_data):
user_data["understanding"] = update.message.text
update.message.reply_text("Оставьте комментарий в свободной форме или пропустите этот шаг, введя /cancel")
return "comment"
def comment(bot, update, user_data):
user_data["comment"] = update.message.text
reply_keyboard = [['Прислать котика', 'Узнать погоду']]
update.message.reply_text("Спасибо за ваш комментарий!", reply_markup=ReplyKeyboardMarkup(reply_keyboard))
return ConversationHandler.END
def skip_comment(bot, update, user_data):
user_data["comment"] = "Пользователь не оставил комментария"
reply_keyboard = [['Прислать котика', 'Узнать погоду']]
update.message.reply_text("Спасибо!", reply_markup=ReplyKeyboardMarkup(reply_keyboard))
return ConversationHandler.END
def dontknow(bot, update, user_data):
update.message.reply_text("Не понимаю")
def false_start(bot, update, user_data):
update.message.reply_text("Нельзя :)")
def start_bot():
my_bot = Updater(settings.TELEGRAM_API_KEY)
dp = my_bot.dispatcher
dp.add_handler(CommandHandler("start", reply_to_start_command))
conv_handler = ConversationHandler(
entry_points=[RegexHandler('^(Заполнить анкету)$', start_anketa, pass_user_data=True)],
states={
"name": [MessageHandler(Filters.text, get_name, pass_user_data=True)],
"attitude": [RegexHandler('^(1|2|3|4|5)$', attitude, pass_user_data=True)],
"understanding": [RegexHandler('^(1|2|3|4|5)$', understanding, pass_user_data=True)],
"comment": [MessageHandler(Filters.text, comment, pass_user_data=True),
CommandHandler('skip', skip_comment, pass_user_data=True)],
},
fallbacks=[MessageHandler(Filters.text, dontknow, pass_user_data=True)]
)
dp.add_handler(conv_handler)
my_bot.start_polling()
my_bot.idle()
if __name__ == "__main__":
start_bot()