-
Notifications
You must be signed in to change notification settings - Fork 2
/
template_bot.py
executable file
·354 lines (290 loc) · 11.6 KB
/
template_bot.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim: set expandtab tabstop=4 shiftwidth=4 :
# template_bot.py - Bot written in Python that...
#
# This is part of the Exocortex Halo project
# (https://github.com/virtadpt/exocortex-halo/).
# By: The Doctor <drwho at virtadpt dot net>
# 0x807B17C1 / 7960 1CDC 85C9 0B63 8D9F DD89 3BD8 FF2B 807B 17C1
# License: GPLv3
# v1.3 - Updated some comments and boilerplate.
# - Changed ConfigParser to configparser, per Python 3 and PEP-8.
# v1.2 - Changed logging.warn() to logging.warning().
# - Reworked the startup logic so that being unable to immediately
# connect to either the message bus or the intended service is a
# terminal state. Instead, it loops and sleeps until it connects and
# alerts the user appropriately.
# - Changed logger to logging.
# v1.1 - Updated template to look more like the code I have in production.
# - Added the facility for user-defined output.
# v1.0 - Initial release.
# TO-DO:
# -
# Load modules.
import argparse
import configparser
import json
import logging
import os
import requests
import sys
import time
# Constants.
# When POSTing something to a service, the correct Content-Type value has to
# be set in the request.
custom_headers = {"Content-Type": "application/json:"}
# Global variables.
# Handle to a logging object.
logger = ""
# Path to and name of the configuration file.
config_file = ""
# Loglevel for the bot.
loglevel = logging.INFO
# The "http://system:port/" part of the message queue URL.
server = ""
# URL to the message queue to take marching orders from.
message_queue = ""
# The name the search bot will respond to. The idea is, this bot can be
# instantiated any number of times with different config files to use
# different search engines on different networks.
bot_name = ""
# How often to poll the message queues for orders.
polling_time = 10
# String that holds the command from the user prior to parsing.
user_command = None
# Handle to a parsed user command.
parsed_command = None
# Optional user-defined text strings for the online help and user interaction.
user_text = None
user_acknowledged = None
# Functions.
# set_loglevel(): Turn a string into a numerical value which Python's logging
# module can use because.
def set_loglevel(loglevel):
if loglevel == "critical":
return 50
if loglevel == "error":
return 40
if loglevel == "warning":
return 30
if loglevel == "info":
return 20
if loglevel == "debug":
return 10
if loglevel == "notset":
return 0
# parse_...(): Takes a string and figures out if it was a correctly
# formatted request to... Requests
# are of the form "..". Returns ...
# or None if it's not a well-formed request.
def parse_...(user_command):
logger.debug("Entered function parse_...().")
words = []
# Clean up the search request.
user_command = user_command.strip()
user_command = user_command.strip(",")
user_command = user_command.strip(".")
user_command = user_command.strip("'")
user_command = user_command.strip("?")
user_command = user_command.strip("!")
# If the user command is empty (i.e., nothing in the queue) return None.
if "no commands" in user_command:
logging.debug("Got an empty command.")
return None
# Tokenize the search request.
words = user_command.split()
logging.debug("Tokenized command: " + str(words))
# Start parsing the the command to see what kind it is. After
# making the determination, remove the words we've sussed out to make the
# rest of the query easier.
# User asked for help.
if not len(words):
return None
if words[0].lower() == "help":
logging.debug("User asked for online help.")
return words[0]
# User asked the construct to...
if (words[0] == "foo") or (words[0] == "bar") or \
(words[0] == "baz"):
logging.info("Got a token that suggests that this is...")
del words[0]
# If the parsed search term is now empty, return an error.
if not len(words):
logging.error("The indexing request appears to be empty.")
return None
# The above, one or more times...
# Return the final result.
logging.debug("Final result: " + something_final_result)
return something_final_result
# send_message_to_user(): Function that does the work of sending messages back
# to the user by way of the XMPP bridge. Takes one argument, the message to
# send to the user. Returns a True or False which delineates whether or not
# it worked.
def send_message_to_user(message):
# Set up a hash table of stuff that is used to build the HTTP request to
# the XMPP bridge.
reply = {}
reply["name"] = bot_name
reply["reply"] = message
# Send an HTTP request to the XMPP bridge containing the message for the
# user.
request = requests.put(server + "replies", headers=custom_headers,
data=json.dumps(reply))
return
# online_help(): Utility function that sends online help to the user when
# requested. Takes no args. Returns nothing.
def online_help():
reply = "My name is " + bot_name + " and I am an instance of " + sys.argv[0] + ".\n\n"
if user_text:
reply = reply + user_text + "\n\n"
reply = reply + """I am... send me a message that looks something like this:\n\n"""
reply = reply + bot_name + ", [command, synonym, synonym...] args...\n\n"
reply = reply + bot_name + ", [command, synonym, synonym...] args...\n\n"
reply = reply + bot_name + ", [command, synonym, synonym...] args...\n\n"
send_message_to_user(reply)
return
# Core code...
# Set up the command line argument parser.
argparser = argparse.ArgumentParser(description="A bot that polls a message queue for...")
# Set the default config file and the option to set a new one.
argparser.add_argument("--config", action="store",
default="./template_bot.conf")
# Loglevels: critical, error, warning, info, debug, notset.
argparser.add_argument("--loglevel", action="store",
help="Valid log levels: critical, error, warning, info, debug, notset. Defaults to info.")
# Time (in seconds) between polling the message queues.
argparser.add_argument("--polling", action="store", help="Default: 10 seconds")
# Parse the command line arguments.
args = argparser.parse_args()
if args.config:
config_file = args.config
# Read the options in the configuration file before processing overrides on the
# command line.
config = ConfigParser.ConfigParser()
if not os.path.exists(config_file):
logging.error("Unable to find or open configuration file " +
config_file + ".")
sys.exit(1)
config.read(config_file)
# Get the URL of the message queue to contact.
server = config.get("DEFAULT", "queue")
# Get the names of the message queues to report to.
bot_name = config.get("DEFAULT", "bot_name")
# Construct the full message queue URL.
message_queue = server + bot_name
# Get the default loglevel of the bot.
config_log = config.get("DEFAULT", "loglevel").lower()
if config_log:
loglevel = set_loglevel(config_log)
# Set the number of seconds to wait in between polling runs on the message
# queues.
try:
polling_time = config.get("DEFAULT", "polling_time")
except:
# Nothing to do here, it's an optional configuration setting.
pass
# Set the loglevel from the override on the command line.
if args.loglevel:
loglevel = set_loglevel(args.loglevel.lower())
# Get user-defined doing-stuff text if defined in the config file.
try:
user_text = config.get("DEFAULT", "user_text")
except:
# Nothing to do here, it's an optional configuration setting.
pass
# Get additional user text if defined in the config file.
try:
user_acknowledged = config.get("DEFAULT", "user_acknowledged")
except:
# Nothing to do here, it's an optional configuration setting.
pass
# Configure the logger.
logging.basicConfig(level=loglevel, format="%(levelname)s: %(message)s")
logger = logging.getLogger(__name__)
# Set the message queue polling time from override on the command line.
if args.polling:
polling_time = args.polling
# Parse the rest of the configuration file...
# Debugging output, if required.
logging.info("Everything is set up.")
logging.debug("Values of configuration variables as of right now:")
logging.debug("Configuration file: " + config_file)
logging.debug("Server to report to: " + server)
logging.debug("Message queue to report to: " + message_queue)
logging.debug("Bot name to respond to search requests with: " + bot_name)
logging.debug("Time in seconds for polling the message queue: " +
str(polling_time))
if user_text:
logging.debug("User-defined help text: " + user_text)
if user_acknowledged:
logging.debug("User-defined command acknowledgement text: " + user_acknowledged)
# Other debugging output...
# Try to contact the XMPP bridge. Keep trying until you reach it or the
# system shuts down.
logging.info("Trying to contact XMPP message bridge...")
while True:
try:
send_message_to_user(bot_name + " now online.")
break
except:
logging.warning("Unable to reach message bus. Going to try again in %s seconds." % polling_time)
time.sleep(float(polling_time))
# Trying to contact other resources and sleeping if we can't (like the above)
# go here...
# Go into a loop in which the bot polls the configured message queue with each
# of its configured names to see if it has any search requests waiting for it.
logging.debug("Entering main loop to handle requests.")
while True:
user_command = None
# Check the message queue for commands.
try:
logging.debug("Contacting message queue: " + message_queue)
request = requests.get(message_queue)
except:
logging.warning("Connection attempt to message queue timed out or failed. Going back to sleep to try again later.")
time.sleep(float(polling_time))
continue
# Test the HTTP response code.
# Success.
if request.status_code == 200:
logging.debug("Message queue " + bot_name + " found.")
# Extract the user command.
user_command = json.loads(request.text)
logging.debug("Value of user_command: " + str(user_command))
user_command = user_command["command"]
# Parse the user command.
parsed_command = parse_...(user_command)
# If the parsed command comes back None (i.e., it wasn't well formed)
# throw an error and bounce to the top of the loop.
if not parsed_command:
time.sleep(float(polling_time))
continue
# If the user is requesting help, assemble a response and send it back
# to the server's message queue.
if parsed_command.lower() == "help":
online_help()
continue
# Tell the user what the bot is about to do.
if user_acknowledged:
send_message_to_user(user_acknowledged)
else:
reply = "Doing the thing. Please stand by."
send_message_to_user(reply)
parsed_command = do_the_thing(parsed_command)
# If something went wrong...
if not parsed_command:
logging.warning("Something went wrong with...")
reply = "Something went wrong with..."
send_message_to_user(reply)
continue
# Reply that it was successful.
reply = "Tell the user that it was successful."
send_message_to_user(reply)
# Message queue not found.
if request.status_code == 404:
logging.info("Message queue " + bot_name + " does not exist.")
# Sleep for the configured amount of time.
time.sleep(float(polling_time))
# Fin.
sys.exit(0)