-
Notifications
You must be signed in to change notification settings - Fork 2
/
qotd.py
executable file
·78 lines (62 loc) · 2.13 KB
/
qotd.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Licensed under the terms of the MIT License
from __future__ import division
from __future__ import unicode_literals
import logging
import json
import datetime
import random
import slackbot
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)
sb = slackbot.SlackBot()
with file('quotes.json', 'r') as quotesfile:
quotes = [json.loads(line) for line in quotesfile.readlines()]
with file('attributions.csv', 'r') as attributionsfile:
attributions = [line.strip() for line in attributionsfile.readlines()]
#sb.setup()
#Slack formatting
#*bold* `code` _italic_ ~strike~
def addQuote(msg):
l = len(sb.botcheck) + 4
q = {
'quote': msg['text'][l:].strip('"“'),
'user': msg['user'],
'time': str(datetime.datetime.utcnow())
}
logging.info('Quote is: ' + q["quote"])
f = open('quotes.json', 'a')
f.write("\n" + json.dumps(q))
f.close()
quotes.append(q)
output = sb.sclient.api_call(
'chat.postMessage',
as_user='true',
channel=msg['channel'],
text='\t_*"' + q["quote"] + '"*_\n\tQuote added. High Five <@' +
msg['user'] + '>!')
logging.debug(output)
def printQuote(msg):
output = sb.sclient.api_call(
'chat.postMessage',
as_user='true',
channel=msg['channel'],
text='\t' + random.choice(attributions) + ':\n\t\t_*"' +
random.choice(quotes)["quote"] + '"*_')
logging.debug(output)
def listQuotes(msg):
mylist = 'Quotes:\n'
for q in quotes:
mylist += '\t_*' + q["quote"] + '*_\n'
output = sb.sclient.api_call(
'chat.postMessage',
as_user='true',
channel=msg['channel'],
text=mylist + '\n\n\t' + str(len(quotes)) + ' total quotes.')
logging.debug(output)
# Help command is auto added
sb.addCommand('quote', printQuote, 'quote [prints random quote]')
sb.addCommand('add', addQuote, 'add <some witty quote I want to add>')
sb.addCommand('list', listQuotes, 'list [prints out all quotes]')
# Begin monitoring the channel and respond when a command is triggered, this call blocks
sb.monitor()