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

Telegram: Agent repeats itself. #35

Open
gtopolice opened this issue Jan 30, 2025 · 0 comments
Open

Telegram: Agent repeats itself. #35

gtopolice opened this issue Jan 30, 2025 · 0 comments

Comments

@gtopolice
Copy link

The agent repeats itself when messaged on telegram. Im not able to replicate the bug, it repeats itself randomly.

Image

Image

import os
import json
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
from game_sdk.hosted_game.agent import Agent
from game_sdk.hosted_game.functions.telegram import TelegramClient

  class TelegramAgent:
      def __init__(self, token: str):
          self.token = token
          self.api_key = os.environ.get("VIRTUALS_API_KEY")
  
          self.tg_client = TelegramClient(bot_token=self.token)
  
          # Load the JSON file for agent configuration
          with open('agent_simple.json') as f:
              agent_json = json.load(f)
  
          self.agent = Agent(
              api_key=self.api_key,
              goal=agent_json['goal'],
              description=agent_json['description'],
              world_info=agent_json['worldInfo']
          )
  
          self.reply_message_fn = self.tg_client.get_function("send_message")
          self.agent.add_custom_function(self.reply_message_fn)
  
          self.application = Application.builder().token(self.token).build()
  
      async def start(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
          pass
  
      async def handle_message(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
          user_message = update.message.text
          chat_id = update.message.chat_id
          user_id = update.message.from_user.id
  
          # React/respond to a certain event
          response = self.agent.react(
              session_id=str(user_id),  # string identifier that you decide
              task="Reply to the user",
              event=f"chat_id: {chat_id} user_message: {user_message}",
              platform="telegram",
          )
  
      def setup_handlers(self):
          self.application.add_handler(CommandHandler("start", self.start))
          self.application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, self.handle_message))
  
      def run(self):
          self.setup_handlers()
          print("Starting agent...")
          self.application.run_polling(drop_pending_updates=True)
  
  if __name__ == "__main__":
      bot_token = os.environ.get("BOT_TOKEN")
      if not bot_token:
          raise ValueError("BOT_TOKEN environment variable is not set")
  
      bot = TelegramAgent(token=bot_token)
      bot.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant