How to Create Telegram Bot Step by Step


Today we are going to explain how to create a telegram bot. Telegram is a popular cloud-based messaging Russian app. This app is available for iOS, Windows, and Android. Using this app users can send messages, audio, videos, and files.

Telegram is direct competition to WhatsApp, which is mostly used in enterprises. Unlike WhatsApp, Telegram app stored data on the cloud server and Bot is the prominent feature of it. More important Telegram app has a feature to optional end-to-end encrypted secret chats.

How a Telegram Bot Works:

Telegram Bot is an automated software application that do some tasks repeatedly. For example when some when message on telegram than via bot, message create in google spreadsheet. This application runs inside the telegram, Using bot API you can manage HTTP requests. You can integrate telegram with other third party web service, send messages, accept payment.

Create a Telegram Bot Step by Step:

Let’s check out the step by step process of creating a telegram bot.

First, open your telegram app and search for botfather, its a bot itself. It will let you create a telegram bot. Click on the Start option.

telegram bot

You can see many control on-screen like /newbot, /token, /revoke etc…

Click or Type /newbot command on it and its ask for choose a bot name. Enter any relevant bot name, don’t use space in bot name.

Now you receive a message that bot is created successfully and HTTP API token shows. This token is used to authorize the telegram bot api calls. Keep note it down for future use.

telegram bot token

To see the complete Bot API description, check this page: https://core.telegram.org/bots/api

Test Telegram API Call:

To check the API response, let execute a sample API call using ‘getMe’ method. Just enter below Web URL in the browser. Must put your bot token in that URL. This will bring your bot account a detail response.

Get Messages that sent to your Telegram Bot:

Their are two API methods to get an update from your bot.

getUpdates
setWebhook

getUpdates Telegram API Method:

First check out ‘getUpdates’ API method. In this method you have to call the message yourself, meaning every time you execute this script, it will bring new update messages response in JSON format.

For example, we have created a script that will get new updates from your bot. In this, we get the chat_id and send a message to your bot window. Check the below code:

When every time you execute above code script, it will post ‘Hello New World’ message on your bot. To get updated data you have to post any message on bot.

send message to bot

This is the manual process, you have to execute the script to send the message again an again. To get this task to automate you can use the second way ‘setWebhook’.

setWebhook Telegram API Method:

In this method telegram automatically execute your server script. Every time bot get new notification, it sends a webhook response on your set webhook URL. This is the most convenient way to create your own Telegram bot.

You must have a secure server to set a webhook URL.

First set the webhook URL via ‘setWebhook’ API method. Use below web URL to do that, must pass a valid secure web page link in URL parameter.

After executing this link in the web browser, you will get a success response.

set webhook

To receive the telegram bot response you need to fetch webhook response in your web server ‘api-call.php’ script file. Check below code example.

Whenever anyone writes a message on your bot then this webhook post ‘Auto Message’ on your bot as a reply. This is an automated process, don’t need any manual action.

Using a telegram bot API, you can perform any action.

Conclusion:

This is a very straightforward way to create a telegram bot, just followed it step by step and you are good to go. If you found this tutorial helpful please share it with others and leave your valuable feedback. Thanks enjoy..

Recommended Posts For You

About Harish

I am professional web developer and blogger. Use this blog to share own api and other web development experience with you. I do accept paid work. Write to me at - [email protected]

View all posts by Harish

1 thought on “How to Create Telegram Bot Step by Step”

  1. A simple, yet straightforward and working sample.
    Struggled on other sites with all their technical mumbo jumbo.
    Thank you

Leave a Comment

Your email address will not be published. Required fields are marked *