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 Bot integration & Voice Prompt #1135

Closed
wants to merge 75 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
9b20855
Make my own version
Wladastic Apr 12, 2023
62eab00
telegram bot sketch
Wladastic Apr 13, 2023
ed520e2
Merge branch 'master' into wlad-version
Wladastic Apr 13, 2023
9fc04ce
whitespace fix
Wladastic Apr 13, 2023
001f51b
reformat for linter
Wladastic Apr 13, 2023
90e0ce5
Merge pull request #1 from Wladastic/wlad-version
Wladastic Apr 13, 2023
ad36d95
Merge pull request #2 from Wladastic/master
Wladastic Apr 13, 2023
5dc86af
Merge branch 'master' into wlad-version
Wladastic Apr 13, 2023
240f614
ttt
Wladastic Apr 13, 2023
da4cabd
Merge branch 'wlad-version' of https://github.com/Wladastic/Auto-GPT …
Wladastic Apr 13, 2023
5ad4e3d
Merge remote-tracking branch 'upstream/master' into wlad-version
Wladastic Apr 13, 2023
da4b434
Merge remote-tracking branch 'upstream/master' into wlad-version
Wladastic Apr 13, 2023
fd26b04
speechrecognition requirement
Wladastic Apr 13, 2023
066d9f7
cleanup main
Wladastic Apr 13, 2023
c30ca78
fix main
Wladastic Apr 13, 2023
3de53e4
fix send_message in main
Wladastic Apr 13, 2023
8c67485
Merge remote-tracking branch 'upstream/master' into wlad-version
Wladastic Apr 13, 2023
4e3b48d
t
Wladastic Apr 13, 2023
c25920c
fix telegram disbled
Wladastic Apr 13, 2023
3f8a64e
fix telegram disbled
Wladastic Apr 13, 2023
026f6f1
added default ENTER instead of y
Wladastic Apr 14, 2023
bbf44fb
Merge remote-tracking branch 'upstream/master' into wlad-version
Wladastic Apr 14, 2023
2bbd84f
cleaner config
Wladastic Apr 14, 2023
ec46520
telegram running!
Wladastic Apr 14, 2023
9b3c700
debug listening for response
Wladastic Apr 14, 2023
c28969b
add stop and fix async
Wladastic Apr 14, 2023
015a351
Cleaner, but getting stuck on start...
Wladastic Apr 14, 2023
6d5107d
bugfixes.. but thread still blocked by ask_user method
Wladastic Apr 14, 2023
32493fb
much more work to figure out than I thought
Wladastic Apr 14, 2023
a145c80
thread block fix attempt
Wladastic Apr 15, 2023
01696b5
Merge remote-tracking branch 'upstream/master' into wlad-version
Wladastic Apr 15, 2023
9188317
update
Wladastic Apr 15, 2023
feb88fb
update config
Wladastic Apr 15, 2023
df44f18
fix __main.py__ starting
Wladastic Apr 15, 2023
d6b5073
fix asyncio import
Wladastic Apr 15, 2023
a536414
telegram bot
Wladastic Apr 15, 2023
2644852
allow feedback input from telegram
Wladastic Apr 15, 2023
905079b
Merge branch 'Significant-Gravitas:master' into wlad-version
Wladastic Apr 15, 2023
5ac1b52
Merge remote-tracking branch 'upstream/master' into wlad-version
Wladastic Apr 15, 2023
7ed8f3f
autogpt package fixes
Wladastic Apr 15, 2023
e8b5781
fix imports
Wladastic Apr 15, 2023
320c130
Merge remote-tracking branch 'upstream/master' into wlad-version
Wladastic Apr 15, 2023
7b7fe06
Merge remote-tracking branch 'upstream/master' into wlad-version
Wladastic Apr 16, 2023
a970f05
fix linting
Wladastic Apr 16, 2023
6c3abed
it works!
Wladastic Apr 16, 2023
5a31f04
last touchup and done!
Wladastic Apr 16, 2023
62d2743
remove dockerfilechange
Wladastic Apr 16, 2023
a3bd771
clean __main__.py
Wladastic Apr 16, 2023
4686a9e
Show "Thinking.." so that bot doesn't look dead.
Wladastic Apr 16, 2023
813d3b1
add message "exiting..."
Wladastic Apr 16, 2023
4d02a69
clean for pipeline
Wladastic Apr 16, 2023
1b32c22
clean for pipeline
Wladastic Apr 16, 2023
2104712
use "python autogpt/start_bot.py --gpt3only --speak" works too now
Wladastic Apr 16, 2023
f5c4ee8
listen to /stop command on ask_user too.
Wladastic Apr 16, 2023
7731c32
listen to /stop command on ask_user too.
Wladastic Apr 16, 2023
d5ed8a7
format
Wladastic Apr 16, 2023
b773b02
Merge remote-tracking branch 'upstream/master' into wlad-version
Wladastic Apr 16, 2023
51465a3
change loop order to fix macos run
Wladastic Apr 16, 2023
dbe708d
fixes with new merges on master...
Wladastic Apr 16, 2023
59aebdc
tasklist instead of ps on windows
Wladastic Apr 16, 2023
513768e
Merge branch 'master' into wlad-version
Wladastic Apr 16, 2023
4085fbd
Merge remote-tracking branch 'upstream/master' into wlad-version
Wladastic Apr 16, 2023
96b28de
fix response elif
Wladastic Apr 16, 2023
702e778
Merge branch 'master' into wlad-version
Wladastic Apr 17, 2023
027af8c
Merge remote-tracking branch 'upstream/master' into wlad-version
Wladastic Apr 18, 2023
ce63bb7
start with python3 if no python & added /yes /no clickable options
Wladastic Apr 18, 2023
4284819
remove AUTOGPT starting now message
Wladastic Apr 18, 2023
7fe8b48
fixed black formatting error
Wladastic Apr 18, 2023
e42fc14
isort fixed && selenium macos check
Wladastic Apr 18, 2023
7864e8a
Merge remote-tracking branch 'upstream/master' into wlad-version
Wladastic Apr 18, 2023
0ecda8c
Merge branch 'master' into wlad-version
Wladastic Apr 19, 2023
bf62cfd
shorten message on Error
Wladastic Apr 19, 2023
f672d3f
Merge remote-tracking branch 'upstream/master' into wlad-version
Wladastic Apr 19, 2023
95b4801
Merge remote-tracking branch 'upstream/master' into wlad-version
Wladastic Apr 19, 2023
d96c4da
Merge remote-tracking branch 'upstream/master' into wlad-version
Wladastic Apr 20, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,26 @@ OPENAI_API_KEY=your-openai-api-key

