diff --git a/README.md b/README.md index 8af825e..3e79c57 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@
-> Push All In One!支持 Server 酱、自定义邮件、钉钉机器人、企业微信机器人、企业微信应用、pushplus、iGot 、Qmsg、息知、PushDeer、Discord、OneBot 等多种推送方式。 +> Push All In One!支持 Server 酱、自定义邮件、钉钉机器人、企业微信机器人、企业微信应用、pushplus、iGot 、Qmsg、息知、PushDeer、Discord、OneBot、Telegram 等多种推送方式。 > > 温馨提示:出于安全考虑, **所有** 推送方式请在 **服务端** 使用!请勿在 **客户端(网页端)** 使用!网页端使用还将额外产生跨域问题。 @@ -45,7 +45,7 @@ npm i push-all-in-one -S ## 👨💻 使用 ```ts -import { ServerChanTurbo, CustomEmail, Dingtalk, WechatRobot, WechatApp, PushPlus, IGot, Qmsg, XiZhi, PushDeer, Discord } from 'push-all-in-one' +import { ServerChanTurbo, CustomEmail, Dingtalk, WechatRobot, WechatApp, PushPlus, IGot, Qmsg, XiZhi, PushDeer, Discord, OneBot, Telegram } from 'push-all-in-one' // Server酱。官方文档:https://sct.ftqq.com/ const SCTKEY = 'SCTxxxxxxxxxxxxxxxxxxx' @@ -119,6 +119,13 @@ const DISCORD_USERNAME = 'Discord Bot' const discord = new Discord(DISCORD_WEBHOOK, DISCORD_USERNAME) discord.send('你好,我很可爱 - Discord') +// Telegram Bot 推送。官方文档:https://core.telegram.org/bots/api#making-requests +const telegram = new Telegram({ + TELEGRAM_BOT_TOKEN: '111111:xxxxxxxxxxxxxx', + TELEGRAM_CHAT_ID: 100000, +}) +telegram.send('你好,我很可爱 - Telegram') + // OneBot 推送。官方文档:https://github.com/botuniverse/onebot-11 // 本项目实现的版本为 OneBot 11 // 在 mirai 环境下实现的插件版本可参考:https://github.com/yyuueexxiinngg/onebot-kotlin diff --git a/package.json b/package.json index 7947402..4c5b6dd 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "push-all-in-one", "version": "3.4.0", - "description": "Push All In One!支持 Server酱、自定义邮件、钉钉机器人、企业微信机器人、企业微信应用、pushplus、iGot 、Qmsg、息知、PushDeer、Discord、OneBot 等多种推送方式", + "description": "Push All In One!支持 Server酱、自定义邮件、钉钉机器人、企业微信机器人、企业微信应用、pushplus、iGot 、Qmsg、息知、PushDeer、Discord、OneBot、Telegram 等多种推送方式", "author": "CaoMeiYouRen", "license": "MIT", "main": "dist/index.js", @@ -38,7 +38,8 @@ "PushDeer", "pushdeer", "Discord", - "OneBot" + "OneBot", + "Telegram" ], "scripts": { "lint": "cross-env NODE_ENV=production eslint src *.js --fix --ext .ts,.js", @@ -140,4 +141,4 @@ "git add" ] } -} +} \ No newline at end of file diff --git a/src/push/telegram.ts b/src/push/telegram.ts index 657c277..2653e4f 100644 --- a/src/push/telegram.ts +++ b/src/push/telegram.ts @@ -5,15 +5,72 @@ import { ajax } from '@/utils/ajax' const Debugger = debug('push:telegram') -export type TelegramOption = { +export interface TelegramOption { + /** + * 机器人令牌 + * 您可以从 https://t.me/BotFather 获取 Token。 + * @author CaoMeiYouRen + * @date 2023-10-22 + */ TELEGRAM_BOT_TOKEN: string - TELEGRAM_CHAT_ID: string + /** + * 支持对话/群组/频道的 Chat ID + * 您可以转发消息到 https://t.me/JsonDumpBot 获取 Chat ID + * @author CaoMeiYouRen + * @date 2023-10-22 + */ + TELEGRAM_CHAT_ID: number + /** + * 静默发送 + * 静默地发送消息。消息发布后用户会收到无声通知。 + * @author CaoMeiYouRen + * @date 2023-10-22 + */ TELEGRAM_SEND_SILENTLY?: boolean + /** + * 阻止转发/保存 + * 如果启用,Telegram 中的机器人消息将受到保护,不会被转发和保存。 + * @author CaoMeiYouRen + * @date 2023-10-22 + */ TELEGRAM_PROTECT_CONTENT?: boolean + /** + * 话题 ID + * 可选的唯一标识符,用以向该标识符对应的话题发送消息,仅限启用了话题功能的超级群组可用 + * @author CaoMeiYouRen + * @date 2023-10-22 + */ TELEGRAM_MESSAGE_THREAD_ID?: string } + +interface From { + id: number + is_bot: boolean + first_name: string + username: string +} +interface Chat { + id: number + first_name: string + last_name: string + username: string + type: string +} +interface Result { + message_id: number + from: From + chat: Chat + date: number + text: string +} +export interface TelegramResponse { + ok: boolean + result: Result +} + /** - * Telegram Bot 推送 + * Telegram Bot 推送。 + * 官方文档:https://core.telegram.org/bots/api#making-requests * * @author CaoMeiYouRen * @date 2023-09-16 @@ -22,10 +79,45 @@ export type TelegramOption = { */ export class Telegram implements Send { + /** + * 机器人令牌 + * 您可以从 https://t.me/BotFather 获取 Token。 + * @author CaoMeiYouRen + * @date 2023-10-22 + * @private + */ private TELEGRAM_BOT_TOKEN: string - private TELEGRAM_CHAT_ID: string + /** + * 支持对话/群组/频道的 Chat ID + * 您可以转发消息到 https://t.me/JsonDumpBot 获取 Chat ID + * @author CaoMeiYouRen + * @date 2023-10-22 + * @private + */ + private TELEGRAM_CHAT_ID: number + /** + * 静默发送 + * 静默地发送消息。消息发布后用户会收到无声通知。 + * @author CaoMeiYouRen + * @date 2023-10-22 + * @private + */ private TELEGRAM_SEND_SILENTLY?: boolean = false + /** + * 阻止转发/保存 + * 如果启用,Telegram 中的机器人消息将受到保护,不会被转发和保存。 + * @author CaoMeiYouRen + * @date 2023-10-22 + * @private + */ private TELEGRAM_PROTECT_CONTENT?: boolean = false + /** + * 话题 ID + * 可选的唯一标识符,用以向该标识符对应的话题发送消息,仅限启用了话题功能的超级群组可用 + * @author CaoMeiYouRen + * @date 2023-10-22 + * @private + */ private TELEGRAM_MESSAGE_THREAD_ID?: string constructor(option: TelegramOption) { @@ -39,11 +131,13 @@ export class Telegram implements Send { } } - async send(text: string): Promise