Telegram
Telegram is a cloud-based instant messaging and voice over IP service. It was developed by the Russian entrepreneur Pavel Durov and his brother Nikolai Durov, who were also the founders of VK.com and Odnoklassniki.
Telegram is one of the most popular messaging apps in the world. It has over 500 million active users and is available in 20 different languages.
How to connect to Telegram 🔗
To connect to Telegram, you need to create a Telegram bot. You can do this by following the instructions in the Telegram documentation. The instructions are as follows:
- Open Telegram and search for
@BotFather
. - Click on the bot to start a chat.
- Send the
/newbot
command to create a new bot. - Follow the instructions to create a new bot.
Once you have created a bot, you will receive a token
. This token is used to authenticate your bot with Telegram.
In this guide we are going to use Sarufi to connect to Telegram. Sarufi is a platform that allows you to create chatbots without any coding knowledge. It is a no-code platform that allows you to create chatbots using a simple drag-and-drop interface. You will be able to deploy your chatbot directly to Telegram using Sarufi Dashboard. You have also the option to use your own server to host your chatbot as custom deployment.
Gettting started
Deploying your chatbot to Telegram is easy. You can deploy your chatbot to Telegram using Sarufi Dashboard or using your own server. We are going to cover both methods in this guide.
- Sarufi Dashboard
- Custom Deployment
Sarufi Dashboard provides a simple interface to deploy your chatbot to Telegram. You can create a chatbot from scratch or use one of the templates available in Sarufi Dashboard. Assuming that you already have a chatbot created in Sarufi Dashboard, you can deploy it to Telegram by following the steps below:
Log in to Sarufi Dashboard.
Click on the chatbot you want to deploy to Telegram.
Navigate to settings >> Integrations >> Telegram.
Add your Telegram credentials/Bot's Token
Copy your bot's token obtained from BotFather.
You have a section to add a
Start Message
which will be the bot's reponse when a user sends the/start
command to your bot. You can customize the message according to your preferences. You have the following variables to use in your message:-{name}
- User's name. This is the name the user has on Telegram{bot_name}
- Bot's name from Sarufi Dashboard
Here is an example of a start message:-
Hi {name}, Welcome To {bot_name}, How can I help you?
Click
save
thenDeploy
Wait for your job to finish. Once done, you will have the section status change to
Live
This means that your bot is live on Telegram. You can now test your bot by opening it in Telegram app. You have a section to view your bot's Logs. You can view the logs to see what your bot is doing.
Test your Telegram Bot by opening it in Telegram.
To make changes to your bot, you can edit your bot in Sarufi Dashboard,
Update
thenRe-deploy
it to Telegram. You can view your bot logs by clickingView Logs
button.Take look at the sample bot we have been working on here.
With custom deployment, you can deploy your chatbot to Telegram using your own server. You can use any server that supports Python and Flask/FastAPI. You can also use a cloud service like Heroku or AWS.
We have blueprint to illustrate how to deploy your either using polling or webhook method. You can read more about these methods, polling vs Webhook
Basically we have two methods to deploy your bot to Telegram:-
- Using Polling
- Using Webhooks
You will have to modify some commands shown here to suit your working environment. Commands like python3
and pip3
will depend on your working environment. You may have to use python
and pip
instead.
- Creating project folder and Virtual Environment 📂
Create a project folder
Let's create a project folder,
Telegram bot
and navigate to it.mkdir 'Telegram bot'
cd 'Telegram bot'Creating virtual environment and activating it
You can can set up your evironment according to your preferences, either via
virtualenv
orconda
. Read more on creating virtual environments here.Unix based systems
Install virtual environment
This step is optional as you may have a python virtual environment already installed. If not, you can install it by running the command below.
sudo apt install python3-venv
Create and activate virtual environment
python3 venv -m sarufi_telegram
source sarufi_telegram/bin/activate
Windows
Install virtual environment
This step is optional as you may have a python virtual environment already installed. If not, you can install it by running the command below.
pip install virtualenv
Create and activate virtual environment
python venv -m sarufi_telegram
.\sarufi_telegram\Scripts\activate
Clone the repo and install packages 📦
The shortest route is to use the Telegram blueprint for Sarufi, which is available in our Github repository. You can find it here by cloning the directory.
git clone https://github.com/Neurotech-HQ/telegram-chatbot-blueprint.git
cd telegram-chatbot-blueprint
pip3 install -r requirements.txtConfigure Environment variables ⚙
After installing packages, we need to configure our credentials. In
telegram-chatbot-blueprint
, create a file(.env
) using your text editor to hold environment variables.Open
.env
in the editor to edit the details with your credentials for sarufi and telegram. Then add the following:-SARUFI_API_KEY = Your Sarufi API Key
SARUFI_BOT_ID = bot id
TELEGRAM_TOKEN = telegram token
START_MESSAGE = Hi {name}, welcome to {bot_name}. How can I help you?The
START_MESSAGE
will be the bot's response when/start
command is sent by user.How to run Telegram 🚀
To run the Telegram bot, simply run
main.py
file after configuring the credentials above.python3 main.py
NOTE: All operations are carried out in the active virtual evironment in case you use a virtual environment.
Below is a sample of our PizzaBot we have been working on, view chatbot.
You can use any server that supports Python and Flask/FastAPI. There are many options for doing custom deployment. You can use Replit, Heroku and many other services. We'll be using ngrok to expose our local server to the internet. The procedures are the same for other services.
We have a blueprint to guide you on how to deploy chatbot via replit. Just fork Sarufi Telegram Webhook repl and you're ready to go.
In this section we are going to use ngrok
to expose our local server to the internet. The steps are as follows:-
You will have to modify some commands shown here to suite your working environment. Commands like python3
and pip3
will depend on your working environment. You may have to use python
and pip
instead.
Ngrok installation
Make sure you have ngrok installed in your local machine. You can follow instructions here to install ngrok.
Quick Project Setup
Create a project directory named
Telegram bot
.This is the directory that will hold our project. You can name it anything you want.
mkdir 'Telegram bot'
cd 'Telegram bot'Make Virtual Environment
You will have to modify some commands shown here to suite your working environment. The commands like
python3
andpip3
will depend on your working environment. You may have to usepython
andpip
instead.Using a virtual environment is a good practice, so we are going to create one. You can read more on why use virtual environment. We shall install all necessary packages in the environment
Unix based systems
Install virtual environment
This step is optional as you may have a virtual environment already installed. If not, you can install it by running the command below.
sudo apt install python3-venv
Create a virtual environment and activate it.
python3 -m venv sarufi
source sarufi/bin/activate
Windows
Install virtual environment
This step is optional as you may have a virtual environment already installed. If not, you can install it by running the command below.
pip install virtualenv
Create a virtual environment and activate it.
virtualenv sarufi
.\sarufi\Scripts\activate
- Clone repo and install requirements
Clone and install requirements.
In this part, we are going to clone the Sarufi Telegram Chatbot deployment Blueprint and install the packages.
Run the commands below
git clone https://github.com/Neurotech-HQ/sarufi-telegram-webhook-blueprint.git
cd sarufi-telegram-webhook-blueprint
pip3 install -r requirements.txtEnvironment variables
After installing packages, we need to configure our credentials. In
telegram-chatbot-blueprint
, create a file.env
to hold environment variables. You will need to obtain your credentials from Sarufi and Telegram. Read more on how to get your credentials below.In
.env
, we are going to add the following credentials. Using your favourite text editor add the following:-SARUFI_API_KEY = your API KEY
SARUFI_BOT_ID = bot id
TELEGRAM_BOT_TOKEN = telegram token
START_MESSAGE = Hi {name}, welcome to {bot_name}. How can I help you?Note: The Start Message will be bot's reponse when a user sends /start command to your bot.
You can customize the message to your preference. You have the following variable that you can use in your start message to make it more personalized:-
- {name} - User's name. This is the name the user has on Telegram
- {bot_name} - Bot's name from Sarufi Dashboard
Starting ngrok server
Ngronk is a tool that allows you to expose a web server running on your local machine to the internet.
ngrok http 8000
You will have a public HTTPS URL indicating that it's forwarding to
localhost:8000
.Set Webhook URL
We are going to set the webhook URL to our bot. This is telling telegram server to send updates to our bot via specified URL. This way our server can rest whenever there is no update. With this option you can deploy your chatbot as a lambda function.
Using your favourite API testing client or curl, set the webhook URL as shown below:
curl --request POST 'https://api.telegram.org/bot<your bot token>/setWebhook?url=<your ngrok public url>'
NOTE: The port number (in this case, 8000) should match the port used in
main.py
!Running your bot
Run the python script. This is the time you have been waiting for... Lets launch 🚀 our bot.
python3 main.py
NOTE: All operations are done in the activated virtual environment.
Open your telegram app, search for your bot --> Send it a text. You can see a sample bot below
Get Credentials
Getting Sarufi credentials
To authorize our chabot, we are are going to use authorization keys from Sarufi. Log in into your Sarufi account. Go to your Profile on account to get authorization keys (client ID and client secret).
Telegram Bot token
To create a chatbot on Telegram, you need to contact BotFather, which is essentially a bot used to create other bots.
To connect to Telegram, you need to create a Telegram bot. You can do this by following the instructions in the Telegram documentation. The instructions are as follows:
- Open Telegram and search for
@BotFather
. - Click on the bot to start a chat.
- Send the
/newbot
command to create a new bot. - Follow the instructions to create a new bot.
Once you have created a bot, you will receive a
token
. This token is used to authenticate your bot with Telegram.- Open Telegram and search for
The Start Message
will be bot's reponse when a user sends /start
command to your bot.
You can customize the message to your preference. You have the following variable that you can use in your start message to make it more personalized:-
{name}
- User's name. This is the name the user has on Telegram{bot_name}
- Bot's name from Sarufi Dashboard
Here is example of a start message:
Hi {name}, welcome to {bot_name}. How can I help you?
Test Your Telegram Bot
To test your Telegram chatbot, you can send a message to your bot on the Telegram app. You can also use the Telegram API to send messages to your bot.
- Open Telegram and search for your bot by name.
- Click on the bot to start a chat.
- Send a message to your bot and see how it responds.
Here is the test sample of our PizzaBot we have been working on.
What you learned 👨🏽💻
In this tutorial, you learned how to connect Sarufi to Telegram and how to configure and run your chatbot in Telegram. You also learned how to test your chatbot by sending messages to it through the Telegram app or using the Telegram API. With this knowledge, you can now create your own custom chatbot and connect it to Telegram using Sarufi.