/
bot.py
69 lines (47 loc) · 1.55 KB
/
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
#!/usr/bin/env python
import sys
import time
import telegrambot as bot
from chat import chat
import argparse
parser = argparse.ArgumentParser(description='Telegram bot')
parser.add_argument('--tokenfile', help='Token file', required=True)
parser.add_argument('--sslcert', help='Public cert for telegram webhook')
parser.add_argument('--whookurl', help='Webhook url')
parser.add_argument('-w', help='Use webhook', action='store_const', const=1)
parser.add_argument('-a', help='Admin username')
args = parser.parse_args()
TOKENFILE = args.tokenfile
ssl_public_cert = args.sslcert
webhook_url = args.whookurl
if args.a:
adminuser = args.a
else:
adminuser = ''
f = open(TOKENFILE, 'r')
token = f.readline().rstrip()
f.close()
def debug(msg):
print "Debug -- "
print msg
if token:
print token
tbot = bot.bot(token, adminuser)
if args.w and ssl_public_cert and webhook_url:
print "Webhook mode enabled"
s = bot.httpserver('127.0.0.1', 9090, tbot, ssl_public_cert, webhook_url)
else:
print "Webhook mode disabled"
print "Reseting exists webhook... " + str(tbot.resetWebhook())
from chat import chat
c = chat(tbot)
print "Processing... "
while True:
print '...'
for ret in tbot.get_next_message():
print ret
msg = ret['message']
ret = c.process(msg)
if ret:
tbot.send_answer(ret[0], ret[1])
time.sleep(10)