#ALLOWLISTED_PLUGINS - Sets the listed plugins that are allowed (Example: plugin1,plugin2,plugin3)
ALLOWLISTED_PLUGINS=


################################################################################
### VOICE INPUT PROVIDER
################################################################################

### MAC OS
# USE_MAC_OS_VOICE_INPUT - Use Mac OS voice input or not (Default: False)
USE_MAC_OS_VOICE_INPUT=False

### GOOGLE
# GOOGLE_CLOUD_API_KEY - Google Cloud API key (Example: my-google-cloud-api-key)
GOOGLE_CLOUD_API_KEY=your-google-cloud-api-key


################################################################################
### TELEGRAM BOT
################################################################################
# TELEGRAM_BOT_TOKEN - Telegram bot token (Example: my-telegram-bot-token)
# TELEGRAM_BOT_CHAT_ID - Telegram bot chat ID (Example: my-telegram-bot-chat-id)
TELEGRAM_ENABLED=False
TELEGRAM_API_KEY=your-telegram-bot-token
TELEGRAM_CHAT_ID=your-telegram-bot-chat-id
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,22 @@ Use at your own risk.

2. To exit the program, press Ctrl + C

## Telegram Bot
First setup a telegram bot by following the instructions [here](https://core.telegram.org/bots#6-botfather)

Then set the following variables in your `.env`:

```
TELEGRAM_ENABLED=True
TELEGRAM_API_KEY=your-telegram-bot-token
TELEGRAM_CHAT_ID=your-telegram-bot-chat-id
```
to obtain your chat id, send a message to your bot and then use the following command:

```
curl https://api.telegram.org/bot<your-telegram-bot-token>/getUpdates
```

## GPT3.5 ONLY Mode

If you don't have access to the GPT4 api, this mode will allow you to use Auto-GPT!
Expand Down
64 changes: 48 additions & 16 deletions autogpt/agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from autogpt.logs import logger, print_assistant_thoughts
from autogpt.speech import say_text
from autogpt.spinner import Spinner
from autogpt.telegram_chat import TelegramUtils
from autogpt.utils import clean_input


Expand Down Expand Up @@ -80,7 +81,9 @@ def start_interaction_loop(self):
"Continuous Limit Reached: ", Fore.YELLOW, f"{cfg.continuous_limit}"
)
break

