How to build a Telegram chatbot
In this guide we’re going to create a Telegram ChatBot that can identify different types of fish. The whole tutorial should take 10-15 minutes and will walk you through setting up the project, connecting Telegram, and deployment.
Project Setup
1. Run the Getting Started Script
Our setup wizard guides you through getting started, run it and follow along with the next steps, entering the values when prompted. If you don’t have access to StartKit yet, you need to purchase it!
2. Get your OpenAI API Key
To get an OpenAI API Key sign up for an account on the OpenAI website, and then navigate to the API Keys page and click Create new secret key.
Then enter the key into the setup wizard:
You can select Open for the type of app we’re creating:
3. Create your bot on Telegram
- Open Telegram (you can install it here) and search for BotFather
- Run the
/newbot
command and follow the instructions to create a new bot
- Add the token to the
.env
file in a new variable calledTELEGRAM_TOKEN
Write the code
- Install the Node Telegram Bot API
- Create a new file in
/server
, call it something likebot.js
Directoryserver
- bot.js
- …
- index.js
- Paste this:
- For this simple bot example we don’t need to run a server, so you can just run your bot:
- Open your Telegram bot (there’s a link to it in BotFather) and start chatting!
Make it better!
You’ll find all the AI prompts in /config/prompts
.
We’re going to make this bot reply like a seafood expert!
Open the vision prompt in /config/prompts/vision.yml
and paste the following into the user prompt:
Run your bot again node bot.js
and it should reply with more detail! 🐠
Deploy your Telegram bot
The quickest and easiest way to deploy is to DigitalOcean’s App Platform which will cost $5/month for the single resource we need.
App Platform is a managed hosting environment, that you can deploy with a single click and will auto-scale your app when needed.
So let’s get deploying!
1. Grant access to your repo
First you need to give DigitalOcean access to your GitHub account using this link, this lets them know what repos you can access:
https://github.com/apps/digitalocean/installations/select_target
2. Create DigitalOcean App
Click this button to create a DigitalOcean App with our default options:
DigitalOcean deploy button for the StartKit.AI Growth plan:
DigitalOcean deploy button for the StartKit.AI Starter plan:
1. Resources
After clicking the button you’ll be greeted by this page:
This shows that DigitalOcean will create two resources for you:
- api: The StartKit.AI Node.js server
- memory: The embeddings server with vector db connection to form your AIs memory.
We don’t need the memory resource for this tutorial so make sure you delete it.
Since we don’t need the whole StartKit.AI server running for this example we need to update the Run Command to our /server/bot.js
entry point. Click Edit next to the api resource and then click Edit again next to the Run Command option:
2. Environment variables
The default environment variables will be prefilled for you on the next page, but the ones that you created during setup will need to be added.
Click the “Edit” button next to the Global section and enter your OPENAI_KEY
and then create a new entry for TELEGRAM_TOKEN
and enter that.
Click save and hit next.
3. Create the app
You don’t need to change any of the other settings, so click through to the end of the process and click “Create Resources”.
After a few minutes your app will finish building but it will fail and that’s ok, please read the next step!!
4. Update the repo
What we’ve just deployed is a version of StartKit.AI from a template GitHub repo, we need to update this to your own repo.
Go to the App Settings page and click “Edit” on the App Spec setting:
Find the github
section, and change startkit-ai/startkit.ai
value to the name of your repo.
For example if your project on GitHub is https://github.com/Jivings/my-ai-project
, then you’d change the value to Jivings/my-ai-project
.
Hit save and your app will re-deploy with your code instead of ours!
Now your Telegram bot should be running! 🚀
Next steps…
Now you can add more commands, customise responses based on messages, and integrate external APIs. We recommend checking out the Telegram’s Bot API documentation for more in-depth Telegram bot building help.
Enable back-and-forth chat with the bot
So far your bot uses the Images API to identify an image.
If you want to have a conversation about the fish or correct mistakes in the identification by replying to the bot you can switch to using the Chat API. A good place to start would be to change out the identifyImage()
function for generateChatReply()
and customise the chat prompt in /config/prompts/chat.yml
.
Have fun!