Add Telegram reply button
This commit is contained in:
@@ -1169,7 +1169,10 @@ async function notifyTelegramVisitorMessage(conversation, messageId) {
|
||||
const sent = await telegramSendMessage(settings, {
|
||||
text: telegramVisitorMessageText(details.conversation, message, attachmentCount),
|
||||
replyMarkup: {
|
||||
inline_keyboard: [[{ text: "Otevrit BO", url: `${PUBLIC_BASE_URL}/admin` }]]
|
||||
inline_keyboard: [[
|
||||
{ text: "Reply", callback_data: `reply:${conversation.public_id}` },
|
||||
{ text: "Otevrit BO", url: `${PUBLIC_BASE_URL}/admin` }
|
||||
]]
|
||||
}
|
||||
});
|
||||
const telegramMessageId = sent?.result?.message_id;
|
||||
@@ -1183,6 +1186,10 @@ async function notifyTelegramVisitorMessage(conversation, messageId) {
|
||||
}
|
||||
|
||||
async function handleTelegramUpdate(update, settings) {
|
||||
if (update?.callback_query) {
|
||||
await handleTelegramCallback(update.callback_query, settings);
|
||||
return;
|
||||
}
|
||||
const message = update?.message;
|
||||
if (!message || !message.text || !message.reply_to_message) return;
|
||||
const chatId = String(message.chat?.id || "");
|
||||
@@ -1212,6 +1219,28 @@ async function handleTelegramUpdate(update, settings) {
|
||||
await telegramSendMessage(settings, { chatId, text: "MaalFlows: odpoved odeslana zakaznikovi." });
|
||||
}
|
||||
|
||||
async function handleTelegramCallback(callbackQuery, settings) {
|
||||
const data = String(callbackQuery?.data || "");
|
||||
if (!data.startsWith("reply:")) return;
|
||||
const chatId = String(callbackQuery.message?.chat?.id || "");
|
||||
if (String(settings.chatId) && chatId !== String(settings.chatId)) return;
|
||||
const conversation = findConversation(data.slice("reply:".length));
|
||||
await telegramAnswerCallback(settings, callbackQuery.id, conversation ? "Napis odpoved." : "Konverzace nenalezena.");
|
||||
if (!conversation) return;
|
||||
const sent = await telegramSendMessage(settings, {
|
||||
chatId,
|
||||
text: "Napis odpoved pro zakaznika:",
|
||||
replyMarkup: { force_reply: true, selective: true }
|
||||
});
|
||||
const promptMessageId = sent?.result?.message_id;
|
||||
if (!promptMessageId) return;
|
||||
db.prepare(`
|
||||
INSERT OR REPLACE INTO telegram_message_links
|
||||
(site_id, conversation_id, telegram_chat_id, telegram_message_id, source_message_id)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
`).run(conversation.site_id, conversation.id, chatId, promptMessageId, null);
|
||||
}
|
||||
|
||||
async function telegramSendMessage(settings, options) {
|
||||
const token = settings.botToken;
|
||||
const chatId = options.chatId || settings.chatId;
|
||||
@@ -1232,6 +1261,23 @@ async function telegramSendMessage(settings, options) {
|
||||
return data;
|
||||
}
|
||||
|
||||
async function telegramAnswerCallback(settings, callbackQueryId, text) {
|
||||
if (!callbackQueryId) return;
|
||||
const response = await fetch(`https://api.telegram.org/bot${settings.botToken}/answerCallbackQuery`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
callback_query_id: callbackQueryId,
|
||||
text
|
||||
})
|
||||
});
|
||||
const data = await response.json().catch(() => ({}));
|
||||
if (!response.ok || data.ok === false) {
|
||||
throw new Error(data.description || `Telegram callback HTTP ${response.status}`);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
async function telegramSetWebhook(settings) {
|
||||
const response = await fetch(`https://api.telegram.org/bot${settings.botToken}/setWebhook`, {
|
||||
method: "POST",
|
||||
|
||||
Reference in New Issue
Block a user