Skip to content

Commit

Permalink
doc: 添加telegram文件相关文档
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Aug 1, 2024
1 parent b24c8b5 commit 8e2362c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
11 changes: 6 additions & 5 deletions src/telegram/message.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {CONST, DATABASE, ENV} from '../config/env.js';
import {Context} from '../config/context.js';
import {getBot, sendMessageToTelegramWithContext} from './telegram.js';
import {getBot, filesListToUrl, sendMessageToTelegramWithContext} from './telegram.js';
import {handleCommandMessage} from './command.js';
import {errorToString} from '../utils/utils.js';
import {chatWithLLM} from '../agent/llm.js';
Expand Down Expand Up @@ -256,7 +256,8 @@ async function msgChatWithLLM(message, context) {
if (ENV.EXTRA_MESSAGE_CONTEXT && context.SHARE_CONTEXT.extraMessageContext && context.SHARE_CONTEXT.extraMessageContext.text) {
content = context.SHARE_CONTEXT.extraMessageContext.text + '\n' + text;
}
return chatWithLLM({message: content}, context, null);
const params = { message: content}
return chatWithLLM(params, context, null);
}


Expand Down Expand Up @@ -305,14 +306,14 @@ export async function handleMessage(request) {
msgInitChatContext,
// 检查环境是否准备好: DATABASE
msgCheckEnvIsReady,
// 过滤非白名单用户
msgFilterWhiteList,
// DEBUG: 保存最后一条消息
msgSaveLastMessage,
// 过滤不支持的消息(抛出异常结束消息处理:当前只支持文本消息)
// 过滤不支持的消息(抛出异常结束消息处理)
msgFilterUnsupportedMessage,
// 处理群消息,判断是否需要响应此条消息
msgHandleGroupMessage,
// 过滤非白名单用户
msgFilterWhiteList,
// 忽略旧消息
msgIgnoreOldMessage,
// 处理命令消息
Expand Down
21 changes: 21 additions & 0 deletions src/telegram/telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,24 @@ export async function getBot(token) {
return resp;
}
}

/**
* 获取文件链接
* @param {string} fileId
* @param {string} token
* @returns {string}
*/
export function getFileLink(fileId, token) {
return `${ENV.TELEGRAM_API_DOMAIN}/file/bot${token}/${fileId}`;
}

/**
*
* @param {TelegramBaseFile[]} files
* @param {string} token
* @returns {string[]}
*/
export function filesListToUrl(files, token) {
return Array.from((new Set(files.map((file) => file.file_id))))
.map((fileId) => getFileLink(fileId, token));
}
2 changes: 1 addition & 1 deletion src/types/agent.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @typedef {object} LlmRequestParams
* @property {?string} [message] - 输入文本
* @property {string} [image] - 图片
* @property {string | string[]} [image] - 图片
* @property {string} [audio] - 音频
*/

Expand Down
3 changes: 0 additions & 3 deletions src/types/context.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
* @typedef {(string|number)} TelegramID
*/

/**
* 用于保存用户配置
Expand Down
29 changes: 26 additions & 3 deletions src/types/telegram.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
/**
* @typedef {(string|number)} TelegramID
*/


/**
* @typedef {Object} TelegramBaseFile
* @property {string} file_id - Unique identifier for this file.
* @property {string} file_unique_id - Unique identifier for this file, which is supposed to be the same over time and for different bots.
* @property {number} file_size - Optional. File size, if known.
*
* @typedef {TelegramBaseFile} TelegramPhoto
* @property {number} width - Photo width.
* @property {number} height - Photo height.
*
* @typedef {TelegramBaseFile} TelegramVoice
* @property {number} duration - Duration of the audio in seconds.
* @property {string} mime_type - Optional. MIME type of the file as defined by sender.
*/


/**
* @typedef {Object} TelegramUser
* @property {number} id - The ID of the user.
* @property {TelegramID} id - The ID of the user.
* @property {boolean} is_bot - True, if the user is a bot.
* @property {string} first_name - The first name of the user.
* @property {string} [last_name] - The last name of the user.
Expand All @@ -10,7 +31,7 @@

/**
* @typedef {Object} TelegramChat
* @property {number} id - The ID of the chat.
* @property {TelegramID} id - The ID of the chat.
* @property {string} type - The type of the chat.
* @property {boolean} is_forum - True, if the chat is a forum.
*/
Expand All @@ -32,6 +53,8 @@
* @property {number} date - The date the message was sent.
* @property {?string} [text] - The text of the message.
* @property {?string} [caption] - The caption of the message.
* @property {TelegramPhoto[]} [photo] - An array of photos.
* @property {TelegramVoice} [voice] - The voice message.
* @property {TelegramMessageEntity[]} [entities] - An array of message entities.
* @property {TelegramMessage} [reply_to_message] - The message that this message is a reply to.
* @property {boolean} is_topic_message - True, if the message is a topic message.
Expand All @@ -40,7 +63,7 @@

/**
* @typedef {Object} TelegramWebhookRequest
* @property {number} update_id - The update's unique identifier.
* @property {TelegramID} update_id - The update's unique identifier.
* @property {TelegramMessage} message - The message
* @property {TelegramMessage} edited_message - The edited message
*/

0 comments on commit 8e2362c

Please sign in to comment.