How to Build Fully Responsive Telegram Bot – PHP Tutorial

Fully Responsive Telegram Bot

Hi Geeks! Before we start to create Fully Responsive Telegram Bot, we must ensure the following resources.

  • A Webserver with PHP and MySQL.
  • Program O – PHP I AIML Chatbot.
  • SSL Certificate (HTTPS) to your web server (Don’t worry about it, I’ll explain how to setup free SSL to your site)

Step 1: Set up Your Telegram Bot

  • Chat with Botfather to create your bot

    On Telegram search @BotFather or you can start conversation by navigating through following link t.me/botfather

    Search Botfather

Start conversation with the botfather by typing /start the botfather replies you with set of commands that you can use with your bot.

Set of Botfather Commands

  • Choose a username to your bot

    Now type /newbot. The bot father will ask you to choose a name for your bot. Enter the desired name you want, for example I have named as Chitti.

    On next step the bot father will ask you to choose a username for your bot, the bot name must end with bot for example I gave Chitti2Bot.

Choose Username to your bot

When you’re done the botfather will leave you a congratulation message with access token which is the secret key of your bot and do not share with anyone. Finally you can set description and profile picture for your bot if you need. Type /help for the commands. Now your telegram bot is ready but it won’t respond now proceed to next step.

Step 2: Install Program O

The second step is to install Program O Chatbot. Program O is an Artificial Intelligence Chat bot written in PHP with MySQL. It has in-built Chatbot API. Therefore it is helpful to integrate with our Bot. Read our full guide How to Install Program O – PHP | AIML Chatbot.

Step 3: Add Free SSL to your site

SSL is Secured Socket Layer (Adds https to our site) helps to transfer encrypted data between server and client. It is important that your url must start with HTTPS to work with telegram bot webhooks. Therefore you need valid SSL Certificate. Don’t worry about that, read our guide How to Setup Free SSL Cloudflare For Your Website.

Step 4: Setting Up Webhook with PHP

  • PHP Webhook Script

    Now we need to setup php script that integrates with Program O and Telegram bot webhook. The following gist will do the work you need to replace with

  1. BOT TOKEN‘ with Telegram Bot token sent by bot father.
  2. bot_id with your Program O bot id and
  3. siteurl  with the url where you’ve installed Program O.

Note: Make sure that you’ve uploaded AIML files in Program O to put your bot to work.

  • Upload Script to Server

    Now upload the script to the server, it is recomended to choose a secret url location to place your script. For example:

  • Binding Script URL with Telegram

    Open your favourite text editor and write down the following URL with your own info.

    For Example:

    Now open up browser and paste the url link and hit enter. When everything goes right you will receive a confirmation message that “Webhook was set”.

Step 5: Test Your Fully Responsive Telegram Bot

Finally its time to test your Fully Responsive Telegram Bot, Open your Telegram App and send message to your bot. The bot will respond as follow

Bot Respond

You can even add your bot to the telegram group. To make it work in the group you should send message with prefix / for example /hi.

Test our demo telegram bot @Chitti2Bot

Also Read: How to build Responsive Facebook Messenger Bot & Get Approved

If the bot doesn’t work, double check you’ve followed all the step or feel free to ask in the comment box below.

17 Comments

  1. I understood your code. But what confuses me is that I expected to see where we will have conditional statements that specify if user sends so…so… thing, reply so…so…so… thing but I couldn’t see such. So I was surprise how the bot will now know what to reply based on the message it receives.

    Thanks as I anticipate favourable response.

  2. Hi, there

    Thank you very much for the tutorial. I have followed it all the way but got abit lost in step 4. Would you be so kind as to elaborate just abit more on this? I created the php script and uploaded it to my host server. This is where i have a problem:

    //Binding Script URL with Telegram
    Open your favourite text editor and write down the following URL with your own info.
    1
    https://api.telegram.org/bot%5BBOT_TOKEN%5D/setwebhook?url=%5BWEB_LOCATION_OF_THE_SCRIPT%5D
    For Example:
    1
    https://api.telegram.org/bot123456789:AAEjT6-AAF75N4UfS6FV6Zgl16-P5VuITteHQC8/setwebhook?url=https://example.com/kjnhsdfs54sdfs54sdfsf5sfd64haddsf/telebot.php
    Now open up browser and paste the url link and hit enter. When everything goes right you will receive a confirmation message that “Webhook was set”.//

    Thank you in advance.

    1. Did you checked, uploaded AIML files is present? If AIML file is error it won’t be uploaded and throws error. Try uploading simple AIML file and check.

  3. I get webhook was set when I talk to my bot on telegram I dont get any response or any logs I have written any issues? is the secret path mandatory?

    1. Secret path is not mandatory.. if your webhook was set and no response, the possible errors maybe

      1) You missed to upload AIML files.
      2) SSL not installed
      3) Check the code you didn’t missed to update necessary settings

  4. Hello admin, thanks for this tutorial, can build a bot that can handle a referral system? with captcha embedded

  5. Hi, great tutorial. Recently i have create telegram bot in PHP, i have built auto send message bot. Can you please tell me what other telegram event we can use in bot ?

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.