skip to Main Content

I am trying to get points allocated to ma user based on the number of referrals, using the telegram bot by passing a payload to the /start command from where i can track it and know who has been referring people to the bot

i have tried following the documentation process and was able to set the command but i didn’t see a way of getting the payload

  ini_set('error_reporting', E_ALL);
    $token="870645666:AAHrjEF006uje1SpG0dFJRFnmfNIZHbGxdM";
    $website ="https://api.telegram.org/bot".$token;

    $update =file_get_contents("php://input");

    $update =json_decode($update, TRUE);

    $chatid =$update["message"]["chat"]["id"];
    $message =$update["message"]["text"];
    $refid=$update["message"]["text"]["payload"];


    $ref=mysqli_fetch_assoc(mysqli_query($connect, "SELECT * FROM 
          bot where refid='$refid'"));

 sendMessage($chatid, "You were referred by".$ref['name'];

    function sendMessage($chatid, $message){
            $url =$website."/sendMessage? 
              chat_id=".$chatid."&text=".urldecode($message);
            file_get_contents($url);

                                    }

There is no output for the payload when i try to access it
i have tried google but i can’t find a way to fetch the payload using php. Any help would be appreciated

2

Answers


  1. https://core.telegram.org/bots#deep-linking

    You can receive the payload in a normal text message that starts with “/start”

    $text = '/start PAYLOAD';
    if(stripos($text, '/start ') === 0)
    {
        $payload = str_replace('/start ', '', $text);
    }
    
    Login or Signup to reply.
  2. Telegram sends Payload inside the message content to your bot

    $message =$update["message"]["text"];
    // extract payload from message text
    $refid=substr($message, strlen('/start'));
    
    // check is it really first message to start the bot
    if($update["message"]["message_id"] != 1 || stripos($message, "/start ") != 0){
        // $refid = "";
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search