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

Added Send Telegram Message Functionality #2593

Closed
wants to merge 9 commits into from
8 changes: 8 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,11 @@ OPENAI_API_KEY=your-openai-api-key
# TW_CONSUMER_SECRET=
# TW_ACCESS_TOKEN=
# TW_ACCESS_TOKEN_SECRET=

################################################################################
### TELEGRAM
################################################################################

# TELEGRAM_BOT_TOKEN - Get TOKEN from @BotFather
# To recieve messages, you need to start conversation with your bot first
TELEGRAM_BOT_TOKEN=your-telegram-bot-token
3 changes: 3 additions & 0 deletions autogpt/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from autogpt.commands.image_gen import generate_image
from autogpt.commands.improve_code import improve_code
from autogpt.commands.twitter import send_tweet
from autogpt.commands.telegram import send_telegram
from autogpt.commands.web_requests import scrape_links, scrape_text
from autogpt.commands.web_selenium import browse_website
from autogpt.commands.write_tests import write_tests
Expand Down Expand Up @@ -213,6 +214,8 @@ def execute_command(command_name: str, arguments):
return generate_image(arguments["prompt"])
elif command_name == "send_tweet":
return send_tweet(arguments["text"])
elif command_name == "send_telegram":
return send_telegram(arguments["text"])
elif command_name == "do_nothing":
return "No action performed."
elif command_name == "task_complete":
Expand Down
26 changes: 26 additions & 0 deletions autogpt/commands/telegram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os
from dotenv import load_dotenv
import requests

load_dotenv()

def send_telegram(message_text):

TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN")
get_ID = f"https://api.telegram.org/bot{TOKEN}/getUpdates"
try:
chat_id = requests.get(get_ID).json()['result']
if len(chat_id) < 1:
return "Sending telegram message failed, you need to start conversation with your bot first."

else:
chat_id = requests.get(get_ID).json()['result'][0]['message']['from']['id']
message = message_text
url = f"https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={chat_id}&text={message}"
requests.get(url) # this sends the message
return "Telegram message sent successfully."

except:
return "ERROR! Invalid telegram bot token. Make sure you entered correct token in your .env file."


1 change: 1 addition & 0 deletions autogpt/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def get_prompt() -> str:
("Execute Python File", "execute_python_file", {"file": "<file>"}),
("Generate Image", "generate_image", {"prompt": "<prompt>"}),
("Send Tweet", "send_tweet", {"text": "<text>"}),
("Send Telegram", "send_telegram", {"text": "<text>"}),
]

# Only add the audio to text command if the model is specified
Expand Down