-
Notifications
You must be signed in to change notification settings - Fork 1
/
voglbot.py
138 lines (119 loc) · 5.98 KB
/
voglbot.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
#!/usr/bin/python
import sys
import time
import random
import threading
import telepot
import pymongo
import re
from telepot.delegate import per_chat_id, create_open
# voglbot modules
from settings_secret import TOKEN # telegram API key
from authorized import * # user management
from manager import * # database management
from voglogger import logger # logger object
import helper # /help documentation
# regular expressions for commands
register_re = re.compile('(/[a-z]+)\s+([a-z]+)\s+(.+)', re.I)
updater_re = re.compile('(/[a-z]+)\s+([a-z+)\s+([a-z]+)\s+(.+)', re.I)
class VOGLBot(telepot.Bot):
def __init__(self, *args, **kwargs):
super(VOGLBot, self).__init__(*args, **kwargs)
self._answerer = telepot.helper.Answerer(self)
self._message_with_inline_keyboard = None
self._previous = ''
# def __init__(self, seed_tuple, timeout):
# super(VOGLBot, self).__init__(seed_tuple, timeout)
# self._previous = ''
# message handler
def on_chat_message(self, message):
content_type, chat_type, chat_id = telepot.glance(message)
# check for message type
if content_type != 'text':
self.sendMessage(chat_id, 'This bot can only receive text messages!')
return
command = message['text'].lower()
logger.info('Received \'%s\' from %s' % (command, whoIs(chat_id)) )
# message for /start
if command == '/start':
self.sendMessage(chat_id, 'Hi! I\'m VOGLBot, a friendly robot assistant for USC FOP 2016.')
self.sendMessage(chat_id, 'You are \'%s\'. If you do not see your name displayed, you cannot use this bot.' % whoIs(chat_id))
return
# deny unauthorized user access
if chat_id not in getIDs(authorized):
logger.warning('Attempted unauthorized access by %s.' % whoIs(chat_id))
self.sendMessage(chat_id, 'You are not authorized to use this bot. Contact Darren at 92328340 or @ohdearren if this is a mistake.')
return
# do stuff
if command == '/help':
self.sendMessage(chat_id, helper.naiveHelp())
elif command.startswith('/help'):
matches = re.match('/help ([a-z]+)', command)
self.sendMessage(chat_id, helper.getHelp(matches.group(1)))
# registration-type commands
elif command.startswith('/add'):
matches = re.match(register_re, command)
self.sendMessage(chat_id, add(matches.group(2), matches.group(3), chat_id))
elif command.startswith('/remove'):
matches = re.match(register_re, command)
self.sendMessage(chat_id, remove(matches.group(2), matches.group(3), chat_id))
elif command.startswith('/strength'):
matches = re.match(register_re, command)
self.sendMessage(chat_id, getStrength(matches.group(2), matches.group(3), chat_id))
elif command.startswith('/enum'):
house = re.match('\s*/enum\s+([a-z]+)', command).group(1)
self.sendMessage(chat_id, getEnumerate(house, chat_id))
elif command.startswith('/find'):
matches = re.match(register_re, command)
self.sendMessage(chat_id, find(matches.group(2), matches.group(3), False, chat_id))
elif command.startswith('/vfind'):
matches = re.match(register_re, command)
self.sendMessage(chat_id, find(matches.group(2), matches.group(3), True, chat_id))
elif command.startswith('/in'):
matches = re.match('/in\s+([a-z]+)\s+(.+)', command)
if matches != None:
house, name = matches.groups()
self.sendMessage(chat_id, updater(house, name, 'status', 'present', chat_id))
inform(self, ['Yuchuan'], '!! %s (%s) checked in' % (name.title(), house.title()))
updateAttendanceLog(house, name, True, chat_id)
else:
self.sendMessage(chat_id, 'Update failed. No such house/person.')
elif command.startswith('/out'):
matches = re.match('/out\s+([a-z]+)\s+(.+)', command)
if matches != None:
house, name = matches.groups()
self.sendMessage(chat_id, updater(house, name, 'status', 'absent', chat_id))
inform(self, ['Yuchuan'], '!! %s (%s) checked out' % (name.title(), house.title()))
updateAttendanceLog(house, name, False, chat_id)
else:
self.sendMessage(chat_id, 'Update failed. No such house/person.')
elif command.startswith('/medical'):
matches = re.match('/medical\s+([a-z]+)\s+(.+)\s*:\s*(.+)', command)
if matches != None:
house, name, message = matches.groups()
self.sendMessage(chat_id, updater(house, name, 'medical', message, chat_id))
inform(self, ['Yuchuan'], '!! %s (%s) medical info updated: %s' % (name.title(), house.title(), message))
else:
self.sendMessage(chat_id, 'Update failed. No such house/person.')
elif command.startswith('/diet'):
matches = re.match('/diet\s+([a-z]+)\s+(.+)\s*:\s*(.+)', command)
if matches != None:
house, name, message = matches.groups()
self.sendMessage(chat_id, updater(house, name, 'diet', message, chat_id))
else:
self.sendMessage(chat_id, 'Update failed. No such house/person.')
elif command.startswith('/log'):
house, name = re.search('/log\s+([a-z]+)\s+(.+)', command).groups()
self.sendMessage(chat_id, getAttendanceLog(house, name, chat_id))
self._previous = command
return
logger.info('VOGLBot is listening ...')
# the timeout here refers to when a delegator starts over
#bot = telepot.DelegatorBot(TOKEN, [
# (per_chat_id(), create_open(VOGLBot, timeout=120)),
#])
#bot.message_loop(run_forever=True)
bot = VOGLBot(TOKEN)
bot.message_loop()
while 1:
time.sleep(10)