-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrapper.py
110 lines (87 loc) · 3.77 KB
/
wrapper.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
#!/usr/bin/env python
#coding:utf-8
"""
Author: Marco Mescalchin --<>
Purpose: Wrapper to telegram bot api
Created: 06/25/15
"""
import requests,json
from threading import Timer
from django.core.cache import cache
from django.conf import settings
from telegrambot.signals import message_received
DEBUG_GET_POST = False
########################################################################
class Bot:
""""""
api_url = "https://api.telegram.org"
#----------------------------------------------------------------------
def __init__(self,token):
"""Constructor"""
self.token = token
def setWebhook(self):
from django.core.urlresolvers import reverse
whurl = "%s%s" % (settings.TELEGRAM_WEBHOOK_URL,reverse('telegrambot.views.webhook'))
r = self.post('setWebhook',{'url':whurl.replace('http:','https:')})
print("Telegram WebHook Setup: %s" % r)
def get(self,method,params=None):
if DEBUG_GET_POST: print("GET --> %s %s" % (method,params))
r = requests.get("%s/bot%s/%s" % (self.api_url,self.token,method),params,timeout=5)
if DEBUG_GET_POST: print("GET <-- %s" % r)
if r.status_code == requests.codes.ok:
j = r.json()
if j['ok']:
if j['result']:
return j['result']
else:
print("GET Error %s" % r.text)
return False
def post(self,method,params=None,files=None):
if DEBUG_GET_POST: print("POST --> %s %s" % (method,params))
r = requests.post("%s/bot%s/%s" % (self.api_url,self.token,method),params,files=files,timeout=60)
if DEBUG_GET_POST: print("POST <-- %s" % (r))
if r.status_code == requests.codes.ok:
j = r.json()
if j['ok']:
if j['result']:
return j['result']
else:
print("POST Error %s" % r.text)
return False
def webhook(self,request):
data = json.loads(request.body.decode('utf8'))
print("<-- WH %s" % data['message'])
message_received.send(self,message=data['message'])
return 'ok'
def action_typing(self,chat_id):
self.post('sendChatAction',{'chat_id':chat_id,'action':'typing'})
def action_upload_photo(self,chat_id):
self.post('sendChatAction',{'chat_id':chat_id,'action':'upload_photo'})
def sendMessage(self,chat_id,text,disable_web_page_preview=True,reply_to_message_id=None,reply_markup=None):
r = self.post('sendMessage',{
'chat_id':chat_id,
'text':text,
'disable_web_page_preview':disable_web_page_preview,
'reply_to_message_id':reply_to_message_id,
'reply_markup':json.dumps(reply_markup)
})
#TODO: check result
def sendPhoto(self,chat_id,photo,caption=None,reply_to_message_id=None,reply_markup=None):
r = self.post('sendPhoto',{
'chat_id':chat_id,
'caption':caption,
'reply_to_message_id':reply_to_message_id,
'reply_markup':json.dumps(reply_markup)
},files={'photo':('image.jpg', photo, 'image/jpeg', {'Expires': '0'})})
#TODO: check result
def getUpdates(self):
updates = self.get('getUpdates',{'offset':cache.get('tgbot-update-id')})
try:
if updates:
cache.set('tgbot-update-id',updates[-1]['update_id'] + 1)
for update in updates:
message_received.send(sender=self, message=update['message'])
except Exception as e:
print("Getupdates Error %s" % e)
finally:
Timer(10,self.getUpdates).start()