Skip to content

Commit

Permalink
refactor: 重构 Telegram 到新版接口
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Nov 8, 2024
1 parent 24ffb17 commit 138cba8
Showing 1 changed file with 38 additions and 42 deletions.
80 changes: 38 additions & 42 deletions src/push/telegram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SendResponse } from '@/interfaces/response'

const Debugger = debug('push:telegram')

export interface TelegramOption {
export interface TelegramConfig {
/**
* 机器人令牌
* 您可以从 https://t.me/BotFather 获取 Token。
Expand All @@ -20,27 +20,37 @@ export interface TelegramOption {
* @date 2023-10-22
*/
TELEGRAM_CHAT_ID: number
/**
* 代理地址
*/
PROXY_URL?: string
}

/**
* 参考 https://core.telegram.org/bots/api#sendmessage
*
* @author CaoMeiYouRen
* @date 2024-11-09
* @export
* @interface TelegramOption
*/
export interface TelegramOption {
/**
* 静默发送
* 静默地发送消息。消息发布后用户会收到无声通知。
* @author CaoMeiYouRen
* @date 2023-10-22
*/
TELEGRAM_SEND_SILENTLY?: boolean
disable_notification?: boolean
/**
* 阻止转发/保存
* 如果启用,Telegram 中的机器人消息将受到保护,不会被转发和保存。
* @author CaoMeiYouRen
* @date 2023-10-22
*/
TELEGRAM_PROTECT_CONTENT?: boolean
protect_content?: boolean
/**
* 话题 ID
* 可选的唯一标识符,用以向该标识符对应的话题发送消息,仅限启用了话题功能的超级群组可用
* @author CaoMeiYouRen
* @date 2023-10-22
*/
TELEGRAM_MESSAGE_THREAD_ID?: string
message_thread_id?: string
[key: string]: any
}

interface From {
Expand Down Expand Up @@ -95,57 +105,43 @@ export class Telegram implements Send {
* @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

proxyUrl?: string

constructor(option: TelegramOption) {
Debugger('option: %O', option)
Object.assign(this, option)
constructor(config: TelegramConfig) {
Debugger('config: %O', config)
Object.assign(this, config)
if (!this.TELEGRAM_BOT_TOKEN) {
throw new Error('TELEGRAM_BOT_TOKEN 是必须的!')
}
if (!this.TELEGRAM_CHAT_ID) {
throw new Error('TELEGRAM_CHAT_ID 是必须的!')
}
if (config.PROXY_URL) {
this.proxyUrl = config.PROXY_URL
}
}

async send(text: string): Promise<SendResponse<TelegramResponse>> {
/**
* 发送消息
*
* @author CaoMeiYouRen
* @date 2024-11-09
* @param title 消息标题
* @param [desp] 消息正文,和 title 相加后不超过 4096 个字符
* @param [option] 其他参数
*/
async send(title: string, desp?: string, option?: TelegramOption): Promise<SendResponse<TelegramResponse>> {
const url = `https://api.telegram.org/bot${this.TELEGRAM_BOT_TOKEN}/sendMessage`
Debugger('text: %s, url: %s', text, url)
Debugger('title: "%s", desp: "%s", option: %O', title, desp, option)
const text = `${title}${desp ? `\n${desp}` : ''}`
return ajax<TelegramResponse>({
url,
method: 'POST',
proxyUrl: this.proxyUrl,
data: {
chat_id: this.TELEGRAM_CHAT_ID,
text,
disable_notification: this.TELEGRAM_SEND_SILENTLY,
protect_content: this.TELEGRAM_PROTECT_CONTENT,
message_thread_id: this.TELEGRAM_MESSAGE_THREAD_ID,
},
})
}
Expand Down

0 comments on commit 138cba8

Please sign in to comment.