Skip to content

Commit

Permalink
fix: 完善 Telegram 文档;优化部分逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Oct 22, 2023
1 parent 573b350 commit 1361062
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 12 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</a>
</p>

> Push All In One!支持 Server 酱、自定义邮件、钉钉机器人、企业微信机器人、企业微信应用、pushplus、iGot 、Qmsg、息知、PushDeer、Discord、OneBot 等多种推送方式。
> Push All In One!支持 Server 酱、自定义邮件、钉钉机器人、企业微信机器人、企业微信应用、pushplus、iGot 、Qmsg、息知、PushDeer、Discord、OneBot、Telegram 等多种推送方式。
>
> 温馨提示:出于安全考虑, **所有** 推送方式请在 **服务端** 使用!请勿在 **客户端(网页端)** 使用!网页端使用还将额外产生跨域问题。
Expand All @@ -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'
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -38,7 +38,8 @@
"PushDeer",
"pushdeer",
"Discord",
"OneBot"
"OneBot",
"Telegram"
],
"scripts": {
"lint": "cross-env NODE_ENV=production eslint src *.js --fix --ext .ts,.js",
Expand Down Expand Up @@ -140,4 +141,4 @@
"git add"
]
}
}
}
108 changes: 101 additions & 7 deletions src/push/telegram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -39,11 +131,13 @@ export class Telegram implements Send {
}
}

async send(text: string): Promise<AxiosResponse<any>> {
async send(text: string): Promise<AxiosResponse<TelegramResponse>> {
const url = `https://api.telegram.org/bot${this.TELEGRAM_BOT_TOKEN}/sendMessage`
return ajax({
Debugger('text: %s, url: %s', text, url)
return ajax<TelegramResponse>({
url,
query: {
method: 'POST',
data: {
chat_id: this.TELEGRAM_CHAT_ID,
text,
disable_notification: this.TELEGRAM_SEND_SILENTLY,
Expand Down
4 changes: 4 additions & 0 deletions src/utils/ajax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ export async function ajax<T = any>(config: AjaxConfig): Promise<AxiosResponse<T

let httpAgent = null
let httpsAgent = null
Debugger('NO_PROXY: %s', process.env.NO_PROXY)
if (!process.env.NO_PROXY) {
Debugger('HTTP_PROXY: %s', process.env.HTTP_PROXY)
Debugger('HTTPS_PROXY: %s', process.env.HTTPS_PROXY)
Debugger('SOCKS_PROXY: %s', process.env.SOCKS_PROXY)
if (url?.startsWith('http://') || baseURL?.startsWith('http://')) {
if (process.env.HTTP_PROXY) {
httpAgent = new HttpsProxyAgent(process.env.HTTP_PROXY)
Expand Down

0 comments on commit 1361062

Please sign in to comment.