-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaloriebmibot.py
295 lines (238 loc) · 9.75 KB
/
caloriebmibot.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
from re import L
from readline import replace_history_item
from tkinter import CURRENT
from telegram import (InlineKeyboardButton,
InlineKeyboardMarkup, MessageAutoDeleteTimerChanged,
Update, callbackquery, replykeyboardmarkup, user)
from telegram.constants import UPDATE_CHANNEL_POST
from telegram.ext import (Updater,
CommandHandler,
Filters,
MessageHandler,
ConversationHandler,
CallbackQueryHandler,
CallbackContext)
#All of this code is written in python
print("Bot testing")
FIRST,SECOND,BMI_MENU,CAL,HEIGHT,WEIGHT,BMI = range(7)
ONE,TWO,THREE,FOUR,FIVE = range(5)
def startcommand(update: Update,_: CallbackContext) -> int:
keyboard = [
[InlineKeyboardButton("Calorie Calculator", callback_data = str(ONE))],
[InlineKeyboardButton("BMI Calculator",callback_data= str(THREE))],
[InlineKeyboardButton("Calories For The Day",callback_data = str(FIVE))]
]
reply_markup = InlineKeyboardMarkup(keyboard )
update.message.reply_text("This Bot Tracks Calories As Well As BMI", reply_markup = reply_markup)
return FIRST
def back(update: Update,_: CallbackContext) -> int:
query = update.callback_query
query.answer()
keyboard = [
[InlineKeyboardButton("Calorie Calculator", callback_data = str(ONE))],
[InlineKeyboardButton("BMI Calculator",callback_data= str(THREE))],
[InlineKeyboardButton("Calories For The Day", callback_data =str(FIVE))]
]
reply_markup = InlineKeyboardMarkup(keyboard)
query.edit_message_text("This Bot Tracks Calories As Well As BMI",reply_markup = reply_markup)
return FIRST
def calorie_cal(update:Update,_: CallbackContext):
query = update.callback_query
query.answer()
keyboard = [
[InlineKeyboardButton(" Breakfast ", callback_data = str(TWO))],
[InlineKeyboardButton(" Lunch ",callback_data= str(THREE))],
[InlineKeyboardButton(" Dinner ",callback_data = str(FOUR))],
[InlineKeyboardButton(" Exit ",callback_data = str(ONE))]
]
reply_markup = InlineKeyboardMarkup(keyboard)
query.edit_message_text("What Are You Having Now?", reply_markup = reply_markup)
return SECOND
#Ask for the user inputs
def breakfast(update: Update,_: CallbackContext):
query = update.callback_query
query.answer()
query.edit_message_text("Please Key In The Amount Of Calories You Had For Breakfast")
return CAL
def lunch(update: Update,_: CallbackContext):
query = update.callback_query
query.answer()
query.edit_message_text("Please Key In The Amount Of Calories You Had For Lunch")
return CAL
def dinner(update: Update,_: CallbackContext):
query = update.callback_query
query.answer()
query.edit_message_text("Please Key In The Amount Of Calories You Had For Dinner")
return CAL
#Portion where the calories are being input
def calorie_input(user_input):
try:
user_int = int(user_input)
cal.append(user_int)
except ValueError:
false_answer = "This Is Not A Number"
return false_answer
answer = "You Have Keyed In " + user_input + " Calories! Click Back To Go Back To The Main Menu"
return answer
#Storing Data in a class
cal = []
# calculates total cals
def cal_calculator(update: Update,_: CallbackContext):
query = update.callback_query
query.answer()
keyboard = [
[InlineKeyboardButton("Back", callback_data = str(ONE))]
]
reply_markup = InlineKeyboardMarkup(keyboard)
query.edit_message_text(f'You Have Consumed {str(sum(cal))} Calories Today!',reply_markup = reply_markup)
return SECOND
#Portion where calories are being received
def calorie_reply(update: Update,_ : CallbackContext):
keyboard = [
[InlineKeyboardButton("Back", callback_data = str(ONE))]
]
reply_markup = InlineKeyboardMarkup(keyboard)
user_input = update.message.text
update.message.reply_text(calorie_input(user_input),reply_markup = reply_markup)
return SECOND
#Area where bmi is being stored
w = [] # weight data
h = [] #height data
current_bmi = []
def bmi_choice(update: Update,_: CallbackContext):
query = update.callback_query
query.answer()
keyboard = [
[InlineKeyboardButton("Current BMI",callback_data = str(ONE))],
[InlineKeyboardButton("New BMI", callback_data = str(TWO))]
]
reply_markup = InlineKeyboardMarkup(keyboard)
query.edit_message_text(f'Welcome to BMI Menu, What Would You Like To Do?',reply_markup = reply_markup)
return BMI_MENU
def recorded_bmi(update: Update,_: CallbackContext):
query = update.callback_query
query.answer()
a = bool(current_bmi)
if a is False:
keyboard = [
[InlineKeyboardButton("Yes",callback_data = str(TWO))],
[InlineKeyboardButton("No, Bring Me To Main Menu",callback_data = str(THREE))]
]
reply_markup = InlineKeyboardMarkup(keyboard)
query.edit_message_text("You Have Not Input Your BMI. Would You Like To Do It?", reply_markup = reply_markup)
return BMI_MENU
else:
keyboard = [
[InlineKeyboardButton("Main Menu", callback_data= str(THREE))],
[InlineKeyboardButton("Update BMI",callback_data = str(TWO))]
]
reply_markup = InlineKeyboardMarkup(keyboard)
query.edit_message_text(f'Your BMI Is Currently {str(current_bmi[-1])} Would You Like To Change Your BMI?',reply_markup= reply_markup)
return BMI_MENU
def body(update:Update,_: CallbackContext):
query = update.callback_query
query.answer()
keyboard = [
[InlineKeyboardButton("Exit",callback_data = str(ONE))]
]
reply_markup = InlineKeyboardMarkup(keyboard)
query.edit_message_text("Please Key In Your Height In Centimetres!", reply_markup = reply_markup)
return HEIGHT
#Height input reply
def height_input(user_input):
try:
user_int = float(user_input)
user_val = user_int/100
h.append(user_val * user_val)
except ValueError:
false_answer = "This Is Not A Number"
return false_answer
answer = "You Have Keyed In " + str(user_val) + "m As Your Height! Please Key In Your Weight In Kilograms (To The Nearest 1 Decimal Place)!"
return answer
#Height reply
def height_reply(update: Update,_ : CallbackContext):
user_input = update.message.text
update.message.reply_text(height_input(user_input))
return WEIGHT
def weight_input(user_input):
try:
user_int = float(user_input)
w.append(user_int)
except ValueError:
false_answer = "This Is Not A Number"
return false_answer
answer = "You Have Keyed In " + str(user_int) + "kg As Your Weight! Press Next For Your BMI"
return answer
def weight_reply(update: Update,_: CallbackContext):
keyboard = [
[InlineKeyboardButton("Next",callback_data = str(ONE))]
]
reply_markup = InlineKeyboardMarkup(keyboard)
user_input = update.message.text
update.message.reply_text(weight_input(user_input),reply_markup =reply_markup)
return BMI
def bmi_calculator(update: Update,_: CallbackContext):
query = update.callback_query
query.answer()
keyboard = [
[InlineKeyboardButton("Exit",callback_data = str(ONE))]
]
reply_markup = InlineKeyboardMarkup(keyboard)
final_bmi = w[-1]/h[-1]
final_bmi = str(round(final_bmi,2))
current_bmi.append(final_bmi)
query.edit_message_text(f'Your BMI Is {final_bmi}!',reply_markup=reply_markup)
return SECOND
def error(update,context):
print(f"Update {update} caused error {context.error}")
def main():
#Insert your API Key Here!
API_KEY = ""
updater = Updater(API_KEY, use_context=True)
main_conv = ConversationHandler(
entry_points= [CommandHandler('start',startcommand)],
states= {
FIRST: [
#Calorie count
CallbackQueryHandler(calorie_cal, pattern = '^' + str(ONE) + '$'
),
#BMI calculator
CallbackQueryHandler(bmi_choice, pattern = '^' + str(THREE) + '$'
),
#Total Calories Consumed
CallbackQueryHandler(cal_calculator,pattern = '^' + str(FIVE) + '$')
],
SECOND: [
CallbackQueryHandler(back, pattern = '^' + str(ONE) + '$'),
CallbackQueryHandler(breakfast, pattern = '^' + str(TWO) + '$'),
CallbackQueryHandler(lunch, pattern = '^' + str(THREE) + '$'),
CallbackQueryHandler(dinner, pattern = '^' + str(FOUR) + '$')
],
#BMI Calculator:
BMI_MENU: [
CallbackQueryHandler(body,pattern = '^' + str(TWO) + '$'),
CallbackQueryHandler(recorded_bmi, pattern = '^' + str(ONE) + '$'),
CallbackQueryHandler(back,pattern = '^' + str(THREE) + '$')
],
CAL: [
MessageHandler(Filters.text,calorie_reply)
],
HEIGHT: [
MessageHandler(Filters.text,height_reply),
CallbackQueryHandler(back,pattern = '^' + str(ONE) + '$')
],
WEIGHT: [
MessageHandler(Filters.text, weight_reply)
],
BMI: [
CallbackQueryHandler(bmi_calculator, pattern = '^' + str(ONE) + '$')
]
},
fallbacks= [CommandHandler("start",startcommand)],
)
dp = updater.dispatcher
dp.add_handler(main_conv)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()