-
Notifications
You must be signed in to change notification settings - Fork 5
/
ChatBot.py
261 lines (208 loc) · 7.08 KB
/
ChatBot.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
254
255
256
257
258
259
260
261
import telebot
from telebot import types
from config import CHANNEL, GROUP, OWNER, TOKEN
from dataEgine import *
from Messages import *
access_token = TOKEN
bot = telebot.TeleBot(access_token)
def inline_menu():
"""
Create inline menu for new chat
:return: InlineKeyboardMarkup
"""
callback = types.InlineKeyboardButton(
text="\U00002709 New chat", callback_data="NewChat"
)
kenkan = types.InlineKeyboardButton(text="🔵 ᴏᴡɴᴇʀ", url=f"t.me/{OWNER}")
group = types.InlineKeyboardButton(text="👥 ɢʀᴏᴜᴘ", url=f"https://t.me/{GROUP}")
channel = types.InlineKeyboardButton(
text="ᴄʜᴀɴɴᴇʟ 📣", url=f"https://t.me/{CHANNEL}"
)
menu = types.InlineKeyboardMarkup()
menu.add(kenkan, channel, group, callback)
return menu
def generate_markup():
"""
Create menu with two buttons: 'Like' and 'Dislike'
:return: ReplyKeyboardMarkup
"""
markup = types.ReplyKeyboardMarkup(one_time_keyboard=False, resize_keyboard=True)
markup.add(like_str)
markup.add(dislike_str)
return markup
def connect_user(user_id):
"""
:param user_id: Chat id with user
:return: boolean
"""
if user_id in communications:
return True
else:
bot.send_message(user_id, m_has_not_dialog)
return False
@bot.message_handler(commands=["start"])
def echo(message):
"""
Make the user in Data Base.
:param message:
:return:
"""
message.chat.type = "private"
user_id = message.chat.id
if message.chat.username is None:
bot.send_message(user_id, m_is_not_user_name)
return
menu = inline_menu()
bot.send_message(user_id, m_start, reply_markup=menu)
@bot.message_handler(commands=["stop"])
def echo(message):
"""
This function remove user from Data Base and sends a farewell message.
:param message:
:return:
"""
menu = types.ReplyKeyboardRemove()
user_id = message.chat.id
if message.chat.id in communications:
bot.send_message(
communications[user_id]["UserTo"], m_disconnect_user, reply_markup=menu
)
tmp_id = communications[user_id]["UserTo"]
delete_info(tmp_id)
delete_user_from_db(user_id)
bot.send_message(user_id, m_good_bye)
@bot.message_handler(
func=lambda call: call.text == like_str or call.text == dislike_str
)
def echo(message):
"""
This function reacts to pressing buttons: 'Like' and 'Dislike'
If both users press 'Like', then bot sends them username from telegram.
If somebody press 'Dislike', then chat finish.
:param message:
:return:
"""
user_id = message.chat.id
if user_id not in communications:
bot.send_message(user_id, m_failed, reply_markup=types.ReplyKeyboardRemove())
return
user_to_id = communications[user_id]["UserTo"]
flag = False
if message.text == dislike_str:
bot.send_message(
user_id, m_dislike_user, reply_markup=types.ReplyKeyboardRemove()
)
bot.send_message(
user_to_id, m_dislike_user_to, reply_markup=types.ReplyKeyboardRemove()
)
flag = True
else:
bot.send_message(user_id, m_like, reply_markup=types.ReplyKeyboardRemove())
update_user_like(user_to_id)
if communications[user_id]["like"]:
bot.send_message(user_id, m_all_like(communications[user_id]["UserName"]))
bot.send_message(
user_to_id, m_all_like(communications[user_to_id]["UserName"])
)
flag = True
if flag:
delete_info(user_to_id)
menu = inline_menu()
bot.send_message(user_id, m_play_again, reply_markup=menu)
bot.send_message(user_to_id, m_play_again, reply_markup=menu)
@bot.message_handler(
content_types=["text", "sticker", "video", "photo", "audio", "voice"]
)
def echo(message):
"""
Resend message to anonymous friend.
:param message:
:return:
"""
user_id = message.chat.id
if message.content_type == "sticker":
if not connect_user(user_id):
return
bot.send_sticker(communications[user_id]["UserTo"], message.sticker.file_id)
elif message.content_type == "photo":
if not connect_user(user_id):
return
file_id = None
for item in message.photo:
file_id = item.file_id
bot.send_photo(
communications[user_id]["UserTo"], file_id, caption=message.caption
)
elif message.content_type == "audio":
if not connect_user(user_id):
return
bot.send_audio(
communications[user_id]["UserTo"],
message.audio.file_id,
caption=message.caption,
)
elif message.content_type == "video":
if not connect_user(user_id):
return
bot.send_video(
communications[user_id]["UserTo"],
message.video.file_id,
caption=message.caption,
)
elif message.content_type == "voice":
if not connect_user(user_id):
return
bot.send_voice(communications[user_id]["UserTo"], message.voice.file_id)
elif message.content_type == "text":
if (
message.text != "/start"
and message.text != "/stop"
and message.text != dislike_str
and message.text != like_str
and message.text != "NewChat"
):
if not connect_user(user_id):
return
if message.reply_to_message is None:
bot.send_message(communications[user_id]["UserTo"], message.text)
elif message.from_user.id != message.reply_to_message.from_user.id:
bot.send_message(
communications[user_id]["UserTo"],
message.text,
reply_to_message_id=message.reply_to_message.message_id - 1,
)
else:
bot.send_message(user_id, m_send_some_messages)
@bot.callback_query_handler(func=lambda call: True)
def echo(call):
"""
Create new chat.
All users are divided into two categories: receivers and emitters.
If bot finds pair, then it creates new chat.
:param call:
:return:
"""
if call.data == "NewChat":
user_id = call.message.chat.id
user_to_id = None
add_users(chat=call.message.chat)
if len(free_users) < 2:
bot.send_message(user_id, m_is_not_free_users)
return
if free_users[user_id]["state"] == 0:
return
for user in free_users:
if user["state"] == 0:
user_to_id = user["ID"]
break
if user_to_id is None:
bot.send_message(user_id, m_is_not_free_users)
return
keyboard = generate_markup()
add_communications(user_id, user_to_id)
bot.send_message(user_id, m_is_connect, reply_markup=keyboard)
bot.send_message(user_to_id, m_is_connect, reply_markup=keyboard)
if __name__ == "__main__":
recovery_data()
bot.stop_polling()
bot.polling(none_stop=True)