Skip to content

Commit

Permalink
perf: 使用最新telegram-bot-api-types简化返回值转换
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Aug 24, 2024
1 parent d1130f3 commit 0e5dd70
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 20 deletions.
2 changes: 1 addition & 1 deletion dist/buildinfo.json

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

19 changes: 13 additions & 6 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/timestamp

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"eslint": "^9.8.0",
"eslint-plugin-format": "^0.1.2",
"rollup-plugin-cleanup": "^3.2.1",
"telegram-bot-api-types": "^7.9.3",
"telegram-bot-api-types": "^7.9.7",
"typescript": "^5.5.4",
"vite": "^5.2.10",
"vite-plugin-checker": "^0.7.2",
Expand Down
8 changes: 8 additions & 0 deletions src/telegram/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class APIClientBase {
}
return this.jsonRequest(method, params);
}

async requestJSON<T, R>(method: Telegram.BotMethod, params: T): Promise<R> {
return this.request(method, params).then(res => res.json() as R);
}
}

export type TelegramBotAPI = APIClientBase & Telegram.AllBotMethods;
Expand All @@ -60,6 +64,10 @@ export function createTelegramBotAPI(token: string): TelegramBotAPI {
return Reflect.get(target, prop, receiver);
}
return (...args: any[]) => {
if (typeof prop === 'string' && prop.endsWith('WithReturns')) {
const method = prop.slice(0, -11) as Telegram.BotMethod;
return Reflect.apply(target.requestJSON, target, [method, ...args]);
}
return Reflect.apply(target.request, target, [prop as Telegram.BotMethod, ...args]);
};
},
Expand Down
6 changes: 3 additions & 3 deletions src/telegram/command/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ export async function loadChatRoleWithContext(message: Telegram.Message, context
return null;
}

let groupAdmin: Telegram.ChatMemberAdministrator[] | null = null;
let groupAdmin: Telegram.ChatMember[] | null = null;
try {
groupAdmin = JSON.parse(await DATABASE.get(groupAdminsKey));
} catch (e) {
console.error(e);
}
if (groupAdmin === null || !Array.isArray(groupAdmin) || groupAdmin.length === 0) {
const api = createTelegramBotAPI(context.SHARE_CONTEXT.botToken);
const result = await api.getChatAdministrators({ chat_id: chatId }).then(res => res.json()).catch(() => null) as Telegram.ResponseSuccess<Telegram.ChatMemberAdministrator[]>;
const result = await api.getChatAdministratorsWithReturns({ chat_id: chatId });
if (result == null) {
return null;
}
Expand All @@ -35,7 +35,7 @@ export async function loadChatRoleWithContext(message: Telegram.Message, context
}
for (let i = 0; i < groupAdmin.length; i++) {
const user = groupAdmin[i];
if (`${user.user.id}` === `${speakerId}`) {
if (`${user.user?.id}` === `${speakerId}`) {
return user.status;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/telegram/handler/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export async function chatWithLLM(message: Telegram.Message, params: LLMChatRequ
const sender = MessageSender.from(context.SHARE_CONTEXT.botToken, message);
try {
try {
const msg = await sender.sendPlainText('...').then(r => r.json()) as Telegram.ResponseSuccess<Telegram.Message>;
const msg = await sender.sendPlainText('...').then(r => r.json()) as Telegram.ResponseWithMessage;
sender.update({
message_id: msg.result.message_id,
});
Expand Down Expand Up @@ -47,7 +47,7 @@ export async function chatWithLLM(message: Telegram.Message, params: LLMChatRequ
}
nextEnableTime = null;
if (resp.ok) {
const respJson = await resp.json() as Telegram.ResponseSuccess<Telegram.Message>;
const respJson = await resp.json() as Telegram.ResponseWithMessage;
sender.update({
message_id: respJson.result.message_id,
});
Expand Down Expand Up @@ -97,7 +97,7 @@ export class ChatHandler implements MessageHandler {
if (message.photo && message.photo.length > 0) {
const id = findPhotoFileID(message.photo, ENV.TELEGRAM_PHOTO_SIZE_OFFSET);
const api = createTelegramBotAPI(context.SHARE_CONTEXT.botToken);
const file = await api.getFile({ file_id: id }).then(res => res.json()) as Telegram.ResponseSuccess<Telegram.File>;
const file = await api.getFileWithReturns({ file_id: id });
let url = file.result.file_path;
if (url) {
if (ENV.TELEGRAPH_ENABLE) {
Expand Down
2 changes: 1 addition & 1 deletion src/telegram/handler/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class GroupMention implements MessageHandler {
// 处理群组消息,过滤掉AT部分
let botName = context.SHARE_CONTEXT.botName;
if (!botName) {
const res = await createTelegramBotAPI(context.SHARE_CONTEXT.botToken).getMe().then(res => res.json()) as Telegram.ResponseSuccess<User>;
const res = await createTelegramBotAPI(context.SHARE_CONTEXT.botToken).getMeWithReturns();
botName = res.result.username || null;
context.SHARE_CONTEXT.botName = botName;
}
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2867,10 +2867,10 @@ tapable@^2.2.0:
resolved "https://registry.yarnpkg.com/tapable/-/tapable-2.2.1.tgz#1967a73ef4060a82f12ab96af86d52fdb76eeca0"
integrity sha512-GNzQvQTOIP6RyTfE2Qxb8ZVlNmw0n88vp1szwWRimP02mnTsx3Wtn5qRdqY9w2XduFNUgvOwhNnQsjwCp+kqaQ==

telegram-bot-api-types@^7.9.3:
version "7.9.3"
resolved "https://registry.yarnpkg.com/telegram-bot-api-types/-/telegram-bot-api-types-7.9.3.tgz#10c1cb112a21411b51c53e4f5f13e4b04f1f18b4"
integrity sha512-2RaHvH9CIjIES4IBRFdEzObOmLoKPxEUhy4bYmJDIu37mK3ZD5FaDFu8mIBNb+j63nqxfnufpDEBV7c0sCchmA==
telegram-bot-api-types@^7.9.7:
version "7.9.7"
resolved "https://registry.yarnpkg.com/telegram-bot-api-types/-/telegram-bot-api-types-7.9.7.tgz#306596893b1c60d1b88ec5ae6edbacd19ae98d60"
integrity sha512-zkSmR0Sx5lMhxccw45Y4RTy1N0KD/9CF1KaQoTmRcJRCb4B/gjr+67KHdEhTraMOFERGh7zyMmMc7b9IqUUC/w==

text-table@^0.2.0:
version "0.2.0"
Expand Down

0 comments on commit 0e5dd70

Please sign in to comment.