-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(integration): Telegram Integration (#175)
Co-authored-by: Joshua Croft <32483134+devjsc@users.noreply.github.com>
- Loading branch information
Showing
9 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Telegram Agent | ||
|
||
The Telegram Agent is a Python agent that serves as an intermediary between other agents in your system and a Telegram bot for sending messages. | ||
|
||
## Prerequisites | ||
|
||
- Python (v3.9+ recommended) | ||
- Poetry (A Python packaging and dependency management tool) | ||
|
||
## Setup | ||
- pip install uAgents | ||
- pip install telegram | ||
- pip install python-telegram-bot | ||
|
||
### Obtain Telegram Bot Token | ||
|
||
1. Go to `@BotFather` on Telegram. | ||
2. Create a new bot or use an existing one to get the token. | ||
3. Remember your bot token and put it later in TELEGRAM_BOT_TOKEN var. | ||
|
||
### Variables Setup | ||
|
||
\`\`\` | ||
TELEGRAM_BOT_TOKEN="Your_Telegram_Bot_Token_Here" | ||
\`\`\` | ||
|
||
## Running The Script | ||
|
||
To run the agent, use the following command: | ||
|
||
\`\`\`bash | ||
poetry run python main.py | ||
\`\`\` | ||
|
||
or | ||
|
||
\`\`\`bash | ||
python3 main.py | ||
\`\`\` | ||
|
||
### Expected Output | ||
|
||
Look for the following output in the console: | ||
|
||
\`\`\` | ||
Agent initialized: {agent_address} | ||
\`\`\` | ||
|
||
Copy the `{agent_address}` and use it wherever needed in your system to interact with this Telegram agent. | ||
|
||
## Inter-agent Communication | ||
|
||
To send messages from another agent, you need to provide: | ||
|
||
1. **Chat ID**: The unique identifier for the chat. | ||
2. **Message**: The actual text to be sent. | ||
|
||
Optionally: | ||
|
||
3. **Bot Token**: If working with multiple bots. | ||
|
||
This will allow any agent in your system to communicate via Telegram by interacting with this Telegram agent. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"title": "Telegram Integration", | ||
"description": "This integration allows agents to forward messages to a Telegram bot. When the agent receives messages from other agents, it forwards these messages to a specified Telegram bot, enabling seamless communication through the Telegram platform.", | ||
"categories": [ | ||
"Messaging", | ||
"Telegram Bot", | ||
"Integration" | ||
], | ||
"deltav": false | ||
} |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import asyncio | ||
from asyncio import Queue | ||
from uagents import Agent, Context, Protocol | ||
from messages.basic import TelegramRequest | ||
from uagents.setup import fund_agent_if_low | ||
|
||
# Asynchronous queue to store messages to be sent | ||
message_queue = Queue() | ||
|
||
# Creating the agent and funding it if necessary | ||
telegram_agent = Agent( | ||
name="telegram_agent", | ||
seed="test seed for telegram agent", | ||
port=8001, | ||
endpoint=["http://127.0.0.1:8001/submit"], | ||
) | ||
fund_agent_if_low(telegram_agent.wallet.address()) | ||
|
||
# Protocol declaration for TelegramRequests | ||
telegram_agent_protocol = Protocol("TelegramRequest") | ||
|
||
# Function to process the message queue | ||
|
||
|
||
async def process_queue(bot): | ||
while True: | ||
if not message_queue.empty(): | ||
chat_id, text = await message_queue.get() | ||
await bot.send_message(chat_id=chat_id, text=text) | ||
await asyncio.sleep(5) | ||
|
||
# Declaration of a message event handler for handling TelegramRequests | ||
|
||
|
||
@telegram_agent_protocol.on_message(model=TelegramRequest) | ||
async def handle_request(ctx: Context, sender: str, request: TelegramRequest): | ||
ctx.logger.info(f"Got request from {sender}: {request.text}") | ||
await message_queue.put((request.chat_id, request.text)) | ||
|
||
# Include protocol to the agent | ||
telegram_agent.include(telegram_agent_protocol) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from uagents import Agent, Context, Protocol # Import necessary modules | ||
from messages.basic import TelegramRequest | ||
from uagents.setup import fund_agent_if_low | ||
|
||
INPUT_TEXT = "This is a test message for telegram sent by fetch agent." | ||
TELEGRAM_AGENT_ADDRESS = "agent1qdesxanj9s5l9xk4f6nsqqp2k93l7un859s0q3mrcwftxpe60xjg5crcf38" | ||
|
||
user_agent = Agent( | ||
name="telegram_user", | ||
port=8000, | ||
endpoint=["http://127.0.0.1:8000/submit"], | ||
) | ||
|
||
fund_agent_if_low(user_agent.wallet.address()) | ||
|
||
user_agent_protocol = Protocol("Request") | ||
|
||
|
||
@user_agent_protocol.on_interval(360, messages=TelegramRequest) | ||
async def text_classification(ctx: Context): | ||
ctx.logger.info(f"Sending message to telegram: {INPUT_TEXT}") | ||
await ctx.send(TELEGRAM_AGENT_ADDRESS, TelegramRequest(text=INPUT_TEXT, chat_id=2032178797)) | ||
|
||
user_agent.include(user_agent_protocol) | ||
|
||
if __name__ == "__main__": | ||
user_agent_protocol.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import asyncio | ||
from telegram.ext import Application | ||
|
||
from uagents import Bureau | ||
|
||
from agents.telegram_agent import process_queue, telegram_agent | ||
from agents.telegram_user import user_agent | ||
|
||
# Telegram bot token | ||
TELEGRAM_BOT_TOKEN = "YOU_BOT_TOKEN" | ||
|
||
if __name__ == "__main__": | ||
bureau = Bureau(endpoint="http://127.0.0.1:8000/submit", port=8000) | ||
print(f"Adding agent to Bureau: {telegram_agent.address}") | ||
bureau.add(telegram_agent) | ||
print(f"Adding user agent to Bureau: {user_agent.address}") | ||
bureau.add(user_agent) | ||
|
||
application = Application.builder().token(TELEGRAM_BOT_TOKEN).build() | ||
# Other handlers can be added here | ||
|
||
loop = asyncio.get_event_loop() | ||
loop.create_task(process_queue(application.bot)) | ||
loop.run_until_complete(bureau.run()) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from uagents import Model | ||
|
||
|
||
class TelegramRequest(Model): | ||
text: str | ||
chat_id: int |