If you want to receive private notifications from your web app in Telegram here are simple code snippets on Node.js, Ruby and PHP that will allow you to do it.
You can have 1 bot fot many channel. Once you've create a bot. You can add it to another channel (skip step 1 and start with step 2)
- Find
@BotFatherin Telegram /newbot- Choose a name
- Choose a username
- Get token. Should be something like:
992382811:DKyqVhUJsjP-uJivOf2yQi5MuUVvsRyRaZMh
- Create a group chat or add to existing one
- Add your bot there
- Add
@RawDataBotbot to your group chat - Get Chat ID from the JSON That you've got from
@RawDataBot - Remove
@RawDataBotfrom your chat
- Add code snippet to your project
- Change the token and the chat id to yourth
- Call the function
send_telegram("hi")
// where package.json is located: `npm install node-fetch`
// or `npm install -g node-fetch` to install globally
const fetch = require("node-fetch");
const sendTelegram = async (message) => {
const token = "YOUR_BOT_TOKEN";
const data = Object.entries({
text: message,
chat_id: "YOUR_CHAT_ID",
parse_mode: "html"
})
.map(entry => entry[0] + "=" + entry[1])
.join("&");
const url = `https://api.telegram.org/bot${token}/sendMessage?${data}`;
console.log(`url: ${url}`);
const response = await fetch(url);
const json = await response.json();
console.log(json);
return json;
};
sendTelegram("hi!");require "open-uri"
class Telegram
def send_telegram(message)
token = "YOUR_TOKEN"
data = {
text: message,
chat_id: "YOUR_CHAT_ID"
}
url = "https://api.telegram.org/bot#{token}/sendMessage?#{data.to_param}&parse_mode=html"
puts "telegram url: " + url
open(url).read
end
send_telegram("hi!")
end<?php
function send_telegram($message) {
$token = "YOUR_TOKEN";
$data = [
'text' => $message,
'chat_id' => 'YOUR_CHAT_ID'
];
file_get_contents("https://api.telegram.org/bot$token/sendMessage?" . http_build_query($data) . "&parse_mode=html");
}
send_telegram("hi!")import urllib.parse
import urllib.request
def send_telegram(message):
token = "YOUR_TOKEN"
data = {
'text': message,
'chat_id': 'YOUR_CHAT_ID'
}
url = f"https://api.telegram.org/bot{token}/sendMessage?{urllib.parse.urlencode(data)}&parse_mode=html"
response = urllib.request.urlopen(url)
return response.read().decode()
send_telegram("hi!")