if cfg.telegram_enabled:
telegramUtils = TelegramUtils()
telegramUtils.send_message("Thinking... \n")
# Send message to AI, get response
with Spinner("Thinking... "):
assistant_reply = chat_with_ai(
Expand All @@ -107,29 +110,48 @@ def start_interaction_loop(self):
command_name, arguments = get_command(assistant_reply_json)
if cfg.speak_mode:
say_text(f"I want to execute {command_name}")
if cfg.telegram_enabled:
TelegramUtils().send_message(
f"I want to execute {command_name} \n with arguments {arguments}"
)
except Exception as e:
logger.error("Error: \n", str(e))

if not cfg.continuous_mode and self.next_action_count == 0:
# ### GET USER AUTHORIZATION TO EXECUTE COMMAND ###
# Get key press: Prompt the user to press enter to continue or escape
# to exit
logger.typewriter_log(
"NEXT ACTION: ",
Fore.CYAN,
f"COMMAND = {Fore.CYAN}{command_name}{Style.RESET_ALL} "
f"ARGUMENTS = {Fore.CYAN}{arguments}{Style.RESET_ALL}",
)
self.user_input = ""
if cfg.telegram_enabled:
telegramUtils = TelegramUtils()
telegramUtils.send_message(
"NEXT ACTION: \n " + f"COMMAND = {command_name} \n "
f"ARGUMENTS = {arguments}"
)
else:
logger.typewriter_log(
"NEXT ACTION: ",
Fore.CYAN,
f"COMMAND = {Fore.CYAN}{command_name}{Style.RESET_ALL} "
f"ARGUMENTS = {Fore.CYAN}{arguments}{Style.RESET_ALL}",
)
print(
"Enter 'y' to authorise command, 'y -N' to run N continuous "
"commands, 'n' to exit program, or enter feedback for "
f"{self.ai_name}...",
flush=True,
)
while True:
console_input = clean_input(
Fore.MAGENTA + "Input:" + Style.RESET_ALL
)
console_input = ""
if cfg.telegram_enabled:
telegramUtils = TelegramUtils()
console_input = telegramUtils.ask_user(
"Enter 'y' to authorise command, 'y -N' to run N continuous \n commands, 'n' to exit program, or enter feedback for me."
)
else:
console_input = clean_input(
Fore.MAGENTA + "Input:" + Style.RESET_ALL
)
if console_input.lower().strip() == "y":
user_input = "GENERATE NEXT COMMAND JSON"
break
Expand Down Expand Up @@ -164,16 +186,26 @@ def start_interaction_loop(self):
"",
)
elif user_input == "EXIT":
if cfg.telegram_enabled:
telegramUtils = TelegramUtils()
telegramUtils.send_message("Exiting...")
print("Exiting...", flush=True)
break
else:
# Print command
logger.typewriter_log(
"NEXT ACTION: ",
Fore.CYAN,
f"COMMAND = {Fore.CYAN}{command_name}{Style.RESET_ALL}"
f" ARGUMENTS = {Fore.CYAN}{arguments}{Style.RESET_ALL}",
)
if cfg.telegram_enabled:
telegramUtils = TelegramUtils()
telegramUtils.send_message(
"NEXT ACTION: \n " + f"COMMAND = {command_name} \n "
f"ARGUMENTS = {arguments}"
)
else:
logger.typewriter_log(
"NEXT ACTION: ",
Fore.CYAN,
f"COMMAND = {Fore.CYAN}{command_name}{Style.RESET_ALL} "
f"ARGUMENTS = {Fore.CYAN}{arguments}{Style.RESET_ALL}",
)

# Execute command
if command_name is not None and command_name.lower().startswith("error"):
Expand Down
2 changes: 2 additions & 0 deletions autogpt/auto_gpt_workspace/instance_history.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Auto-GPT instance history:
- First instance created on Sat Apr 15 17:16:18 2023
2 changes: 1 addition & 1 deletion autogpt/commands/web_selenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def scrape_text_with_selenium(url: str) -> tuple[WebDriver, str]:
# See https://developer.apple.com/documentation/webkit/testing_with_webdriver_in_safari
driver = webdriver.Safari(options=options)
else:
if platform == "linux" or platform == "linux2":
if platform == "linux" or platform == "linux2" or platform == "darwin":
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--remote-debugging-port=9222")

Expand Down
11 changes: 8 additions & 3 deletions autogpt/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
"""
This module contains the configuration classes for AutoGPT.
"""
from autogpt.config.ai_config import AIConfig
from autogpt.config.config import Config, check_openai_api_key
from autogpt.config.singleton import AbstractSingleton, Singleton
try:
from autogpt.config.ai_config import AIConfig
from autogpt.config.config import Config, check_openai_api_key
from autogpt.config.singleton import AbstractSingleton, Singleton
except ModuleNotFoundError:
from .ai_config import AIConfig
from .config import Config, check_openai_api_key
from .singleton import AbstractSingleton, Singleton

