-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
164 lines (117 loc) · 5.31 KB
/
Copy pathbot.py
File metadata and controls
164 lines (117 loc) · 5.31 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
import requests
import telebot
import json
import time
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton
import re
TELEGRAM_TOKEN = ''
COZE_TOKEN = ''
BOT_ID = ''
bot = telebot.TeleBot(TELEGRAM_TOKEN)
chat_histories = {}
def call_coze_api(user_id, query, chat_history, message=None):
url = 'https://api.coze.com/open_api/v2/chat'
headers = {
'Authorization': f'Bearer {COZE_TOKEN}',
'Content-Type': 'application/json'
}
data = {
'bot_id': BOT_ID,
'user': str(user_id),
'query': query,
'stream': True,
'chat_history': chat_history
}
response = requests.post(url, headers=headers, json=data, stream=True)
if response.status_code != 200:
print(f"Error: {response.status_code}, {response.text}")
return
full_message = ""
previous_message = ""
last_update_time = time.time()
follow_up_questions = []
for line in response.iter_lines():
if line:
try:
decoded_line = line.decode('utf-8')
if decoded_line.startswith("data:"):
decoded_line = decoded_line[5:]
print(f"Decoded Line: {decoded_line}") # logs
json_line = json.loads(decoded_line)
if 'message' in json_line:
message_content = json_line['message']
content = message_content['content']
if message_content['type'] == 'follow_up':
follow_up_questions.append(content)
elif message_content['type'] == 'answer':
if "{" in content and "}" in content:
content = json.loads(content).get('data', content)
full_message += content
current_time = time.time()
if current_time - last_update_time >= 2:
if full_message != previous_message and message:
try:
bot.edit_message_text(chat_id=message.chat.id, message_id=message.message_id,
text=full_message)
previous_message = full_message
last_update_time = current_time
except telebot.apihelper.ApiTelegramException as e:
if e.result.status_code == 429:
retry_after = int(e.result.json()['parameters']['retry_after'])
print(f"Too Many Requests: retry after {retry_after} seconds")
time.sleep(retry_after)
else:
raise e
time.sleep(0.00001)
except json.JSONDecodeError as e:
print(f"JSON Decode Error: {e}")
continue
full_message = apply_markdown(full_message)
markup = create_markup(follow_up_questions)
bot.edit_message_text(chat_id=message.chat.id, message_id=message.message_id, text=full_message,
reply_markup=markup)
return full_message, follow_up_questions
def create_markup(follow_up_questions):
markup = InlineKeyboardMarkup()
for question in follow_up_questions:
try:
callback_data = re.sub(r'\W+', '_', question)[:64]
markup.add(InlineKeyboardButton(question, callback_data=callback_data))
except Exception as e:
print(f"Error creating button for question '{question}': {e}")
continue
return markup
def apply_markdown(text):
text = text.replace('\n', '\n\n')
return text
@bot.message_handler(commands=['start'])
def send_welcome(message):
bot.reply_to(message, "Hi, I'm created by @vt72983")
@bot.message_handler(commands=['clear'])
def clear_history(message):
user_id = message.from_user.id
if user_id in chat_histories:
del chat_histories[user_id]
bot.reply_to(message, "The current dialog has been cleared!")
@bot.message_handler(func=lambda message: True)
def handle_message(message):
user_id = message.from_user.id
query = message.text
if user_id not in chat_histories:
chat_histories[user_id] = []
chat_histories[user_id].append({'role': 'user', 'content': query, 'content_type': 'text'})
sent_message = bot.reply_to(message, "Request processing...")
call_coze_api(user_id, query, chat_histories[user_id], sent_message)
chat_histories[user_id].append({'role': 'assistant', 'content': "Request has been processed.", 'content_type': 'text'})
@bot.callback_query_handler(func=lambda call: True)
def handle_query(call):
user_id = call.from_user.id
query = call.data
if user_id not in chat_histories:
chat_histories[user_id] = []
chat_histories[user_id].append({'role': 'user', 'content': query, 'content_type': 'text'})
sent_message = bot.send_message(call.message.chat.id, "Request processing...")
call_coze_api(user_id, query, chat_histories[user_id], sent_message)
chat_histories[user_id].append({'role': 'assistant', 'content': "Request has been processed.", 'content_type': 'text'})
if __name__ == '__main__':
bot.polling(none_stop=True)