Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the handler receives the user's message twice #1419

Open
3 tasks done
AlexeyYakovlev1 opened this issue Apr 24, 2024 · 0 comments
Open
3 tasks done

the handler receives the user's message twice #1419

AlexeyYakovlev1 opened this issue Apr 24, 2024 · 0 comments

Comments

@AlexeyYakovlev1
Copy link

Checklist

  • I am sure the error is coming from Pyrogram's code and not elsewhere
  • I have searched in the issue tracker for similar bug reports, including closed ones
  • I ran pip3 install -U https://github.com/pyrogram/pyrogram/archive/master.zip and reproduced the issue using the latest development version

Description

I have a bot that responds to user messages, I recently discovered a problem that I didn’t pay attention to, now it’s a critical error for my project

Steps to reproduce

the user sent a message to my bot, the bot responded to it, after which the handler accepted the same message again and responded to it a second time

Code example

import os

from pyrogram import filters, Client
from dotenv import load_dotenv

from classes.TgmHandlers import TgmHandlers
from classes.Utils import Utils

load_dotenv()

os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")

db_dsn = os.getenv("DSN")

api_id   = os.getenv("API_ID_TG")
api_hash = os.getenv("HASH_ID_TG")
app      = Client(name="me_client2", api_id=api_id, api_hash=api_hash)

# text message from user
@app.on_message(filters.text)
async def message(app, message):
	tgm_handlers = TgmHandlers(app, message)

	try:
		await tgm_handlers.on_text_message()
	except Exception as e:
		if str(e).startswith("Could not parse LLM output: `"):
			response = str(e).removeprefix("Could not parse LLM output: `").removesuffix("`")
			
			await app.send_message(tgm_handlers.USERNAME, response)
		else:
			print(f"We got a error: {e}")
			
			rand_phrase = Utils().get_rand_phrase_of_clarify()

			await app.send_message(tgm_handlers.USERNAME, rand_phrase)

# in tgm_handlers.on_text_message() work this code:
await chat_manager.chat_bot(all_messages)

# and chat_manager.chot_bot(all_messages) code:
async def chat_with_neural_manager(self, user_message):
        response = self.llm_chain(user_message).invoke(input=user_message)
        
        await self.app.send_message(self.username, response["output"])

Logs

For example:
Client: Hello, how much products you have?
(waiting +-10sec)
Bot: Hello! I have 2 products: programming and design
(in here handler get message from client again: Hello, how much products you have? waiting +-10sec)
Bot: Hello! I have 2 products: programming and design
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant