Faizal Selamat avatar

Pre-format message sending telegram via link

onlinedoit

Published: 14 Apr 2021 › Updated: 14 Apr 2021

Pre-format message sending telegram via link

Prequesition:

  • Create a Telegram public channel
  • Create a Telegram BOT via BotFather
  • Set the bot as administrator in your channel

Provided that you did the above, now you can send a message to your channel by issuing an HTTP GET request to the Telegram BOT API at the following URL:

https://api.telegram.org/bot[BOT_API_KEY]/sendMessage?chat_id=[MY_CHANNEL_NAME]&text=[MY_MESSAGE_TEXT]

  • BOT_API_KEY is the API Key generated by BotFather when you created your bot
  • MY_CHANNEL_NAME is the handle of your channel (e.g. @my_channel_name)
  • MY_MESSAGE_TEXT is the message you want to send (URL-encoded)

Now let’s se how to do this in different languages the easy way.

PHP

$apiToken = "my_bot_api_token";

$data = [
    'chat_id' => '@my_channel_name',
    'text' => 'Hello world!'
];

$response = file_get_contents("https://api.telegram.org/bot$apiToken/sendMessage?" . http_build_query($data) );// Do what you want with result

JAVA

String urlString = "https://api.telegram.org/bot%s/sendMessage?chat_id=%s&text=%s";

String apiToken = "my_bot_api_token";
String chatId = "@my_channel_name";
String text = "Hello world!";

urlString = String.format(urlString, apiToken, chatId, text);

URL url = new URL(urlString);
URLConnection conn = url.openConnection();

StringBuilder sb = new StringBuilder();
InputStream is = new BufferedInputStream(conn.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String inputLine = "";
while ((inputLine = br.readLine()) != null) {
    sb.append(inputLine);
}
String response = sb.toString();
// Do what you want with response

Leave Pre-format message sending telegram via link to:

Written by

Read more #telegram posts


Best Posts From Faizal Selamat

We have not curated any of onlinedoit's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Faizal Selamat