-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot-main.py
executable file
·144 lines (103 loc) · 5.13 KB
/
bot-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
import telegram
import emoji
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, ConversationHandler
import datetime
import logging, csv, io, os, sys
#Credentials file
from credentials import bot_creds
############
## SET-UP ##
############
#Setting up logging
#Generate log name with path based on cwd of this Python script
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt='%H:%M:%S',
level=logging.INFO)
#Instatiate logger
logger = logging.getLogger(__name__)
log_fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), 'ema_log.csv')
fh = logging.FileHandler(log_fname)
fh.setLevel(logging.INFO)
logger.addHandler(fh)
###############
## FUNCTIONS ##
###############
#Function that defines what the bot should do on receiving the \start message
def start(update: telegram.Update, context: telegram.ext.CallbackContext):
context.bot.send_message(chat_id=update.message.chat_id,
text=emoji.emojize("Welcome to the University of Bristol's Mood Music Study! :musical_note: \nThanks so much for taking part. \nPlease remember to enable notifications from Telegram. \n\nYou will recieve a notification when it is time for you to answer a survey."))
#context.job_queue.run_daily(ema_start, datetime.time(20, 35, 00, 000000), context=update.message.chat_id)
context.job_queue.run_repeating(ema_start, interval=120, first=10, context=update.message.chat_id)
#Function for the /help command
def helpme(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="Soon this will show you all my available commands!")
#Function to manage unrecognised commands
def unknowncommand(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="Sorry, I didn\'t understand that command")
#Function to manage text input
def unknowntext(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="Sorry, I\'m only able to understand your numeric survey answers. \nPlease email nina.dicara@bristol.ac.uk if you need anything, or wait until your next survey. \nThank you!")
#Initialise the states for conversation handler.
HAPPINESS, ENERGY = range(2)
def ema_start(context: telegram.ext.CallbackContext):
#Custom keyboard layout
kb = [['1','2', '3', '4', '5'], ['6', '7', '8', '9', '10']]
kb_markup = telegram.ReplyKeyboardMarkup(kb, one_time_keyboard=True)
context.bot.send_message(chat_id=context.job.context, text = "Hi, it\'s time for another survey. \nHow happy do you feel right now, where 1 = Very Unhappy and 10 = Very Happy?",
reply_markup=kb_markup)
return HAPPINESS
def ema_happiness(update, context):
user = update.message.from_user
#Logs: username, chat id, datetime of original message, question, datetime of reply, reply info
logger.info("%s, %s, %s, Happiness, %s, %s", user.username, update.message.chat_id, update.message.forward_date, update.message.date, update.message.text)
#Custom keyboard layout
kb = [['1','2', '3', '4', '5'], ['6', '7', '8', '9', '10']]
kb_markup = telegram.ReplyKeyboardMarkup(kb, one_time_keyboard=True)
update.message.reply_text(
"How energetic do you feel right now, where 1 = Very Tired and 10 = Very Energetic?",
reply_markup=kb_markup)
return ENERGY
def ema_energy(update, context):
user = update.message.from_user
#Logs: username, chat id, datetime of original message, question, datetime of reply, reply info
logger.info("%s, %s, %s, Energy, %s, %s", user.username, update.message.chat_id, update.message.forward_date, update.message.date, update.message.text)
update.message.reply_text("Thank you! All done.")
return ConversationHandler.END
#######################
## MAIN METHOD STUFF ##
#######################
def main():
#Initialise the updater and dispatcher for main method.
updater = Updater(token=bot_creds, use_context=True)
dispatcher = updater.dispatcher
###########################
## CONVERSATION HANDLERS ##
###########################
ema_handler = ConversationHandler(
entry_points=[MessageHandler(Filters.regex('[1-9]|10'), ema_happiness)],
states={
ENERGY: [MessageHandler(Filters.regex('[1-9]|10'), ema_energy)]
},
fallbacks = []
)
#EMA Conversation Handler
dispatcher.add_handler(ema_handler)
######################
## COMMAND HANDLERS ##
######################
#Start Handler which sets up the Job-Queue
dispatcher.add_handler(CommandHandler('start', start, pass_job_queue=True))
#Help Command Handler
dispatcher.add_handler(CommandHandler('help', helpme))
######################
## MESSAGE HANDLERS ##
######################
#Unknown Command Handler -
dispatcher.add_handler(MessageHandler(Filters.command, unknowncommand))
#Unknown Anything Handler -
dispatcher.add_handler(MessageHandler(Filters.text, unknowntext))
#Set Bot running
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()