How to Create Telegram Bot With PHP


Today we are going to explain how to create telegram bot with PHP. Telegram is a popular cloud based messaging Russian app. This app is available for iOS, Windows and Android. Using this app user 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 cloud server and Bot is the prominent featured of it. More important Telegram app has feature to optional end-to-end encrypted secret chats.

How Telegram Bot Works:

Telegram Bot is 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 applications 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 Telegram Bot Step by Step:

First open your telegram app and search for botfather, its a bot itself. It will let you create an bot. Click on 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 an message that bot is created successfully and HTTP API token shows. This token is used to authorize the 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 API Call:

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

Get Messages that sent to your Telegram Bot:

Thier are two API methods to get update from your bot.

getUpdates
setWebhook

getUpdates API Method:

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

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

When every time you excute above code script, it will post ‘Hello New World’ message on your bot. To get update 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 message again an again. To get this task automate you can use second way ‘setWebhook’.

setWebhook API Method:

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

You must have secure server to set an webhook URL.

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

After executing this link in web browser, you will get success resposne.

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 write an message on your bot than this webhook post ‘Auto Message’ on your bot as reply. This is automate process, don’t need any manual action.

Using telegram bot, you can perform any action via its API.

Conclusion:

This is very straight way to create an telegram bot in PHP, 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..

Leave a Comment