forked from ClearMind/heliosphere
/
dinklebot.py
195 lines (154 loc) · 5.84 KB
/
dinklebot.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
__author__ = 'artemredkin'
import json
import urllib
import urlparse
import db
import telegram
import collections
import rsvp
import logging
import random
import poll
from google.appengine.api import urlfetch
def parse(message):
values = message.split(None, 1)
if len(values) == 1:
return values[0], None
return values
class Registry():
def __init__(self):
self.commands = commands = collections.OrderedDict({})
def register(self, command):
self.commands[command.name()] = command
class Command:
def call(self, chat, author, arguments):
pass
def help(self):
pass
def name(self):
pass
def description(self):
pass
class EchoCommand(Command):
def call(self, chat, author, arguments):
telegram.send(chat, arguments.encode('utf-8'))
def help(self):
return "Usage: /echo <string>"
def name(self):
return "/echo"
def description(self):
return "Send back message"
class ImageCommand(Command):
def __init__(self):
self.google_search_key = None
self.cache_list = []
self.cache = {}
def find(self, q, start):
if self.google_search_key is None:
self.google_search_key = db.get_key('google_search')
data = {
'key': self.google_search_key,
'cx': '009373417816394415455:i3e_omr58us',
'q': q.encode('utf-8'),
'searchType': 'image',
'safe': 'medium',
'num': 10
}
response = urlfetch.fetch(url='https://www.googleapis.com/customsearch/v1?' + urllib.urlencode(data), method=urlfetch.GET)
return json.loads(response.content)['items']
def call(self, chat, author, q):
if q in self.cache:
(items, offset) = self.cache[q]
if offset == 10:
items = self.find(q, 10)
offset = 0
self.cache[q] = (items, offset + 1)
image = items[offset]
else:
items = self.find(q, 0)
if len(items) == 0:
telegram.send(chat, "Nothing found")
return
image = items[0]
if self.cache_list == 10:
last = self.cache_list[0]
self.cache_list.pop(0)
self.cache.pop(last, None)
self.cache_list.append(q)
self.cache[q] = (items, 1)
image_url = image['link']
image_response = urlfetch.fetch(image_url, method=urlfetch.GET)
if image_response.status_code != 200:
telegram.send(chat, "Error retrieving image")
return
image_name = urlparse.urlsplit(image_url).path.split('/')[-1]
telegram.send_image(chat, image_response.content, image_name, image['mime'])
def help(self):
return "Usage: /img <query>"
def name(self):
return "/img"
def description(self):
return "Google Image Search"
class QuoteCommand(Command):
def __init__(self):
self.quotes = ['We\'ve woken the hiiiveee!', 'Well, at least it\'s chained up.',
'An I thought YOU had the hard job.', 'I\'m beginning to sense a pattern!',
'Don\'t do that.', 'Can\'t we just stay here with the murderous robots?',
'Well that had to ruin their day.', 'I think you got your point across.',
'IT\'S IN THE WALLS!!!', 'That wizard came from The Moon.',
'Just so you know I have no idea what I\'m doing','This should take us right to the grave.',
'I\'m a Ghost, actually', 'We may want to move back.',
'Think they\'d mind if we take their Pikes?','Fallen ships this close to the surface.... MOVE!',
'This will take just a little while longer.', 'So... think you can kill a God?',
'This could be bad!', 'This could be good!', 'This could be going better...',
'Those Fallen are just as crazy as we are.', 'I\'ll work faster']
def call(self, chat, author, arguments):
telegram.send(chat, random.choice(self.quotes))
def help(self):
return "Usage: /quote"
def name(self):
return "/quote"
def description(self):
return "Dinklebot Wisdom"
r = Registry()
r.register(EchoCommand())
r.register(ImageCommand())
r.register(QuoteCommand())
r.register(rsvp.RsvpRegisterCommand())
r.register(rsvp.RsvpTypesCommand())
r.register(rsvp.RsvpListCommand())
r.register(rsvp.RsvpNewCommand())
r.register(rsvp.RsvpJoinCommand())
r.register(rsvp.RsvpLeaveCommand())
r.register(rsvp.RsvpDeleteCommand())
r.register(poll.PollCommand())
r.register(poll.NewPollCommand())
r.register(poll.AsnwerCommand())
r.register(poll.VotePollCommand())
r.register(poll.ResultPollCommand())
r.register(poll.EndPollCommand())
def recieve(request):
commands = r.commands
(chat, author, message) = telegram.recieve(request)
if message.startswith('/'):
(command, arguments) = parse(message)
if command == '/help':
if arguments is not None:
cmd = '/' + arguments
if cmd not in commands:
telegram.send(chat, "Uknown command: " + str(arguments))
return
telegram.send(chat, commands[cmd].help())
return
response = 'Commands:'
for name, command in commands.iteritems():
response += '\n' + name + ': ' + command.description()
response += '\n\nType /help <command> to know more'
telegram.send(chat, response)
return
if command in commands:
try:
commands[command].call(chat, author, arguments)
except Exception as e:
logging.info(request)
logging.error(e, exc_info=True)