I created a telegram bot in python, Used py-telegram-bot-api and telebot libraries, after launch it gives error. I also used different APIs, the result is the same The main purpose of the code is to translate text from ChatGpt to telegram
code:
import os
print('Bot started')
NUMBERS_ROWS = 2
import openai
import telebot
openai.api_key = ""
bot = telebot.TeleBot('')
if not os.path.exists("users"):
os.mkdir("users")
@bot.message_handler(content_types=['text'])
def msg(message):
if f"{message.chat.id}.txt" not in os.listdir('users'):
with open(f"users/{message.chat.id}.txt", "x") as f:
f.write('')
with open(f'users/{message.chat.id}.txt', 'r', encoding='utf-8') as file:
oldmes = file.read()
if message.text == '/clear':
with open(f'users/{message.chat.id}.txt', 'w', encoding='utf-8') as file:
file.write('')
return bot.send_message(chat_id=message.chat.id, text='История очищена!')
try:
send_message = bot.send_message(chat_id=message.chat.id, text='Обрабатываю запрос, пожалуйста подождите!')
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0301",
messages=[{"role": "user", "content": oldmes},
{"role": "user","content": f'Предыдущие сообщения: {oldmes}; Запрос: {message.text}'}], presence_penalty=0.6)
bot.edit_message_text(text=completion.choices[0].message["content"], chat_id=message.chat.id, message_id=send_message.message_id)
with open(f'users/{message.chat.id}.txt', 'a+', encoding='utf-8') as file:
file.write(message.text.replace('\n', ' ') + '\n' + completion.choices[0].message["content"].replace('\n', ' ') + '\n')
with open(f'users/{message.chat.id}.txt', 'r', encoding='utf-8') as f:
lines = f.readlines()
if len(lines) >= NUMBERS_ROWS +1:
with open(f'users/{message.chat.id}.txt', 'w', encoding='utf-8') as f:
f.writelines(lines[2:])
if len(lines) > 4095:
for x in range(0, len(lines), 4095):
bot.reply_to(message, text=lines[x:x+4095])
else:
bot.reply_to(message, text=lines)
except Exception as e:
bot.send_message(chat_id=message.chat.id, text=e)
bot.infinity_polling()
After launch it gives the following error:ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ Error:
<!--[if gte IE 10]><!-->
<script>
if (!navigator.cookieEnabled) {
window.addEventListener('DOMContentLoaded', function () {
var cookieEl = document.getElementById('cookie-alert');
cookieEl.style.display = 'block';
})
}
</script>
<!--<![endif]-->
</head>
<body>
<div id="cf-wrapper">
<div class="cf-alert cf-alert-error cf-cookie-error" id="cookie-alert" data-translate="enable_cookies">Please enable cookies.</div>
<div id="cf-error-details" class="cf-error-details-wrapper">
<div class="cf-wrapper cf-header cf-error-overview">
<h1 data-translate="block_headline">Sorry, you have been blocked</h1>
<h2 class="cf-subheadline"><span data-translate="unable_to_access">You are unable to access</span> api.openai.com</h2>
</div><!-- /.header -->
<div class="cf-section cf-highlight">
<div class="cf-wrapper">
<div class="cf-screenshot-container cf-screenshot-full">
<span class="cf-no-screenshot error"></span>
</div>
</div>
</div><!-- /.captcha-container -->
<div class="cf-section cf-wrapper">
<div class="cf-columns two">
<div class="cf-column">
<h2 data-translate="blocked_why_headline">Why have I been blocked?</h2>
<p data-translate="blocked_why_detail">This website is using a security service to protect itself from online attacks. The action you just performed triggered the security solution. There are
several actions that could trigger this block including submitting a certain word or phrase, a SQL command or malformed data.</p>
</div>
<div class="cf-column">
<h2 data-translate="blocked_resolve_headline">What can I do to resolve this?</h2>
<p data-translate="blocked_resolve_detail">You can email the site owner to let them know you were blocked. Please include what you were doing when this page came up and the Cloudflare Ray ID
found at the bottom of this page.</p>
</div>
</div>
</div><!-- /.section -->
<div class="cf-error-footer cf-wrapper w-240 lg:w-full py-10 sm:py-4 sm:px-8 mx-auto text-center sm:text-left border-solid border-0 border-t border-gray-300">
<p class="text-13">
return _make_request(token, method_url, params=payload, method='post')
File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\telebot\apihelper.py", line 162, in _make_request
json_result = _check_result(method_name, result)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\telebot\apihelper.py", line 189, in _check_result
raise ApiTelegramException(method_name, result, result_json)
telebot.apihelper.ApiTelegramException: A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: message is too long
"