-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
136 lines (122 loc) · 6.15 KB
/
main.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
import asyncio
import os
import re
import telebot
from EdgeGPT import Chatbot, ConversationStyle
from telebot.util import quick_markup
TOKEN = ""
COOKIE_PATH = 'cookie.json'
bot = telebot.TeleBot(TOKEN)
EDGES = {}
my_conversation_style = ConversationStyle.balanced
@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
bot.reply_to(
message, "Введи /help для показа информации\nДля смены стилей вводи /switch и следующую приписку: \ncreative (Креативный)\nbalanced (Баланс)\nprecise (Строгий)")
@bot.message_handler(commands=['reset'])
def send_reset(message):
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:
bot.reply_to(message, "Ошибка: " + str(e), parse_mode='Markdown')
else:
bot.reply_to(message, "Очищено успешно!")
@bot.message_handler(commands=['switch'])
def switch_style(message):
message_list = message.text.split(" ")
if len(message_list) > 1:
styles = {
"creative": ConversationStyle.creative,
"balanced": ConversationStyle.balanced,
"precise": ConversationStyle.precise
}
if message_list[1] in styles:
global my_conversation_style
my_conversation_style = styles[message_list[1]]
bot.reply_to(
message, f"Текущий стиль: {message_list[1].capitalize()}")
else:
bot.reply_to(
message, "Выберите один из параметров! (/help)")
else:
bot.reply_to(
message, "Выберите один из параметров! (/help)")
@bot.message_handler(func=lambda msg: True)
def response_all(message):
if message.text.startswith('/') and message.text[1:] not in ['start', 'help', 'reset', 'switch']:
bot.reply_to(message, "Нет такой команды")
return
message_text = ''
if message.chat.type == "private":
message_text = message.text
bot.reply_to(message, "Обработка запроса, ожидайте!")
response_list = asyncio.run(bing_chat(message_text, message))
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])
@bot.callback_query_handler(func=lambda msg: True)
def callback_all(callback_query):
try:
response_list = asyncio.run(
bing_chat(callback_query.data, callback_query))
except Exception as e:
bot.reply_to(callback_query.message, "Ошибка: " +
str(e), parse_mode='Markdown')
else:
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 = "Что-то не так. Пожалуйста, перезагрузите чат"
if 'suggestedResponses' in response_dict['item']['messages'][1]:
suggested_responses = response_dict['item']['messages'][1]['suggestedResponses']
markup = quick_markup({
re.sub(r'\[\^\d\^]', '', suggested_responses[i]['text']): {
'callback_data': suggested_responses[i]['text'].encode('utf-8')[:64].decode('utf-8', 'ignore')}
for i in range(min(len(suggested_responses), 3))
}, row_width=1)
else:
markup = quick_markup({
'Предложенных ответов нет': {'url': 'https://bing.com/chat'}
}, row_width=1)
throttling = response_dict['item']['throttling']
if 'maxNumUserMessagesInConversation' in throttling and 'numUserMessagesInConversation' in throttling:
max_num_user_messages_in_conversation = throttling['maxNumUserMessagesInConversation']
num_user_messages_in_conversation = throttling['numUserMessagesInConversation']
response += "\n———————\n"
response += f"Контекст: {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 += "\nКонтекст был автоматически очищен"
attributions = response_dict['item']['messages'][1]['sourceAttributions']
if len(attributions) >= 3:
response += "\n———————\nИсточники:\n"
for i in range(3):
provider_display_name = re.sub(
r'\[\^\d\^]', '', attributions[i]['providerDisplayName'])
see_more_url = re.sub(
r'\[\^\d\^]', '', attributions[i]['seeMoreUrl'])
response += f"{i + 1}.[{provider_display_name}]({see_more_url})\n"
response_list = [response, markup]
return response_list
bot.polling()