eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
2 lines • 4.6 kB
JavaScript
import{isObject}from"#shared/guards.js";import{parseJsonObject}from"#shared/json.js";import{parseTelegramChatType}from"#public/channels/telegram/inbound.js";const TELEGRAM_MESSAGE_TEXT_MAX_LENGTH=4096;function telegramContinuationToken(e){let t=e.messageThreadId===void 0?``:String(e.messageThreadId),n=e.conversationId===void 0?``:String(e.conversationId);return`${String(e.chatId)}:${t}:${n}`}async function resolveTelegramBotToken(e){let t=e??process.env.TELEGRAM_BOT_TOKEN;if(!t)throw Error(`TELEGRAM_BOT_TOKEN is required.`);return typeof t==`function`?await t():t}async function callTelegramApi(e){let t=e.fetch??fetch,n=await resolveTelegramBotToken(e.botToken),r={headers:{"content-type":`application/json; charset=utf-8`},method:`POST`};e.body!==void 0&&(r.body=JSON.stringify(parseJsonObject(e.body)));let i=await t(`${e.apiBaseUrl??`https://api.telegram.org`}/bot${n}/${encodeURIComponent(e.method)}`,r);return{body:await parseResponseBody(i),ok:i.ok,status:i.status}}async function sendTelegramMessage(e){let t=await callTelegramApi({apiBaseUrl:e.apiBaseUrl,body:normalizeTelegramMessageBody(e.body,e.chatId),botToken:e.credentials?.botToken,fetch:e.fetch,method:`sendMessage`});if(!t.ok)throw Error(`Telegram sendMessage failed with HTTP ${t.status}: ${formatTelegramErrorBody(t.body)}`);return toTelegramMessageResult(t.body)}async function sendTelegramChatAction(e){return callTelegramApi({apiBaseUrl:e.apiBaseUrl,body:parseJsonObject({action:e.action,chat_id:e.chatId,message_thread_id:e.messageThreadId}),botToken:e.credentials?.botToken,fetch:e.fetch,method:`sendChatAction`})}async function answerTelegramCallbackQuery(e){return callTelegramApi({apiBaseUrl:e.apiBaseUrl,body:parseJsonObject({callback_query_id:e.callbackQueryId,show_alert:e.showAlert,text:e.text}),botToken:e.credentials?.botToken,fetch:e.fetch,method:`answerCallbackQuery`})}async function editTelegramMessageText(e){return callTelegramApi({apiBaseUrl:e.apiBaseUrl,body:parseJsonObject({chat_id:e.chatId,message_id:Number(e.messageId),reply_markup:e.replyMarkup,text:e.text}),botToken:e.credentials?.botToken,fetch:e.fetch,method:`editMessageText`})}async function editTelegramMessageReplyMarkup(e){return callTelegramApi({apiBaseUrl:e.apiBaseUrl,body:parseJsonObject({chat_id:e.chatId,message_id:Number(e.messageId),reply_markup:e.replyMarkup}),botToken:e.credentials?.botToken,fetch:e.fetch,method:`editMessageReplyMarkup`})}async function getTelegramFile(e){let t=await callTelegramApi({apiBaseUrl:e.apiBaseUrl,body:{file_id:e.fileId},botToken:e.credentials?.botToken,fetch:e.fetch,method:`getFile`});if(!t.ok)throw Error(`Telegram getFile failed with HTTP ${t.status}.`);let n=isObject(t.body)?t.body:{},r=isObject(n.result)?n.result:{};if(typeof r.file_path!=`string`||r.file_path.length===0)throw Error(`Telegram getFile response did not include result.file_path.`);return{filePath:r.file_path,raw:t.body}}async function downloadTelegramFile(e){let t=e.fetch??fetch,n=await resolveTelegramBotToken(e.credentials?.botToken);return t(`${e.fileBaseUrl??e.apiBaseUrl??`https://api.telegram.org`}/file/bot${n}/${e.filePath}`)}function splitTelegramMessageText(e){if(e.length<=4096)return[e];let t=[],n=e;for(;n.length>TELEGRAM_MESSAGE_TEXT_MAX_LENGTH;){let e=n.lastIndexOf(`
`,TELEGRAM_MESSAGE_TEXT_MAX_LENGTH);e<=0&&(e=n.lastIndexOf(` `,TELEGRAM_MESSAGE_TEXT_MAX_LENGTH)),e<=0&&(e=TELEGRAM_MESSAGE_TEXT_MAX_LENGTH),t.push(n.slice(0,e).trimEnd()),n=n.slice(e).trimStart()}return t.push(n),t}function normalizeTelegramMessageBody(e,t){return parseJsonObject({...e,chat_id:t})}function toTelegramMessageResult(e){let t=isObject(e)?e:{},n=isObject(t.result)?t.result:{},r=isObject(n.chat)?n.chat:{};return{chatId:typeof r.id==`number`||typeof r.id==`string`?String(r.id):void 0,chatType:parseTelegramChatType(r.type)??void 0,id:typeof n.message_id==`number`||typeof n.message_id==`string`?String(n.message_id):``,raw:e}}function formatTelegramErrorBody(e){if(typeof e==`string`)return e;if(typeof e==`object`&&e){let t=e.description;if(typeof t==`string`&&t.length>0)return t}try{let t=JSON.stringify(e);return t===void 0||t.length===0?`empty response body`:t}catch{return`unserializable response body`}}async function parseResponseBody(e){let t=await e.text();if(!t)return null;try{return JSON.parse(t)}catch{return t}}export{TELEGRAM_MESSAGE_TEXT_MAX_LENGTH,answerTelegramCallbackQuery,callTelegramApi,downloadTelegramFile,editTelegramMessageReplyMarkup,editTelegramMessageText,getTelegramFile,resolveTelegramBotToken,sendTelegramChatAction,sendTelegramMessage,splitTelegramMessageText,telegramContinuationToken};