# Tools are available in the autogen.tools namespace
from autogen import ConversableAgent, LLMConfig
from autogen.tools.experimental import TelegramRetrieveTool, TelegramSendTool
# For running the code in Jupyter, use nest_asyncio to allow nested event loops
#import nest_asyncio
#nest_asyncio.apply()
llm_config = LLMConfig(model="gpt-4o-mini", api_type="openai")
# Our tool executor agent, which will run the tools once recommended by the telegram_agent, no LLM required
executor_agent = ConversableAgent(
name="executor_agent",
human_input_mode="NEVER",
)
with llm_config:
telegram_agent = ConversableAgent(name="telegram_agent")
# Authentication and target Chat ID (in this case a Group chat)
api_id = "123....."
api_hash = "a2e............................."
_chat_id_group = "-4712345678"
# Create our send tool
telegram_send_tool = TelegramSendTool(api_id=api_id, api_hash=api_hash, chat_id=_chat_id_group)
# Register it for recommendation by our Telegram agent
telegram_send_tool.register_for_llm(telegram_agent)
# Register it for execution by our executor agent
telegram_send_tool.register_for_execution(executor_agent)
# And the same for our our retrieve tool
telegram_retrieve_tool = TelegramRetrieveTool(api_id=api_id, api_hash=api_hash, chat_id=_chat_id_group)
telegram_retrieve_tool.register_for_llm(telegram_agent)
telegram_retrieve_tool.register_for_execution(executor_agent)
# Let’s send a message to our Telegram channel, with a joke for the day.
# We’ll limit it to 2 turns, allowing the Telegram agent to receive the request,
# construct and recommend the send tool, and then the executor agent to execute the tool,
# sending the message to the Telegram group.
executor_agent.initiate_chat(
recipient=telegram_agent,
message="Let's send a message to Telegram giving them a joke for the day about AI agentic frameworks",
max_turns=2,
)