skip to Main Content

I have 2 problems with programming telegram bot by PHP.

  1. problem:
    please, When I try send text with more lines by using API of telegram. By this code:
<?php
$update = file_get_contents('php://input');
$update = json_decode($update, true);
$chatId= $update["message"]["from"]["id"]?$update["message"]["from"]["id"]:null;
$mess= "here is my text.";
$mess = $mess. "n";
$mess = $mess. " this is new line".;
send($mess, $chatId);

function send($text,$chat){
   if(strpos($text, "n")){
        $text = urlencode($text);
    }

    $parameters = array(
        "chat_id" => $chat,
        "text" => $text,
        "parse_mode" => "Markdown"
    );

    api("sendMessage?", $parameters)
}

function api($method,$command){
$token = "******";
$api = "https://api.telegram.org/bot$token/$method";

    if(!$curld = curl_init()){
       echo $curld; 
    }
    curl_setopt($curld, CURLOPT_VERBOSE, true);
    curl_setopt($curld, CURLOPT_POST, true);
    curl_setopt($curld, CURLOPT_POSTFIELDS, $command);
    curl_setopt($curld, CURLOPT_URL, $api);
    curl_setopt($curld, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curld, CURLOPT_TIMEOUT, 30);
    curl_setopt($curld, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curld, CURLOPT_SSL_VERIFYHOST, FALSE);
    $apiRequest = curl_exec($curld);
    curl_close($curld);
    return $apiRequest;
}

My text in telegram bot look as:

"here+is+my+text.+this+is+new+line."

  1. problem, maybe question:
    When user come to my telegram bot, so I want to user see keyboard’s buttons, which I created. Now this I have only for new user, he must click on button "Start".
    But I want to user see keyboard’s buttons, when he return sometimes.

Can you help me with this?

Thank you

2

Answers


  1. The special chars in your text are caused by the urlencode() call.

    Since you’re passing the data as POSTFIELD, there’s no need to use urlencode()

    Also, there were some syntax error’s like a extra . behind $mess = $mess. " this is new line".;.

    Updated script:

    <?php
        $update = file_get_contents('php://input');
        $update = json_decode($update, true);
        $chatId= $update["message"]["from"]["id"]?$update["message"]["from"]["id"]:null;
        $mess= "here is my text.";
        $mess = $mess. "n";
        $mess = $mess. " this is new line";
    
        send($mess, $chatId);
    
        function send($text,$chat){
    
            $parameters = array(
                "chat_id" => $chat,
                "text" => $text,
                "parse_mode" => "Markdown"
            );
    
            api("sendMessage", $parameters);
        }
    
        function api($method, $command){
    
            $token = "!!";
            $api = "https://api.telegram.org/bot{$token}/{$method}";
    
            if(!$curld = curl_init()){
               echo $curld;
            }
            // curl_setopt($curld, CURLOPT_VERBOSE, true);
            curl_setopt($curld, CURLOPT_POST, true);
            curl_setopt($curld, CURLOPT_POSTFIELDS, $command);
            curl_setopt($curld, CURLOPT_URL, $api);
            curl_setopt($curld, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($curld, CURLOPT_TIMEOUT, 30);
            curl_setopt($curld, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curld, CURLOPT_SSL_VERIFYHOST, FALSE);
            $apiRequest = curl_exec($curld);
            curl_close($curld);
            return $apiRequest;
        }
    

    enter image description here

    Login or Signup to reply.
  2. Consider using westacks/telebot library. This code does the same thing but with less code and more clear

    <?php 
    
    require 'vendor/autoload.php';
    
    use WeStacksTeleBotTeleBot;
    
    // Init bot
    $bot = new TeleBot('your bot token');
    
    // Get update from incoming POST request
    $update = $bot->handleUpdate();
    
    // Send message
    $bot->sendMessage([
        'chat_id' => $update->chat()->id,
        'text' => "here is my text.n this is new line",
        "parse_mode" => "Markdown"
    ]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search