Skip to content

Commit

Permalink
chore: 使用 telegram-bot-api-jsdoc 替代自定义telegram types
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Nov 15, 2024
1 parent 9499d82 commit 191bf0f
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 52 deletions.
2 changes: 1 addition & 1 deletion build/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"esbuild": "^0.23.0",
"eslint": "^9.8.0",
"eslint-plugin-format": "^0.1.2",
"telegram-bot-api-jsdoc": "^7.11.0",
"typescript": "^5.5.4",
"wrangler": "^3.69.1"
},
Expand Down
2 changes: 1 addition & 1 deletion src/polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if (typeof Buffer === 'undefined') {
if (data instanceof Uint8Array) {
return new Buffer(data.buffer);
}
throw new Error('Unsupported data type');
throw new Error(`Unsupported data type: ${typeof data}, encoding: ${encoding}`);
}

toString(encoding) {
Expand Down
12 changes: 6 additions & 6 deletions src/render.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { checkAddressStatus } from './helper.js';

import 'telegram-bot-api-jsdoc';
import './types.js';

/**
Expand Down Expand Up @@ -42,7 +42,7 @@ async function sendOpenAIRequest(key, endpoint, model, prompt) {
* Render the email list mode.
* @param {EmailCache} mail - The email object.
* @param {Environment} env - The environment object.
* @returns {Promise<TelegramSendMessageRequest>} The rendered email list mode object.
* @returns {Promise<SendMessageParams>} The rendered email list mode object.
*/
export async function renderEmailListMode(mail, env) {
const {
Expand Down Expand Up @@ -100,7 +100,7 @@ To\t\t:\t${mail.to}
* Render the email detail.
* @param {string} text - The email text.
* @param {string} id - The email ID.
* @returns {TelegramSendMessageRequest} - The rendered email detail.
* @returns {SendMessageParams} - The rendered email detail.
*/
function renderEmailDetail(text, id) {
return {
Expand All @@ -127,7 +127,7 @@ function renderEmailDetail(text, id) {
* Render the email preview mode.
* @param {EmailCache} mail - The email object.
* @param {Environment} env - The environment object.
* @returns {Promise<TelegramSendMessageRequest>} The rendered email list mode object.
* @returns {Promise<SendMessageParams>} The rendered email list mode object.
*/
// eslint-disable-next-line unused-imports/no-unused-vars
export async function renderEmailPreviewMode(mail, env) {
Expand All @@ -138,7 +138,7 @@ export async function renderEmailPreviewMode(mail, env) {
* Render the email summary mode.
* @param {EmailCache} mail - The email object.
* @param {Environment} env - The environment object.
* @returns {Promise<TelegramSendMessageRequest>} The rendered email list mode object.
* @returns {Promise<SendMessageParams>} The rendered email list mode object.
*/
export async function renderEmailSummaryMode(mail, env) {
let {
Expand All @@ -160,7 +160,7 @@ export async function renderEmailSummaryMode(mail, env) {
* Render the email debug mode.
* @param {EmailCache} mail - The email object.
* @param {Environment} env - The environment object.
* @returns {Promise<TelegramSendMessageRequest>} The rendered email list mode object.
* @returns {Promise<SendMessageParams>} The rendered email list mode object.
*/
export async function renderEmailDebugMode(mail, env) {
const addresses = [
Expand Down
10 changes: 5 additions & 5 deletions src/telegram.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { renderEmailDebugMode, renderEmailListMode, renderEmailPreviewMode, renderEmailSummaryMode } from './render.js';
import { loadMailCache } from './dao.js';

import 'telegram-bot-api-jsdoc';
import './types.js';

const TmaModeDescription = {
Expand Down Expand Up @@ -49,7 +49,7 @@ export async function sendMailToTelegram(mail, env) {

/**
* @typedef {Function} TelegramMessageHandler
* @param {TelegramMessage} message - The Telegram message object.
* @param {Message} message - The Telegram message object.
* @returns {Promise<Response>} The fetch response.
*/

Expand All @@ -59,7 +59,7 @@ export async function sendMailToTelegram(mail, env) {

/**
* Handles the incoming Telegram command
* @param {TelegramMessage} message - The Telegram message object.
* @param {Message} message - The Telegram message object.
* @param {object} env - The environment object.
* @returns {Promise<Response>|null} The fetch response.
*/
Expand Down Expand Up @@ -134,7 +134,7 @@ function handleOpenTMACommand(env, mode, text) {

/**
* Handles the incoming Telegram callback.
* @param {TelegramCallbackQuery} callback - The Telegram callback object.
* @param {CallbackQuery} callback - The Telegram callback object.
* @param {object} env - The environment object.
* @returns {Promise<void>} The fetch response.
*/
Expand Down Expand Up @@ -206,7 +206,7 @@ async function telegramCallbackHandler(callback, env) {
*/
export async function telegramWebhookHandler(req, env) {
/**
* @type {TelegramWebhookRequest}
* @type {Update}
*/
const body = await req.json();
if (body?.message) {
Expand Down
39 changes: 0 additions & 39 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,6 @@
* @property {string} [DEBUG] - Whether to enable debug mode.
*/

/**
* @typedef {object} TelegramMessage
* @property {object} chat - The chat object.
* @property {number| string} chat.id - The chat ID.
* @property {object} from - The sender object.
* @property {number | string} from.id - The sender ID.
* @property {number} message_id - The message ID.
* @property {string} text - The text content of the message.
*/

/**
* @typedef {object} TelegramCallbackQuery
* @property {string} id - The callback query ID.
* @property {string} data - The callback data.
* @property {TelegramMessage} message - The message object.
*/

/**
* @typedef {object} TelegramWebhookRequest
* @property {TelegramMessage} [message] - The message object.
* @property {TelegramCallbackQuery} [callback_query] - The callback query object.
*/

/**
* @typedef {object} EmailCache
* @property {string} id - The ID of the email cache.
Expand All @@ -78,19 +55,3 @@
* @property {string} [html] - The HTML content of the email.
* @property {string} [text] - The text content of the email.
*/

/**
* @typedef {object} TelegramInlineKeyboardButton
* @property {string} text - The text of the button.
* @property {string} [callback_data] - The callback data of the button.
* @property {string} [url] - The URL of the button.
*/

/**
* @typedef {object} TelegramSendMessageRequest
* @property {string|number} chat_id - The chat ID.
* @property {string} text - The text content of the message.
* @property {boolean} disable_web_page_preview - Whether to disable the web page preview.
* @property {object} reply_markup - The reply markup.
* @property {TelegramInlineKeyboardButton[][]} reply_markup.inline_keyboard - The inline keyboard.
*/
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2763,6 +2763,11 @@ tapable@^2.2.0:
resolved "https://registry.yarnpkg.com/tapable/-/tapable-2.2.1.tgz#1967a73ef4060a82f12ab96af86d52fdb76eeca0"
integrity sha512-GNzQvQTOIP6RyTfE2Qxb8ZVlNmw0n88vp1szwWRimP02mnTsx3Wtn5qRdqY9w2XduFNUgvOwhNnQsjwCp+kqaQ==

telegram-bot-api-jsdoc@^7.11.0:
version "7.11.0"
resolved "https://registry.yarnpkg.com/telegram-bot-api-jsdoc/-/telegram-bot-api-jsdoc-7.11.0.tgz#0c56dd596c7ce6f3c8daa5fe1b32377019b98dbd"
integrity sha512-YkxKikDe+15QTEJ9ct3I0F5OQUk9crEWQylcJpiZ77MZg0GaF1/n4QvDODcbNgJKiCGFyxcj7nBmmtP/My85wQ==

text-table@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4"
Expand Down

0 comments on commit 191bf0f

Please sign in to comment.