-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
196 lines (140 loc) · 5.24 KB
/
main.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
import os
import telebot
import re
from functions import start_of_week, barchart
from pymongo import MongoClient
#Initiate bot
API_KEY = os.environ['API_KEY']
bot = telebot.TeleBot(API_KEY)
#Connect to MongoDB server
cluster = "mongodb+srv://nadz94:[email protected]/?retryWrites=true&w=majority"
client = MongoClient(cluster)
db = client.Strand.GymTracker
###############
#Start command#
###############
@bot.message_handler(commands=["start"])
def send_start_message(msg):
"""
markup = types.ReplyKeyboardMarkup(row_width=2)
btn1 = types.KeyboardButton("/help")
btn2 = types.KeyboardButton("/mytotal")
btn3 = types.KeyboardButton("/")
btn4 = types.KeyboardButton("/close")
markup.add(btn1, btn2, btn3, btn4)
"""
bot.send_message(
chat_id=msg.chat.id,
text=
"Welcome skinny boy. what would you like to do? (see /help for instructions)")
#,reply_markup=markup)
##############
#Help command#
##############
@bot.message_handler(commands=["help"])
def send_help_message(msg):
bot.reply_to(
msg, """The following commands are available:
/start -> to initiate the bot
/addcount X -> to add your weekly count of gym attendance where 'X' is a number between 0-7
/updatecount X -> update an existing count
/mytotal -> your total number of gym days since Jan 2023
/myweeklycount -> your total number of days for the current week
/weeklyview -> a graph of your weekly attendance
""")
#######################
#Add your weekly count#
#######################
@bot.message_handler(commands=["addcount"])
def add_count(msg):
week_start = start_of_week(msg.date)
# Print for debugging purpose
print('name: ', msg.from_user.first_name, '\n w\c: ',
week_start, '\n count: ', msg.text[-1], '\n chat type: ',
msg.chat.type)
# Extract count from message
try:
count = msg.text.split(" ")[1]
if not count:
bot.reply_to(msg,"You did not specify a number")
# Check if value is within range 0-7
elif re.match(r"^[0-9]+$",count):
if int(count) not in range(0,8):
bot.reply_to(msg, "There are only 7 days in a week you donut")
# Checking if count already exists in database
elif db.find_one({"WeekStarting":week_start,"User_id":msg.from_user.id}):
bot.reply_to(msg, "You have already submitted a count for this week")
else:
db.insert_one({"WeekStarting":week_start,
"DateSubmitted":msg.date,
"User_id":msg.from_user.id,
"Username":msg.from_user.username,
"Name":msg.from_user.first_name,
"Count":count})
bot.reply_to(
msg, """The following count has been added:
name: %s,
w\c: %s,
count: %s
""" % (msg.from_user.first_name, week_start, count))
else:
bot.reply_to(msg,"You did not specify a number")
except IndexError:
bot.reply_to(msg, "You did not specify a number")
##########################
#Update your weekly count#
##########################
@bot.message_handler(commands=["updatecount"])
def update_count(msg):
try:
count = msg.text.split(" ")[1]
# Checking for count
if not count:
bot.reply_to(msg, "You did not specify a number")
elif re.match(r"^[0-9]+$",count):
if int(count) not in range(0,8):
bot.reply_to(msg, "There are only 7 days in a week you donut")
else:
db.update_one({"WeekStarting":start_of_week(msg.date),"User_id":msg.from_user.id},
{"$set": {"Count":count}})
bot.reply_to(msg, f"Your count has been updated to: {count}")
except IndexError:
bot.reply_to(msg, "You did not specify a number")
#######################################
#Return total count for full database#
#######################################
@bot.message_handler(commands=["mytotal"])
def return_year_total(msg):
# Finding database entries for specific user
all_entries = list(db.find({"User_id":msg.from_user.id}))
total_count = sum([int(x['Count']) for x in all_entries])
bot.reply_to(msg, f"Your overall total attendace is: {total_count}")
###########################
#Return count for the week#
###########################
@bot.message_handler(commands=["myweeklycount"])
def return_weekly_count(msg):
# Finding database entry for specific user
week_start = start_of_week(msg.date)
try:
count = db.find_one({"WeekStarting":week_start,"User_id":msg.from_user.id})["Count"]
bot.reply_to(msg, f"Your total for this week is: {count}")
# Otherwise return nothing
except TypeError:
bot.reply_to(msg, "You have not submitted a count for this week")
#########################################
#Return a chart showing weekly breakdown#
#########################################
@bot.message_handler(commands=["weeklyview"])
def return_specified_week_count(msg):
# Generate graph
barchart(msg,db)
# Send image of graph
bot.send_photo( msg.chat.id, open("chart.png", "rb") )
bot.polling()
'''
@bot.message_handler(func=lambda msg: msg.from_user.username == "ElRataAlada"))
def send_help_message(msg):
print(msg.from_user.id, msg.from_user.username)
bot.reply_to(msg,"What colour is your deadlift?")
'''