__all__ = [
"check_openai_api_key",
Expand Down
13 changes: 12 additions & 1 deletion autogpt/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
from colorama import Fore
from dotenv import load_dotenv

from autogpt.config.singleton import Singleton
try:
from autogpt.config.singleton import Singleton
except ModuleNotFoundError:
from config.singleton import Singleton

load_dotenv(verbose=True)

Expand Down Expand Up @@ -58,9 +61,17 @@ def __init__(self) -> None:
self.elevenlabs_voice_1_id = os.getenv("ELEVENLABS_VOICE_1_ID")
self.elevenlabs_voice_2_id = os.getenv("ELEVENLABS_VOICE_2_ID")

self.use_mac_os_voice_input = False
self.use_mac_os_voice_input = os.getenv("USE_MAC_OS_VOICE_INPUT")

self.use_mac_os_tts = False
self.use_mac_os_tts = os.getenv("USE_MAC_OS_TTS")

self.telegram_enabled = os.getenv("TELEGRAM_ENABLED") == "True"
self.telegram_api_key = os.getenv("TELEGRAM_API_KEY")
self.telegram_chat_id = os.getenv("TELEGRAM_CHAT_ID")
self.telegram_voice_enabled = os.getenv("TELEGRAM_VOICE_ENABLED") == "False"

self.use_brian_tts = False
self.use_brian_tts = os.getenv("USE_BRIAN_TTS")

Expand Down
7 changes: 7 additions & 0 deletions autogpt/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ def typewriter_log(
if speak_text and CFG.speak_mode:
say_text(f"{title}. {content}")

if CFG.telegram_enabled:
from autogpt.telegram_chat import TelegramUtils

telegramUtils = TelegramUtils()
message = f"{title}. {content}"
telegramUtils.send_message(message)

if content:
if isinstance(content, list):
content = " ".join(content)
Expand Down
115 changes: 115 additions & 0 deletions autogpt/start_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import asyncio
import os
import sys
import traceback
from threading import Lock, Semaphore

from telegram import Update
from telegram.ext import Application, CallbackContext, CommandHandler

try:
from autogpt.config.config import Config
from autogpt.telegram_chat import TelegramUtils, is_authorized_user
except ModuleNotFoundError:
from config import Config
from telegram_chat import TelegramUtils, is_authorized_user

cfg = Config()

main_started = False

mutex_lock = Lock() # Ensure only one sound is played at a time
# The amount of sounds to queue before blocking the main thread
queue_semaphore = Semaphore(1)


async def stop(update: Update, context: CallbackContext):
if is_authorized_user(update):
process = os.popen("ps -Af")
output = process.read()
process.close()
if "python -m autogpt" in output:
await update.message.reply_text("Stopping Auto-GPT now!")
os.system("pkill -f autogpt")
TelegramUtils.send_message("Auto-GPT was stopped.")
else:
TelegramUtils.send_message("Auto-GPT was not running.")


async def start(update: Update, context: CallbackContext):
global main_started
print("Starting Auto-GPT...")
if is_authorized_user(update):
# Check if operating system is Windows
if os.name == "nt":
# Check if main is still running
process = os.popen("tasklist")
else:
# check if main is still running
process = os.popen("ps -Af")
output = process.read()
process.close()
if "python -m autogpt" in output:
await update.message.reply_text("Already started!")
return
else:
# check if python is installed, using python3 if not
if os.name == "nt":
# Check if main is still running
process = os.popen("where python")
else:
# check if main is still running
process = os.popen("which python")
output = process.read()
process.close
print(output)
if "python" in output:
os.system("python -m autogpt {}".format(" ".join(sys.argv[1:])))
else:
os.system("python3 -m autogpt {}".format(" ".join(sys.argv[1:])))


def main():
print("Starting up...")

telegramUtils = TelegramUtils()
telegramUtils.send_message("Starting Auto-GPT...")

# Delete old messages
asyncio.run(telegramUtils.delete_old_messages())

TelegramUtils().send_message(
"Hello! I need you to confirm with /start to start me. <3"
)

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

application = Application.builder().token(cfg.telegram_api_key).build()
application.add_handler(CommandHandler("stop", stop))
application.add_handler(CommandHandler("start", start))
try:
print("Check your Telegram chat to start Auto-GPT! ;-)")
loop.run_until_complete(application.run_polling())
except KeyboardInterrupt:
pass


if __name__ == "__main__":
try:
main()
# queue_semaphore.acquire(True)
# thread = threading.Thread(target=main)
# thread.start()
except KeyboardInterrupt:
print("Exiting...")
TelegramUtils.send_message("I hope I could help! :) \n \n Bye bye! <3")

exit(0)
except Exception as e:
print(e)
print(traceback.format_exc())
TelegramUtils.send_message(
"Sorry, I have to stop. \n \n An error occurred: " + str(e)
)
exit(1)
Loading