-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathandreabot.py
210 lines (173 loc) · 7.9 KB
/
andreabot.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
#!/usr/bin/python
import sys
import time
import datetime
import re
import telepot
from telepot.delegate import per_chat_id, create_open
# andreabot modules
from settings_secret import TOKEN
from botlogger import logger
import authorized
import helper
# establish connection to mongodb server
import pymongo
try:
connection = pymongo.MongoClient('monty', 27017)
logger.info('Successfully connected to MongoDB daemon')
except pymongo.errors.ConnectionFailure as e:
logger.error('Failed to connect to MongoDB: %s' % e)
logger.error('AndreaBot exiting!')
sys.exit(1)
db = connection['andreabot']
announcements = db['announcements']
def getTime():
return datetime.datetime.fromtimestamp(time.time()).strftime('%H:%M:%S, %d %B %Y ')
def getLog(to_return):
messages = announcements.find().sort( [ ('_id', -1) ] ).limit(to_return)
delimiter = '======================\n'
reply = delimiter
if messages.count() == 0:
reply += 'No records found.\n' + delimiter
else:
for message in messages:
reply = delimiter + message['message'] + message['timestamp'] + '\n' + reply
reply += '%d most recent announcements ^' % to_return
return reply
def yell(bot, message, requester_id):
# deny unauthorized yeller
if requester_id not in authorized.getMailingList() or requester_id < 0:
logger.warn('Attempted unauthorized use of /yell by %s' % authorized.whoIs(requester_id))
bot.sendMessage(requester_id, 'Sorry! You can\'t use this function. Contact Darren @ohdearren if this is a mistake.')
return
# deny empty yell
if re.match('^\s*/yell\s*$', message) != None:
logger.warn('%s: /yell denied; no message' % authorized.whoIs(requester_id))
bot.sendMessage(requester_id, 'I need a message to yell! Yell not carried out.')
return
# strip the command word
broadcast = re.sub('^/yell\s+', '', message)
# append byline
broadcast += '\n\nSent by %s via yell\n' % (authorized.whoIs(requester_id))
# add the announcement to the database
timestamp = datetime.datetime.fromtimestamp(time.time()).strftime('%I:%M%p, %d %B %Y')
announcement_log = {
'message': broadcast,
'to': 'all',
'by': authorized.whoIs(requester_id),
'timestamp': timestamp,
}
announcements.insert_one(announcement_log)
logger.info('Added yell by %s to database.' % authorized.whoIs(requester_id))
# talk to the requester
bot.sendMessage(requester_id, 'Message is propagating now all listeners.')
for recipient in authorized.getMailingList():
bot.sendMessage(recipient, broadcast)
logger.info('%s: Yell propagated to %s (\'%s ...\')' % (authorized.whoIs(requester_id), authorized.whoIs(recipient), broadcast[:60]))
return
def whisper(bot, message, needAcknowledgement, requester_id):
# deny unauthorized yeller
if requester_id not in authorized.getMailingList() or requester_id < 0:
logger.warn('Attempted unauthorized use of /whisper by %s' % authorized.whoIs(requester_id))
bot.sendMessage(requester_id, 'Sorry! You can\'t use this function. Contact Darren @ohdearren if this is a mistake.')
return
# deny empty whisper
if re.match('^\s*/whisper\s*$', message) != None:
logger.warn('%s: /whisper denied; no message or groups!' % authorized.whoIs(requester_id))
bot.sendMessage(requester_id, 'I need a message and group list to whisper! Whisper not carried out.')
return
matches = re.match('\s*/whisper\s+([a-zA-Z+]+)\s+(.+)', message)
groups = matches.group(1).lower()
broadcast = matches.group(2)
group_list = groupArg2List(groups)
timestamp = datetime.datetime.fromtimestamp(time.time()).strftime('%I:%M%p, %d %B %Y')
if authorized.groupIsValid(group_list):
broadcast = re.sub('^\s*/whisper\s*[a-zA-Z+]+', '', message)
broadcast += '\n\nSent by %s via whisper to:\n' % (authorized.whoIs(requester_id))
broadcast += ', '.join(group_list) + '\n'
bot.sendMessage(requester_id, 'Message is propagating now to: %s' % ', '.join(group_list))
# log in database
announcement_log = {
'message': broadcast,
'to': '+'.join(group_list),
'by': authorized.whoIs(requester_id),
'timestamp': timestamp,
}
announcements.insert_one(announcement_log)
logger.info('Added whisper by %s to database.' % authorized.whoIs(requester_id))
# propagate message
for recipient in authorized.getGroups(group_list):
bot.sendMessage(recipient, broadcast)
logger.info('%s: Whisper propagated to %s (\'%s ...\')' % (authorized.whoIs(requester_id), authorized.whoIs(recipient), broadcast[:60]))
# echo to sender
bot.sendMessage(requester_id, 'MESSAGE ECHO\n\n' + broadcast)
# inform darren
bot.sendMessage(authorized.address_book.get('Darren'), 'Whisper sent by %s to group \'%s\'' % (authorized.whoIs(requester_id), groups))
else:
# bad group parameters
bot.sendMessage(requester_id, 'One of your groups is not valid. Check /who for the group names.')
logger.warn('%s: /whisper denied; invalid group' % authorized.whoIs(requester_id))
return
def groupArg2List(groupList):
if '+' not in groupList:
return [groupList]
else:
return re.split('\s*+\s*', groupList)
#class AndreaBot(telepot.helper.ChatHandler):
class AndreaBot(telepot.Bot):
def __init__(self, *args, **kwargs):
super(AndreaBot, self).__init__(*args, **kwargs)
self._answerer = telepot.helper.Answerer(self)
self._message_with_inline_keyboard = None
# def __init__(self, seed_tuple, timeout):
# super(AndreaBot, self).__init__(seed_tuple, timeout)
# self._count = 0
def on_chat_message(self, message):
content_type, chat_type, chat_id = telepot.glance(message)
command = message['text']
logger.info('Received \'%s\' from %s' % (command, authorized.whoIs(chat_id)))
# for '/start'
if command == '/start':
self.sendMessage(chat_id, 'Hi! I\'m AndreaBot. I help Andrea and other FOP comm members disseminate information. Your name is \'%s\'.' % authorized.whoIs(chat_id))
# /help [<command>]
elif command == '/help':
self.sendMessage(chat_id, helper.getNaiveHelp())
elif command.startswith('/help'):
keyword = re.match('\s*/help\s+([a-z]+)\s*', command).group(1)
self.sendMessage(chat_id, helper.getHelp(keyword))
# /who
elif command == '/who':
self.sendMessage(chat_id, authorized.enumerateListeners())
# /yell
elif command.startswith('/yell'):
yell(self, command, chat_id)
# /whisper
elif command.startswith('/whisper'):
whisper(self, command, False, chat_id)
# /log
elif command == '/log':
self.sendMessage(chat_id, getLog(5))
# /vlog <size>
elif command.startswith('/vlog'):
return_size = re.match('\s*/vlog\s+(\d+)', command).group(1)
self.sendMessage(chat_id, getLog(int(return_size)))
# /time
elif command == '/time':
self.sendMessage(chat_id, getTime())
elif command.startswith('/name'):
name = re.match('\s*/name\s+(.+)', command).group(1)
logger.info('/name: %s is \'%s\'' % (chat_id, name))
self.sendMessage(authorized.address_book.get('Darren'), '%s is %s' % (chat_id, name))
self.sendMessage(chat_id, 'Thanks! You will be added shortly.')
# when nothing works
else:
self.sendMessage(chat_id, 'Invalid command. Try /help')
return
logger.info('AndreaBot is listening ...')
bot = AndreaBot(TOKEN)
bot.message_loop()
#bot = telepot.DelegatorBot(TOKEN, [
# (per_chat_id(), create_open(AndreaBot, timeout=120)),
#])
while 1:
time.sleep(10)