Skip to content

Latest commit

 

History

History

chat

OpenAI Chat Bot

A simple chat bot using OpenAI's API that runs on an ESP32 (or desktop if you prefer).

Setup

Make sure you have Toit installed. The easiest is to use Jaguar.

Install the dependencies (in this directory; or add --project_root=<DIR>).

  • with Jaguar: jag pkg install
  • with pure Toit: toit.pkg install

If you are using Jaguar (preferred), flash it to your device:

  • jag flash

Telegram

Create a new Telegram bot by running /newbot. '@BotFather' will as you for a name and username, then provide the authentication token.

If you want your bot to be able to chat in groups, you need to disable "Groups Privacy": go into the settings, by running /mybots and select the bot you just created. Go to the "Group Privacy" section and disable it.

Create an account at https://platform.openai.com/, and create an API key in the api-keys page.

Authentication

The chatbot requires a password before it sends any request to OpenAI.

To provide the password write the following in a direct message to the bot:

/authenticate <password>

If you want to authorize a group, take the group's chat-id. (The chat-bot will tell you the chat-id in its error message). Then write the following in a direct message to the bot:

/authenticate <password> <chat-id>

Running locally

Set the following environment variables:

  • OPENAI_API_KEY: your OpenAI API key
  • TELEGRAM_TOKEN: the token provided by '@BotFather'
  • CHAT_PASSWORD: the password for the chat. (See the 'Authentication' section above)

Run the bot:

  • with Jaguar: jag run -d host telegram.toit
  • with pure Toit: toit.run telegram.toit

Running on an ESP32

Take the provided telegram_esp32-example.toit and copy/rename it to telegram_esp32.toit (or telegram_esp32_<suffix>.toit if you want to run multiple bots).

Change the credentials in that file, then do the usual Jaguar installation steps:

  • optionally start the serial monitor: jag monitor
  • run the bot: jag run telegram_esp32.toit

If you want to install the bot so it runs on boot:

  • install the bot: jag container install bot telegram_esp32.toit

If you have questions, please ask on the Toit Discord.

Discord

Create a new application on the Discord Developer Portal.

Go to the 'Bot' tab (on the left) and click on 'Add Bot', followed by 'Yes, do it!'.

Depending on your preferences make the bot public or private. Since you are paying for the OpenAI API calls, you probably want to make it private.

Make sure to enable the priviliged gateway intents. Specifically, you need the 'Message Content Intent' (and maybe 'Server Members Intent' as well).

Click on the 'Reset Token' (next to the Icon) and copy/save the token.

Go to the OAuth2 tab (on the left) and go to URL Generator. Select the 'bot' scope (and no other) with the following permissions:

  • Read Messages/View Channels
  • Send Messages
  • Send Messages in Threads

Copy/save the generated URL.

Running locally

Set the following environment variables:

  • OPENAI_API_KEY: your OpenAI API key
  • DISCORD_TOKEN: the token provided by Discord
  • DISCORD_URL: optional; the URL generated by Discord

Run the bot:

  • with Jaguar: jag run -d host discord.toit
  • with pure Toit: toit.run discord.toit

Running on an ESP32

Take the provided discord_esp32-example.toit and copy/rename it to discord_esp32.toit (or discord_esp32_<suffix>.toit if you want to run multiple bots).

Change the credentials in that file, then do the usual Jaguar installation steps:

  • optionally start the serial monitor: jag monitor
  • run the bot: jag run discord_esp32.toit

If you want to install the bot so it runs on boot:

  • install the bot: jag container install bot discord_esp32.toit

Support

If you have questions, please ask on the Toit Discord.