-
Notifications
You must be signed in to change notification settings - Fork 1
/
start.py
executable file
·160 lines (141 loc) · 4.73 KB
/
start.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
#!/usr/bin/env python
import httplib
import urllib
import json
import sys
from modules.basic_module import BasicModule
from modules.dice import DiceModule
from modules.lunch import LunchModule
from modules.finance import FinanceModule
from modules.wordcheck import WordCheckModule
from modules.kvdb import KVDBModule
from modules.memo import MemoModule
from modules.event import EventModule
from modules.aqi import AqiModule
from modules.rss import RssModule
from modules.hiking import HikingModule
#from modules.github import GithubModule
from commands import Command, CommandParseException
class RJJBot:
SERVER = 'api.telegram.org'
with open('api-key.txt', 'r') as f:
key = f.read().strip()
BOT = '/bot%s/' % (key)
OFFSET_FILE = 'offset.txt'
def __init__(self):
self.modules = []
self.print_reply = False
def send_request(self, method, data={}):
c = httplib.HTTPSConnection(self.SERVER)
headers = {"Content-type": "application/json"}
try:
c.request('POST', self.BOT + method, json.dumps(data), headers)
r = c.getresponse()
if r.status != 200:
raise Exception('HTTP response %s: %s' % (r.status, r.reason))
try:
res = r.read()
o = json.loads(res)
if o['ok'] is not True:
raise Exception('Method returned failure: %s' % (o.get('result')))
return o['result']
except Exception, e:
raise Exception('Error parsing response JSON: %s' % (res))
except Exception, e:
print "ERROR SENDING REQUEST: %s" % (str(e))
def update_offset(self, offset):
x = self.get_offset()
if offset > x:
with open(self.OFFSET_FILE, 'w') as f:
f.write(str(offset))
def get_offset(self):
with open(self.OFFSET_FILE, 'r') as f:
x = f.read()
return int(x)
def process_messages(self, messages):
if messages is None:
return
for message in messages:
self.process_message(message)
def send_message(self, text, chat_id):
if isinstance(text, basestring):
self.send_request('sendMessage', {'chat_id': chat_id, 'text': text.encode('utf-8'), 'disable_web_page_preview': True})
elif text.get('type') == 'html':
self.send_request('sendMessage', {'chat_id': chat_id, 'text': text.get('content').encode('utf-8'), 'parse_mode': 'HTML', 'disable_web_page_preview': True if text.get('disable_preview') == None else text.get('disable_preview')})
elif text.get('type') == 'sticker':
self.send_request('sendSticker', { 'chat_id': chat_id, 'sticker': text.get('content', '') })
###### helpers above #######
def process_message(self, msg):
update_id = int(msg.get('update_id'))
self.update_offset(update_id)
print 'Now process %s' % (update_id)
m = msg.get('message')
if m is not None and m.get('message_id') is not None:
m_id = m.get('mesesage_id')
chat_id = m['chat']['id']
command = None
if 'text' in m:
try:
command = Command(m['text'])
except CommandParseException, e:
command = None
# Delegate messages to modules
for module in self.modules:
try:
reply = module.process_message(m, command)
except TypeError, e:
reply = module.process_message(m)
if reply != None:
if self.print_reply:
print reply
else:
self.send_message(reply, chat_id)
return
def start(self):
print "RJJ Standby"
while True:
try:
#if chat_id is not None:
# self.send_message(message, chat_id)
update_id = self.get_offset()
res = self.send_request('getUpdates', { 'offset': update_id + 1 })
self.process_messages(res)
except Exception, e:
raise
print 'Exception caught: ', type(e)
for module in self.modules:
ret = module.tick()
if ret is not None:
self.send_message(ret, '-8311114')
import time
time.sleep(1)
print "RJJ Close"
def start_local(self):
self.print_reply = True
print ("> "),
while True:
line = sys.stdin.readline()
msg = {
'update_id': 1,
'message': {
'text': line,
'message_id': 1,
'chat': { 'id': 9999 },
'from': {
'id': 'local',
'first_name': 'Local'
}
}
}
if len(line) == 0:
break
self.process_message(msg)
print ("> "),
print "RJJ Close"
if __name__ == '__main__':
rjj = RJJBot()
rjj.modules = [ AqiModule(), BasicModule(), MemoModule(), DiceModule(), LunchModule(), FinanceModule(), WordCheckModule(), KVDBModule(), EventModule(), RssModule(), HikingModule() ]
if (len(sys.argv) > 1 and sys.argv[1] == "local"):
rjj.start_local()
else:
rjj.start()