Warning: Undefined property: stdClass::$country_name in /var/www/html/prod/suresh/pagecounter.php on line 10

Warning: Undefined property: stdClass::$city in /var/www/html/prod/suresh/pagecounter.php on line 11

Warning: Undefined array key "HTTP_REFERER" in /var/www/html/prod/suresh/pagecounter.php on line 25

Telegram Bot: Insert line break to text message

Adding "\n" will not break the line in telegram bot. We need to encode it as it will get pass through URL.

A core PHP sample is shown below :

<?php
        $nl = urlencode("\n");
        $returnMsg = "First Line : Sample Message".$nl;
        $returnMsg .= "Second Line : Sample Message".$nl;
        $returnMsg .= "Third Line : Sample Message";
        try {
            $content = file_get_contents($path."/sendmessage?chat_id=".$this->webChatId."&text=".$returnMsg."&parse_mode=html");
            if ($content === false) {
                log_message("Error", "Failed to send message");
            }
        } catch (Exception $e) {
            log_message("Error", "Failed to send message==>".$ex);
        }        
    }
?>

Sharing how we can do it using a Class:

<?php
class Telegram {
    public $webhookApi;
    public $basePath = "https://api.telegram.org/bot";
    public $replyMessage;
    public $webChatId;
    public $nl;
    function __construct() {
        parent::__construct();
        $this->nl = urlencode("\n");
    }
    public function send() {
        $path = $this->basePath.$this->webhookApi;
        $returnMsg = "First Line : Sample Message".$this->nl;
        $returnMsg .= "Second Line : Sample Message".$this->$nl;
        $returnMsg .= "Third Line : Sample Message";
        try {
            $content = file_get_contents($path."/sendmessage?chat_id=".$this->webChatId."&text=".$returnMsg."&parse_mode=html");
            if ($content === false) {
                log_message("Error", "Failed to send message");
            }
        } catch (Exception $e) {
            log_message("Error", "Failed to send message==>".$ex);
        }        
    }
?>




Your feedbacks are most welcome..