forked from szamani20/Shora-Telegram-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShora.py
253 lines (212 loc) · 10.2 KB
/
Shora.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import time
import telepot
import logging
from telepot.namedtuple import ForceReply
from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton
from settings import *
from shora_api import *
message_with_inline_keyboard = None
live_users = []
live_requests = []
logger = logging.getLogger(__name__)
class Chat:
def __init__(self, id, username=None, first_name=None, last_name=None):
self.id = id
self.username = username
self.first_name = first_name
self.last_name = last_name
class Subject:
def __init__(self):
self.item = ''
self.place = ''
self.more = ''
self.type = 0 # 1 ----> tasisat, 2 ----> lost
def __str__(self):
return '%s %s %s %s' % (
self.item,
self.place,
self.more,
'تأسیسات' if self.type == 1 else 'گمشده'
)
class Request:
def __init__(self, chat, subject):
self.chat = chat
self.subject = subject
def on_chat_message(msg):
# print('msg: ', msg)
content_type, chat_type, chat_id = telepot.glance(msg)
if content_type != 'text':
bot.sendMessage(chat_id, 'آر یو کیدینگ می؟')
return None
# print('Chat:', content_type, chat_type, chat_id)
command = msg['text']
# print(' Live Users: ', live_users, '\n', 'Live Requests: ', live_requests, '\n')
if content_type != 'text':
return None
if command == '/show':
markup = InlineKeyboardMarkup(inline_keyboard=[
[dict(text='سایت شورای صنفی', url='http://shora.ce.sharif.edu/')],
[InlineKeyboardButton(text='تاسیسات', callback_data='tasisat')],
[InlineKeyboardButton(text='گمشدهها', callback_data='lost')],
])
global message_with_inline_keyboard
message_with_inline_keyboard = bot.sendMessage(chat_id, 'منو',
reply_markup=markup)
return None
if command == '/done':
working_request = None
working_request_index = -1
for req in live_requests:
if req.chat.id == chat_id:
working_request = req
working_request_index = live_requests.index(working_request)
break
if working_request_index == -1:
# bot.sendMessage(chat_id, 'چیزی نگفتی که هنوز 🤔')
return None
if working_request.subject.item == '' or working_request.subject.place == '':
bot.sendMessage(chat_id, 'مورد یا مکان رو هنوز مشخص نکردی 😁')
return None
api_success, api_message = shora_api.send_message(ShoraMessage(working_request.subject.item,
working_request.subject.place,
working_request.subject.more))
# commit new request
live_requests.pop(working_request_index)
live_users.remove(chat_id)
bot.sendMessage(chat_id, 'مسالهٔ موردنظر شما ثبت شد' + '\n' +
'آیتم: ' + working_request.subject.item + '\n' +
'مکان: ' + working_request.subject.place + '\n' +
'توضیحات: ' + working_request.subject.more + '\n')
if api_success:
logger.info("New issue submitted " + working_request.subject)
else:
logger.error("Issue could not be submitted " + working_request.subject + " with error message: " + api_message)
if working_request.subject.type == 1:
bot.sendMessage(chat_id,
'ما پیگیر مسالهٔ ثبت شده شما هستیم و آن را در لیست کارهای بخش تاسیسات قرار خواهیم داد.')
elif working_request.subject.type == 2:
bot.sendMessage(chat_id, 'امیدواریم وسیله گم شده ی خود را هر چه زودتر پیدا کنید.')
return None
if command == '/cancel':
if chat_id not in live_users:
bot.sendMessage(chat_id, 'هنوز چیزی نگفتی 🤔')
return None
if chat_id in live_users:
live_users.remove(chat_id)
for req in live_requests:
if req.chat.id == chat_id:
live_requests.remove(req)
bot.sendMessage(chat_id, 'حلله ✋🏻')
break
return None
if command == '/content':
# print('content')
if chat_id not in live_users:
bot.sendMessage(chat_id, 'هنوز چیزی نگفتی 🤔')
return None
for req in live_requests:
if req.chat.id == chat_id:
bot.sendMessage(chat_id, 'متن تا الان: ' + '\n' +
'آیتم: ' + req.subject.item + '\n' +
'مکان: ' + req.subject.place + '\n' +
'توضیحات: ' + req.subject.more + '\n')
return None
chat = Chat(**msg['from'])
chat_id = chat.id
text = msg['text']
if chat_id in live_users:
# print('Live User')
working_request = None
working_request_index = -1
for req in live_requests:
if req.chat.id == chat_id:
working_request = req
working_request_index = live_requests.index(working_request)
break
if working_request is None:
# print('working_request id None')
working_request = Request(chat, Subject())
live_requests.append(working_request)
working_request_index = len(live_requests) - 1
working_subject = working_request.subject
# a little hard coded, i know
if 'reply_to_message' in msg:
if 'گزارش' in msg['reply_to_message']['text']:
working_subject.type = 1
elif 'گم' in msg['reply_to_message']['text']:
working_subject.type = 2
if working_subject.item == '':
# print('If 1')
working_subject.item = text
working_request.subject = working_subject
live_requests[working_request_index] = working_request
bot.sendMessage(chat_id, 'لطفا محل موردنظر را بفرمایید', reply_markup=ForceReply())
elif working_subject.place == '':
# print('If 2')
working_subject.place = text
working_request.subject = working_subject
live_requests[working_request_index] = working_request
bot.sendMessage(chat_id, 'توضیحات بیشتر (اگر نیاز به توضیح ندارد /done را بزنید)',
reply_markup=ForceReply())
elif working_subject.more == '':
# print('If 3')
working_subject.more = text
working_request.subject = working_subject
live_requests.pop(working_request_index)
live_users.remove(chat_id)
api_success, api_message = shora_api.send_message(ShoraMessage(working_subject.item,
working_subject.place,
working_subject.more))
if api_success:
logger.info("New issue submitted " + working_request.subject)
else:
logger.error("Issue could not be submitted " + working_request.subject + " with error message: " + api_message)
# commit new request
bot.sendMessage(chat_id, 'مسالهٔ موردنظر شما ثبت شد' + '\n' +
'آیتم: ' + working_subject.item + '\n' +
'مکان: ' + working_subject.place + '\n' +
'توضیحات: ' + working_subject.more)
if working_subject.type == 1:
bot.sendMessage(chat_id,
'ما پیگیر مسالهٔ ثبت شده شما هستیم و آن را در لیست کارهای بخش تاسیسات قرار خواهیم داد.')
elif working_subject.type == 2:
bot.sendMessage(chat_id, 'امیدواریم وسیله گم شده ی خود را هر چه زودتر پیدا کنید.')
else:
markup = InlineKeyboardMarkup(inline_keyboard=[
[dict(text='سایت شورا صنفی', url='http://shora.ce.sharif.edu/')],
[InlineKeyboardButton(text='تاسیسات', callback_data='tasisat')],
[InlineKeyboardButton(text='گمشده ها', callback_data='lost')],
])
global message_with_inline_keyboard
message_with_inline_keyboard = bot.sendMessage(chat_id, 'منو',
reply_markup=markup)
def on_edited_chat_message(msg):
content_type, chat_type, chat_id = telepot.glance(msg, flavor='edited_chat')
bot.sendMessage(chat_id, 'ادیت نکن دیگه🙈', reply_to_message_id=msg['message_id'])
def on_callback_query(msg):
query_id, from_id, data = telepot.glance(msg, flavor='callback_query')
# print('Callback query:', query_id, from_id, data)
if data == 'tasisat':
# print('Tasisat callback')
if from_id in live_users:
bot.sendMessage(from_id, 'یدونه یدونه!')
return None
live_users.append(from_id)
bot.sendMessage(from_id, 'لطفا عنوان گزارش یا پیشنهاد خود را بفرمایید', reply_markup=ForceReply())
elif data == 'lost':
# print('lost callback')
if from_id in live_users:
bot.sendMessage(from_id, 'یدونه یدونه!')
return None
live_users.append(from_id)
bot.sendMessage(from_id, 'لطفا عنوان وسیله پیداشده یا گم شده را بفرمایید', reply_markup=ForceReply())
# almost one to go
shora_api = ShoraAPI(SHORA_CALLBACK_URL, SIGNING_SECRET)
bot = telepot.Bot(TOKEN)
answerer = telepot.helper.Answerer(bot)
bot.message_loop({'chat': on_chat_message,
'edited_chat': on_edited_chat_message,
'callback_query': on_callback_query})
print('Listening ...')
while 1:
time.sleep(SLEEP_TIME)