25

Cloudflare Workers 转发 Telegram Bot 推送信息

 2 years ago
source link: https://azhuge233.com/cloudflare-workers-%e8%bd%ac%e5%8f%91-telegram-bot-%e6%8e%a8%e9%80%81%e4%bf%a1%e6%81%af/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Cloudflare Workers 转发 Telegram Bot 推送信息

在一些地区或者一些场景下访问 Telegram 会有难度,Cloudflare 虽说在一些地区速度也不理想,但总比没有强

将通知消息的标题和内容发送到 Cloudflare Workers 的边缘网络,然后 Workers 转发到 Telegram Bot 的 API(发起 GET 或 POST 请求)

这个方法也可以在未被封禁的服务器上实现,或者在具有外网访问条件的环境内实现,然后通过端口转发到公网

下面是 Cloudflare Workers 的实现代码

const OPT = {
BotToken: '', // Telegram Bot API
ChatID:'', // User ChatID
ParseMode: 'markdownv2' //keep blank, html, markdown or markdownv2
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
async function handleRequest(request) {
let url = new URL(request.url);
let title = url.searchParams.get('title')||'Telegram Bot Notification';
let msg = url.searchParams.get('msg')||'Add msg parameter to send customized message';
if(msg.errcode){
return new Response(JSON.stringify(msg), {
status: 200,
headers:{
'content-type':'application/json; charset=UTF-8'
return await sendMessage(title, msg);
async function sendMessage(title, msg){
let url = "https://api.telegram.org/";
url += "bot" + OPT.BotToken + "/sendMessage?";
url += "chat_id=" + OPT.ChatID + "&";
url += "parse_mode=" + OPT.ParseMode + "&";
url += "text=*" + title + "*%0A%0A";
url += msg;
return fetch(url ,{
method:'get',
headers: {
'Accept': 'text/html,application/xhtml+xml,application/xml;',
'Accept-Encoding': 'gzip, deflate, br',
'User-Agent': 'Mozilla/5.0 Chrome/90.0.4430.72'
const OPT = {
  BotToken: '', // Telegram Bot API
  ChatID:'', // User ChatID
  ParseMode: 'markdownv2' //keep blank, html, markdown or markdownv2
}
 
addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})
 
async function handleRequest(request) {
  let url = new URL(request.url);

  let title = url.searchParams.get('title')||'Telegram Bot Notification';
  let msg = url.searchParams.get('msg')||'Add msg parameter to send customized message';
 
  if(msg.errcode){
    return new Response(JSON.stringify(msg), {
      status: 200, 
      headers:{
        'content-type':'application/json; charset=UTF-8'
      }
    })
  }
 
  return await sendMessage(title, msg);
}

async function sendMessage(title, msg){
  let url = "https://api.telegram.org/";
  url += "bot" + OPT.BotToken + "/sendMessage?";
  url += "chat_id=" + OPT.ChatID + "&";
  url += "parse_mode=" + OPT.ParseMode + "&";
  url += "text=*" + title + "*%0A%0A";
  url += msg;
  
  return fetch(url ,{
    method:'get',
    headers: {
      'Accept': 'text/html,application/xhtml+xml,application/xml;',
      'Accept-Encoding': 'gzip, deflate, br',
      'User-Agent': 'Mozilla/5.0 Chrome/90.0.4430.72'
    }
  });
}

新建 Workers,将代码粘贴至脚本中,点击部署

脚本默认使用 MarkdownV2 格式,如果使用纯文本或 HTML 格式,需要自行修改位于 34 行的标题加粗 ** 号

可以在 OPT 内添加其他通知参数,关于其他参数可以查看 Telegram Bot API

以上代码已发布到 azhuge233 / Teleflare-Messenger


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK