8

I am working on a telegram bot which sends telephone numbers to my telegram account. The problem is, that a '+' is converted to a ' ' blank. So every telephone number is wrong.
E.g. '+4915733000000' turns into '4915733000000'. I've tried to use the HTML code &#43 the unicode version \u002B and the url encoding caracter %2B and none of them work.

https://api.telegram.org/botTOKEN/sendMessage?chat_id=MYID&text=Test:\u2031 Unicode:\u002B HTML:+ URL:%2B 

Result: Test:‱ Unicode: HTML:

Do you know any possiblility to send a plus sign?

Thanks!

Böne
  • 101
  • 1
  • 4

5 Answers5

2

In case someone is using VBA to send Telegram messages with + in them you can replace your string like that:

Dim URL as String
Dim reURL as String

URL = "https://www.webpage.com/product+name/specifics+number" 'etc....
reURL = replace(URL, "+, "%2B")

'send message to telegram code here

For more Encoding info you can visit: https://www.w3schools.com/tags/ref_urlencode.ASP

Ahti
  • 25
  • 7
0

It is possible to send the plus sign using POST method.

Here's the sample Google App Script code (can be easily adapted to JavaScript).

var options = {
  method : "post",
  payload: {
    method: "sendMessage",
    chat_id: "<chat_id_here>",
    text: "+something",
    parse_mode: "HTML"
  }
};

var response = UrlFetchApp.fetch("https://api.telegram.org/bot<YOUR_TOKEN>/", options);

Plus sign can also be easily sent with parse_mode="Markdown".
Just checked (this time on Python using telebot library) that both options work:

bot.send_message(CHAT_ID, "Phone number: +1234567890", parse_mode='Markdown')
bot.send_message(CHAT_ID, "Phone number: +1234567890", parse_mode='HTML')
Code Your Dream
  • 347
  • 3
  • 11
0

I had the same problem. I was using Java and Spring's WebClient. The only way to make it work is building WebClient using DefaultUriBuilderFactory and set encoding mode to NONE.

DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(url);
factory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.NONE);
WebClient webClient = WebClient.builder().uriBuilderFactory(factory).filter(logRequest()).build();

Default is EncodingMode.TEMPLATE_AND_VALUES so if you replace + with %2B the resulting URL is %252B. Setting the encoding mode to NONE doesn't replace any especial characters so I had to replace them manually.

private String replaceUrlSpecialCharacters(String message) {
    return message.replace("%", "%25").replace("+", "%2B").replace(" ", "%20").replace("|", "%7C").replace(System.lineSeparator(), "%0A");
}

And now the + sign is shown in my messages.

Víctor Gómez
  • 724
  • 6
  • 23
0

I'am using PHP and this case was solved with rawurlencode. Below is the code:

public function send_message($tg_msg)
{
    $tg_token = ''; // Bot Token
    $chat_id = ''; // Chat ID
    $url = 'https://api.telegram.org/bot' . $tg_token . '/sendMessage?parse_mode=markdown&chat_id=' . $chat_id;
    $curlopt_url = $url . '&text=' . rawurlencode($tg_msg);
    $ch = curl_init();
    $optArray = array(
        CURLOPT_URL => $curlopt_url,
        CURLOPT_RETURNTRANSFER => true
    );
    curl_setopt_array($ch, $optArray);
    curl_exec($ch);
    curl_close($ch);
}

$msg = 'The message';
send_message($msg);

And now the + sign is shown in my messages.

0

I got that solved by just using this php function:

utf8_encode(text_to_send)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61