Eleirbag89 / TelegramBotPHP

A very simple PHP Telegram Bot API for sending messages.
http://eleirbag89.github.io/TelegramBotPHP
MIT License
809 stars 341 forks source link

How to sendMesage with bold / italics? #103

Closed cesaregerbino closed 7 years ago

cesaregerbino commented 7 years ago

First of all thanks for your code that is very useful for me!

I'd like to put some bold / italics word in my messages but I can't ...

$myText .= "*Name* : ";
......
...
...
$parse_mode = "Markdown";
$content = array('chat_id' => $chat_id,  'text' => $myText, 'parse_mode'=> $parse_mode, 'disable_web_page_preview'=>true);
$telegram->sendMessage($content);

No error when I execute but nothing appears from my bot .... note that if I remove'parse_mode'=> $parse_mode, from the $content all works fine ...

Suggestions / examples?

Thank you very much in advance!

Mohsen322 commented 7 years ago

hi cesar your sample code work great for me i think your problem exist before these lines of codes.

can you send full code?

cesaregerbino commented 7 years ago

This is a good news for me! My code it's quite too big but I'll try di reduce it so I'll be able to send it.Thanks!

cesaregerbino commented 7 years ago

I found where is the problem .. .in my text I've some urls like this one http://www.cittadellasalute.to.it/index.php?option=com_content&view=article&id=6786:situazione-pazienti-in-pronto-soccorso&catid=165:pronto-soccorso&Itemid=372 .... all works fine when I dont use parse_mode but when I set parse_mode = "Markdown" to obtain some bold text, in my code these url create problems. I think that these are about special chars like "?", "&", and so on ... how may I manage this situation? Thank you in advance!!

Mohsen322 commented 7 years ago

yes special characters are problems... and i dont think we can show bolding url...

anyway, i have two suggestation

1- use HTML-style for sending messages with links --> Document Here

2- or use urlencode function for ur links --> Here

cesaregerbino commented 7 years ago

Sorry Mohsen ... I don't need to put in bold the urls, but when I configure parse_mode="Markdown" and use some words in my text (NOT the urls ... ), in bold, other parts of the text, in which there are also the urls don't work ...

Here you're a little code sample taht probably explain my situation ...

 function shell($telegram,$text,$chat_id,$user_id,$location,$reply_to_msg)
    {
        date_default_timezone_set('Europe/Rome');
        $today = date("Y-m-d H:i:s");
        //$text = "http://www.cittadellasalute.to.it/index.php?option=com_content&view=article&id=6786:situazione-pazienti-in-pronto-soccorso&catid=165:pronto-soccorso&Itemid=372";
        $text = "http://www.cittadellasalute.to.it/index.php?option=com_content";
        $theReply1="*The text* you've typed is: ".$text;
        $content = array('chat_id' => $chat_id, 'text' => $theReply1,' disable_web_page_preview'=>true, 'parse_mode'=> "Markdown");
        $telegram->sendMessage($content);
        sleep (1);
    }

This code DOESN'T work but if you use $text = "http://www.cittadellasalute.to.it/index.php"; all works fine and the result is ....

The text you've typed is: http://www.cittadellasalute.to.it/index.php

..... that is what I would like!! :-)

Now I try to read the links you suggest ...

cesaregerbino commented 7 years ago

I've solved .... I've changed the parse_mode to HTML and changed the special characters in the url using HTML entities ....

Here you're the code that works!

function shell($telegram,$text,$chat_id,$user_id,$location,$reply_to_msg)
    {
        date_default_timezone_set('Europe/Rome');
        $today = date("Y-m-d H:i:s");
        //$text = "http://www.cittadellasalute.to.it/index.php?option=com_content&view=article&id=6786:situazione-pazienti-in-pronto-soccorso&catid=165:pronto-soccorso&Itemid=372";
        $text = "http://www.cittadellasalute.to.it/index.php?option=com_content&view=article&id=6786:situazione-pazienti-in-pronto-soccorso&catid=165:pronto-soccorso&Itemid=372";
        $theReply1="<b>The text</b> you've typed is: ".$text;
        $content = array('chat_id' => $chat_id, 'text' => $theReply1,' disable_web_page_preview'=>true, 'parse_mode'=> "HTML");
        $telegram->sendMessage($content);
        sleep (1);
    }

Thanks a lot for the suggestions!

Cesare

Mohsen322 commented 7 years ago

well done!