-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathbing.py
235 lines (204 loc) · 10.3 KB
/
bing.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
import asyncio
import os
import re
import telebot
from EdgeGPT import Chatbot, ConversationStyle
from telebot.util import quick_markup
BOT_TOKEN = os.getenv('BOT_TOKEN')
ALLOWED_USER_IDS = os.getenv('ALLOWED_USER_IDS').split(',')
BOT_ID = os.getenv('BOT_ID', '')
COOKIE_PATH = os.getenv('COOKIE_PATH', './cookie.json')
GROUP_MODE = os.getenv('GROUP_MODE', 'False')
PUBLIC_MODE = os.getenv('PUBLIC_MODE', 'False')
print("\033[1;33mThe startup is successful, the configuration is as follows : ")
print("BOT_TOKEN: " + BOT_TOKEN)
print("BOT_ID: " + BOT_ID)
print("ALLOWED_USER_IDS: ")
print(ALLOWED_USER_IDS)
print("COOKIE_PATH: " + COOKIE_PATH)
print("GROUP_MODE: " + GROUP_MODE)
print("PUBLIC_MODE: " + PUBLIC_MODE + '\033[1;33m')
bot = telebot.TeleBot(BOT_TOKEN)
EDGES = {}
not_allow_info = '⚠️You are not authorized to use this bot⚠️'
markup = quick_markup({
'Github': {'url': 'https://github.com/pininkara/BingChatBot'},
}, row_width=1)
my_conversation_style = ConversationStyle.balanced
@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
if is_allowed(message) or PUBLIC_MODE == "True" or message.chat.type == "group":
bot.reply_to(
message, "Bing Chat Bot By pininkara~\n/help - Show help message\n/reset - Reset conversation\n/switch - "
"Switch conversation style (creative,balanced,precise)\n", reply_markup=markup)
else:
bot.reply_to(message, not_allow_info)
@bot.message_handler(commands=['reset'])
def send_reset(message):
if is_allowed(message) or PUBLIC_MODE == "True" or message.chat.type == "group":
try:
if message.from_user.id not in EDGES:
EDGES[message.from_user.id] = Chatbot(cookie_path=COOKIE_PATH)
asyncio.run(EDGES[message.from_user.id].reset())
except Exception as e:
print("\033[31mError: ", e)
bot.reply_to(message, "Error : " + str(e), parse_mode='Markdown')
else:
bot.reply_to(message, "Reset successful🎉")
else:
bot.reply_to(message, not_allow_info)
@bot.message_handler(commands=['switch'])
def switch_style(message):
if is_allowed(message):
message_list = message.text.split(" ")
if len(message_list) > 1:
if message_list[1] == "creative":
bot.reply_to(
message,
"Switch successful , current style is creative")
elif message_list[1] == "balanced":
conversation_style = ConversationStyle.balanced
bot.reply_to(
message,
"Switch successful , current style is balanced")
elif message_list[1] == "precise":
conversation_style = ConversationStyle.precise
bot.reply_to(
message,
"Switch successful , current style is precise")
else:
bot.reply_to(
message,
"Parameter error , please choose one of (creative,balanced,precise)\n(e.g./switch balanced")
else:
bot.reply_to(
message, "Parameter error , please choose one of (creative,balanced,precise)\n(e.g./switch balanced")
else:
bot.reply_to(
message, '⚠️You are not authorized to switch the conversation style⚠')
@bot.message_handler(func=lambda msg: True)
def response_all(message):
print('\033[0;32mMessage: ' + message.text)
print(
'From: ', message.from_user.first_name, message.from_user.last_name, ' @', message.from_user.username)
message_text = ''
is_reply = message.reply_to_message is not None and '@' + \
message.reply_to_message.from_user.username == BOT_ID
if message.chat.type == "private" or GROUP_MODE == "True" or message.text.startswith(BOT_ID) or is_reply:
if is_allowed(message) or PUBLIC_MODE == "True" or message.chat.type == "group":
if message.text.startswith(BOT_ID) and BOT_ID != '':
message_text = message.text[len(BOT_ID):]
else:
message_text = message.text
response_list = asyncio.run(bing_chat(message_text, message))
print("\033[1;34mResponse: " + response_list[0])
if len(response_list[0]) > 4095:
for x in range(0, len(response_list[0]), 4095):
bot.reply_to(
message, response_list[0][x:x + 4095], parse_mode='Markdown', reply_markup=response_list[1])
else:
bot.reply_to(
message, response_list[0], parse_mode='Markdown', reply_markup=response_list[1])
else:
bot.reply_to(message, not_allow_info)
else:
print('\033[0;36mUntargeted message, ignored\033[0;36m')
@bot.callback_query_handler(func=lambda msg: True)
def callback_all(callback_query):
print("\033[0;32mCallbackQuery: " + callback_query.data)
print(
'From: ', callback_query.from_user.first_name, callback_query.from_user.last_name, '@',
callback_query.from_user.username)
try:
response_list = asyncio.run(
bing_chat(callback_query.data, callback_query))
except Exception as e:
print("\033[31mError: ", e)
bot.reply_to(callback_query.message, "Error : " +
str(e), parse_mode='Markdown')
else:
print("\033[1;34mResponse: " + response_list[0] + '\033[1;34m')
if len(response_list[0]) > 4095:
for x in range(0, len(response_list[0]), 4095):
bot.reply_to(
callback_query.message, response_list[0][x:x +
4095], parse_mode='Markdown',
reply_markup=response_list[1])
else:
bot.reply_to(
callback_query.message, response_list[0], parse_mode='Markdown', reply_markup=response_list[1])
async def bing_chat(message_text, message):
if message.from_user.id not in EDGES:
EDGES[message.from_user.id] = Chatbot(cookie_path=COOKIE_PATH)
response_dict = await EDGES[message.from_user.id].ask(prompt=message_text,
conversation_style=my_conversation_style)
if 'text' in response_dict['item']['messages'][1]:
response = re.sub(r'\[\^\d\^]', '',
response_dict['item']['messages'][1]['text'])
else:
response = "Something wrong. Please reset chat"
if 'suggestedResponses' in response_dict['item']['messages'][1]:
suggested_responses0 = re.sub(
r'\[\^\d\^]', '', response_dict['item']['messages'][1]['suggestedResponses'][0]['text'])
suggested_responses1 = re.sub(
r'\[\^\d\^]', '', response_dict['item']['messages'][1]['suggestedResponses'][1]['text'])
suggested_responses2 = re.sub(
r'\[\^\d\^]', '', response_dict['item']['messages'][1]['suggestedResponses'][2]['text'])
markup = quick_markup({
suggested_responses0: {
'callback_data': suggested_responses0.encode('utf-8')[:64].decode('utf-8', 'ignore')},
suggested_responses1: {
'callback_data': suggested_responses1.encode('utf-8')[:64].decode('utf-8', 'ignore')},
suggested_responses2: {'callback_data': suggested_responses2.encode(
'utf-8')[:64].decode('utf-8', 'ignore')}
}, row_width=1)
else:
markup = quick_markup({
'No Suggested Responses': {'url': 'https://bing.com/chat'}
}, row_width=1)
if 'maxNumUserMessagesInConversation' in response_dict['item']['throttling'] and 'numUserMessagesInConversation' in \
response_dict['item']['throttling']:
max_num_user_messages_in_conversation = response_dict['item'][
'throttling']['maxNumUserMessagesInConversation']
num_user_messages_in_conversation = response_dict[
'item']['throttling']['numUserMessagesInConversation']
response = response + "\n----------\n"
response = response + "Messages In Conversation : %d / %d" % (
num_user_messages_in_conversation, max_num_user_messages_in_conversation)
if num_user_messages_in_conversation >= max_num_user_messages_in_conversation:
await EDGES[message.from_user.id].reset()
response = response + "\nAutomatic reset succeeded🎉"
if len(response_dict['item']['messages'][1]['sourceAttributions']) >= 3:
provider_display_name0 = re.sub(
r'\[\^\d\^]', '', response_dict['item']['messages'][1]['sourceAttributions'][0]['providerDisplayName'])
see_more_url0 = re.sub(
r'\[\^\d\^]', '', response_dict['item']['messages'][1]['sourceAttributions'][0]['seeMoreUrl'])
provider_display_name1 = re.sub(
r'\[\^\d\^]', '', response_dict['item']['messages'][1]['sourceAttributions'][1]['providerDisplayName'])
see_more_url1 = re.sub(
r'\[\^\d\^]', '', response_dict['item']['messages'][1]['sourceAttributions'][1]['seeMoreUrl'])
provider_display_name2 = re.sub(
r'\[\^\d\^]', '', response_dict['item']['messages'][1]['sourceAttributions'][2]['providerDisplayName'])
see_more_url2 = re.sub(
r'\[\^\d\^]', '', response_dict['item']['messages'][1]['sourceAttributions'][2]['seeMoreUrl'])
response = response + "\n----------\nReference:\n"
response = response + \
"1.[%s](%s)\n" % (provider_display_name0, see_more_url0)
response = response + \
"2.[%s](%s)\n" % (provider_display_name1, see_more_url1)
response = response + \
"3.[%s](%s)\n" % (provider_display_name2, see_more_url2)
markup = quick_markup({
suggested_responses0: {'callback_data': suggested_responses0.encode('utf-8')[:64].decode('utf-8', 'ignore')},
suggested_responses1: {'callback_data': suggested_responses1.encode('utf-8')[:64].decode('utf-8', 'ignore')},
suggested_responses2: {'callback_data': suggested_responses2.encode(
'utf-8')[:64].decode('utf-8', 'ignore')}
}, row_width=1)
response_list = [response, markup]
return response_list
def is_allowed(message) -> bool:
# Check if user is allowed
if str(message.from_user.id) in ALLOWED_USER_IDS:
return True
return False
bot.infinity_polling()