amazingpaddy / bing-telegram-bot

A chatgpt bot for telegram
24 stars 5 forks source link

I modified the code a little, maybe you will like it, if I'm a beginner and don't judge strictly #1

Open Deadtem333 opened 1 year ago

Deadtem333 commented 1 year ago

import library

import re
import asyncio
import pickle import aiogram
from EdgeGPT import Chatbot

Insert your bot token

BOT_TOKEN = ''

bot = aiogram.Bot(token=BOT_TOKEN)

Create a Dispatcher object - it will be responsible for incoming messages

dp = aiogram.dispatcher.Dispatcher(bot)

Add your Telegram ID to the list without the @ symbol

authorized_id = ['<INSERT YOUR TELEGRAM USER NAME, NOT THE DISPLAY NAME>']

Initiate a dictionary to store message history

message_history = {}

async def bingChat(prompt, cookiePath, context='', user_id=None):
global message_history
gbot = Chatbot(cookiePath=cookiePath)
full_prompt = ' '.join([str(context), str(prompt)]).strip()
message_history[user_id] = full_prompt
response_dict = await asyncio.wait_for(gbot.ask(prompt=full_prompt), timeout=30)
response = response_dict['item']['messages'][1]['text']

Save cookie

with open(cookiePath, 'wb') as f:
    pickle.dump(gbot.cookies, f)
return re.sub(r'\[\^\d\^\]', '', response)   

pay attention to the decorator

@dp.message_handler(content_types=['text'])
async def handle_message(message: aiogram.types.Message):
global message_history
username = message.from_user.username
user_id = message.from_user.id

#   Checking if the user is logged in
if username not in authorized_id:   
    await bot.send_message(chat_id=message.chat.id, text="Not authorized to use this bot")   
    return   

cookiePath = f"./{user_id}.cookies.pkl"   

#    Getting message history
context = message_history.get(user_id, "")   

# combine the context with the current message
prompt = " ".join([context.strip(), message.text]).strip()   

# If the message is "/remove", remove it from the history

if prompt.strip() == "/remove":   
    message_history.pop(user_id, None)   
    await bot.reply_to(message, "Message history cleared")   
    return   

#   Save the message in history
message_history[user_id] = prompt   

print(f"Request received from {username} - {prompt}")   
if not prompt.strip():   
    await bot.reply_to(message, "Empty query sent. Please send a message to get a response.")   
else:   
    #    We ask the model
    bot_response = await bingChat(prompt, cookiePath, context, user_id=user_id)   
    print(f"Response received - {bot_response}")   

    #   Sending a response
    await bot.send_message(chat_id=message.chat.id, text=bot_response.replace('?\n\n', ''))   

async def main():

Launching the bot

await dp.start_polling()   

if name == 'main':
asyncio.run(main())

in this code, I use the aiogram library for the telegram bot, I was able to change it so that the bot itself determines the user's messages and it seems like the bot should remember previous messages, but I could not check this, I will be glad if someone experienced can complete this code.

amazingpaddy commented 1 year ago

Let me check.

Deadtem333 commented 1 year ago

Hi, the previous code i posted is not very good, can i upload the one i made yesterday and you can check it out?

ср, 5 апр. 2023 г., 01:14 Paddy @.***>:

Let me check.

— Reply to this email directly, view it on GitHub https://github.com/amazingpaddy/bing-telegram-bot/issues/1#issuecomment-1496668956, or unsubscribe https://github.com/notifications/unsubscribe-auth/A64OOD7QNUP6PHRSLV3LJELW7SMLLANCNFSM6AAAAAAWPF2ECQ . You are receiving this because you authored the thread.Message ID: @.***>