How get messages from the telegram with PHP and Telegram API? Create Telegram bot with PHP

31 January 2023 2273 Reading time: 3 minute

How to reach messages using PHP and Telegram Bot API?

Telegram messaging application has been widely used lately, one of the reasons for this is bots that can be created to automate things in telegram. Telegram bots are suitable to be developed in the desired direction using software languages. Users manage telegram bots groups, They are used for hundreds of purposes such as selling products like an e-commerce platform, informing customers, answering questions by generating automatic answers, and performing crypto analysis.

You may have a question mark in your mind: how to create a telegram bot and how to develop it with programming languages?

How to create a Telegram bot?

  1. Find Telegram's official bot management bot by searching for BotFather in the Telegram search tab
  2. Click start talking to bot
  3. By running the
  4. /newbot command, you notify BotFather that you want to create a new bot, and you will receive this message in response (Alright, a new bot. How are we going to call it? Please choose a name for your bot.)
  5. You submit the name to represent your bot, you will receive it in the response (Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot.)
  6. Submit username ending with bot, if the username you submitted has not been used before, your bot will be created successfully. The message you receive as a response contains the HTTP API, you can do many things using it

Create Telegram bot with PHP

There are a few things we can do to send a message to any telegram user using a telegram bot with PHP.

  1. Creating a bot and owning a bot token as we explained above
  2. The user to whom we will send a message should start by logging into the bot (https://t.me/{your username})
  3. We need to learn the chat_id number of the user to whom we will send the message. How to find Telegram chat id you can learn by reading our article

You can send a message to a telegram user with a bot using the following PHP codes.

 


$botToken="YOUR_API_TOKEN";

$website="https://api.telegram.org/bot".$botToken;
$chatId= receiver_chat_id;  //Receiver Chat Id
$params= [
    'chat_id'=>$chatId,
    'text'=>$message,
];
$ch = curl_init($website . '/sendMessage');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($params));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);

How get messages from the telegram with PHP and Telegram API?

In order to manage the bot with PHP, it is necessary to create a Webhook. Next, you will prepare the link as in the example I gave below and open it in the browser, and you will introduce the webhook you created on your site to telegram. https://api.telegram.org/bot{YOUR_API_TOKEN}/setWebhook?url=https://yoursite.com/bot.php Then, by using the PHP codes I have given below, you can access various information about the user who wrote the message and send a message to the user in response.


    include 'helper/Telegram.php';

    $token = "5664215883:AAH-OtJXA194Z7ubTiNaYdInGO9VmL9AlBk";
    
    $telegram = new Telegram($token);
    
    $chat_id            = $telegram->ChatID();
    $msg                = $telegram->Text();
    $data               = $telegram->getData();
    $setData            = $telegram->setData();
    $ReplyToMessageID   = $telegram->ReplyToMessageID();
    //For more: https://github.com/xeayal/telegramApi/blob/main/Telegram.php
    
    $content = array('chat_id' => $chat_id, 'text' => 'Your chat_id: '.$chat_id);
    $telegram->sendMessage($content);
    

Telegram.php

Similar articles