-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
188 lines (141 loc) · 5.96 KB
/
main.py
File metadata and controls
188 lines (141 loc) · 5.96 KB
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
# -*- coding: utf-8 -*-
import StringIO
import json
import logging
import random
import urllib
import urllib2
import multipart
# стандартные модули для Google Engine
from google.appengine.api import urlfetch
from google.appengine.ext import ndb
import webapp2
# Две следующие строчки необходимы для иморта сторонних библиотек из папки libs
#import sys
#sys.path.insert(0, 'libs')
# Импорт сторонних модулей
# Например
# import vkontakte
#Для удобства все команды можно вывести в словарь
messages = {
'start_message': ('Мой список команд:\n'
+'/about - информация о боте\n'
+'/stop - остановить бот\n'),
'stop_message':('Пока!\n'
+'/start - запустить бот'),
'about_message': ("Информация о боте\n"
+"Автор: \n"
+"Версия: \n"),
}
TOKEN = 'ВСТАВЬТЕ ТОКЕН ВАШЕГО БОТА'
BASE_URL = 'https://api.telegram.org/bot' + TOKEN + '/'
# ================================
class EnableStatus(ndb.Model):
# key name: str(chat_id)
enabled = ndb.BooleanProperty(indexed=False, default=False)
# ================================
def setEnabled(chat_id, yes):
es = EnableStatus.get_or_insert(str(chat_id))
es.enabled = yes
es.put()
def getEnabled(chat_id):
es = EnableStatus.get_by_id(str(chat_id))
if es:
return es.enabled
return False
# ================================
class MeHandler(webapp2.RequestHandler):
def get(self):
urlfetch.set_default_fetch_deadline(60)
self.response.write(json.dumps(json.load(urllib2.urlopen(BASE_URL + 'getMe'))))
class GetUpdatesHandler(webapp2.RequestHandler):
def get(self):
urlfetch.set_default_fetch_deadline(60)
self.response.write(json.dumps(json.load(urllib2.urlopen(BASE_URL + 'getUpdates'))))
class SetWebhookHandler(webapp2.RequestHandler):
def get(self):
urlfetch.set_default_fetch_deadline(60)
url = self.request.get('url')
if url:
self.response.write(json.dumps(json.load(urllib2.urlopen(BASE_URL + 'setWebhook', urllib.urlencode({'url': url})))))
class WebhookHandler(webapp2.RequestHandler):
def post(self):
urlfetch.set_default_fetch_deadline(60)
body = json.loads(self.request.body)
logging.info('request body:')
logging.info(body)
self.response.write(json.dumps(body))
update_id = body['update_id']
message = body['message']
message_id = message.get('message_id')
date = message.get('date')
text = message.get('text')
fr = message.get('from')
chat = message['chat']
chat_id = chat['id']
if not text:
logging.info('no text')
return
def reply(msg=None, img=None):
if msg:
resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
'chat_id': str(chat_id),
'text': msg.encode('utf-8'),
'disable_web_page_preview': 'true',
'reply_to_message_id': str(message_id),
})).read()
elif img:
resp = multipart.post_multipart(BASE_URL + 'sendPhoto', [
('chat_id', str(chat_id)),
('reply_to_message_id', str(message_id)),
], [
('photo', 'image.jpg', img),
])
else:
logging.error('no msg or img specified')
resp = None
logging.info('send response:')
logging.info(resp)
############## ОТСЮДА МОЖНО НАЧИНАТЬ КАСТОМИЗИРОВАТЬ ВАШ БОТ ##############
if text.startswith('/'):
#СПИСОК КОМАНД ДЛЯ БОТА, НАЧИНАЮЩИХСЯ С /
if text == '/start':
reply(messages['start_message'].decode('utf-8'))
setEnabled(chat_id, True)
elif text == '/stop':
mes = 'Пока!\n/start - запустить бот'
reply(mes.decode('utf-8'))
setEnabled(chat_id, False)
elif text == '/about':
reply(messages['about_message'].decode('utf-8'))
else:
mes = 'Не уверен, что понимаю, о чем ты...'
reply(mes.decode('utf-8'))
# Другие команды начинающиеся не с /
elif 'who are you' in text:
reply('telegram bot, created by Alexander Osipenko')
elif 'what time' in text:
reply('look at the top-right corner of your screen!')
################################################################################
else:
if getEnabled(chat_id):
try:
resp1 = json.load(urllib2.urlopen('http://www.simsimi.com/requestChat?lc=en&ft=1.0&req=' + urllib.quote_plus(text.encode('utf-8'))))
back = resp1.get('res')
except urllib2.HTTPError, err:
logging.error(err)
back = str(err)
if not back:
reply('okay...')
elif 'I HAVE NO RESPONSE' in back:
reply('you said something with no meaning')
else:
reply(back)
else:
logging.info('not enabled for chat_id {}'.format(chat_id))
app = webapp2.WSGIApplication([
('/me', MeHandler),
('/updates', GetUpdatesHandler),
('/set_webhook', SetWebhookHandler),
('/webhook', WebhookHandler),
], debug=True)