forked from weiting109/tg_coffeebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers_pho.py
168 lines (108 loc) · 5.54 KB
/
handlers_pho.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"""
Handlers for NDP Better To(gather) chatbot
"""
import logging
from telegram import (ReplyKeyboardMarkup, ReplyKeyboardRemove)
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, ConversationHandler)
"""
the functions defined below are callback functions passed to Handlers. Arguments for
different classes of Handler can be found in docs.
some_fun(update, context) is the standard callback signature for the context based API
"""
logger = logging.getLogger(__name__)
#initialize dict keys
RULES, INTRO, NAME, GENDER, BIO, AGE = range(6)
def start(update, context):
#sends starting message and request password
update.message.reply_text(
"Welcome to Better To(gather)'s party-matching bot! "
"We'll match you with other attendees with similar hobbies or interests. Exciting hor? \n"
"\nYou shall not pass...without a password! Please enter:"
)
#changes state of conv_handler. should make this function a bit more flexible
return RULES
def get_user_details(user):
"""Gets user details from User object"""
user_id = user.id
username = user.full_name
logger.info(f"Got user details: {user_id} {username}")
if not get_user_document(user_id):
create_user_document(user_id, username)
def rules(update, context):
user = update.message.from_user
logger.info("User %s 's password: %s", user.first_name, update.message.text)
#set reply_keyboard
reply_keyboard = [["OK, can"]]
update.message.reply_text(
"OK very nice. Hello! "
"This is an open chat, but we are also a “family-friendly” page, so please keep comments and wall posts clean.\n"
"\nWe want you to tell us what’s on your mind and provide a platform for likeminded individuals to connect through their interests, "
"but please note that content falling into any of the categories below will be removed: \n"
"\n1. We do not allow graphic, obscene, explicit or racial comments or submissions "
"nor do we allow comments that are abusive, hateful or intended to defame anyone or any organization. \n"
"\n2. We do not allow third-party solicitations or advertisements. "
"This includes promotion or endorsement of any financial, commercial or non-governmental agency. "
"Similarly, we do not allow attempts to defame or defraud any financial, commercial or non-governmental agency. \n"
"\n3. We do not allow comments that support or encourage illegal activity. \n"
"\nLet’s make this a safe space for everyone! :-D",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True, resize_keyboard=True)
)
return INTRO
def intro(update, context):
user = update.message.from_user
logger.info("User %s says: %s", user.first_name, update.message.text)
update.message.reply_text('Great! Your name please:')
return NAME
def name(update, context):
user = update.message.from_user
logger.info("Name of %s: %s", user.first_name, update.message.text)
#store user's name in dict (accessed through context.user_data)
context.user_data['name'] = update.message.text
#define next state for conversation
reply_keyboard = [['He/him', 'She/her', 'They/them']]
update.message.reply_text(
'Ah boy, ah girl, others?',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True, resize_keyboard=True))
return GENDER
def gender(update, context):
user = update.message.from_user
logger.info("Gender of %s: %s", user.first_name, update.message.text)
#store user's gender in dict (accessed through context.user_data)
context.user_data['gender'] = update.message.text
#define keyboard for age range
reply_keyboard = [['<20', '20s', '30s','40s','Other']]
update.message.reply_text('You how old?',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True, resize_keyboard=True))
return AGE
def age(update, context):
user = update.message.from_user
logger.info("Age of %s: %s", user.first_name, update.message.text)
#store user's age in dict (accessed through context.user_data)
context.user_data['age'] = update.message.text
update.message.reply_text('Almost done soompah! Tell us something interesting about yourself?')
return BIO
def bio(update, context):
user = update.message.from_user
logger.info("Bio of %s: %s", user.first_name, update.message.text)
#store user's age in dict (accessed through context.user_data)
context.user_data['bio'] = update.message.text
update.message.reply_text('Okay, finding a match for you...')
return ConversationHandler.END
def cancel(update, context):
user = update.message.from_user
logger.info("User %s canceled the conversation.", user.first_name)
update.message.reply_text('Bye!',
reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
def catch_random(update, context):
user = update.message.from_user
logger.info("User %s sent an unrecognized command %s", user.first_name, update.message.text)
update.message.reply_text("I don't understand leh, try another reply")
add_start_cmd = CommandHandler('start', start)
add_rules = MessageHandler(Filters.regex('^password$'), rules)
add_intro = MessageHandler(Filters.text, intro)
add_name = MessageHandler(Filters.text, name)
add_gender = MessageHandler(Filters.regex('^(He/him|She/her|They/them)$'), gender)
add_age = MessageHandler(Filters.text, age)
add_bio = MessageHandler(Filters.text, bio)
add_catch_random = MessageHandler(Filters.all, catch_random)