Telegram-user: add location context
This commit is contained in:
parent
9b94a751c8
commit
f45dca1205
@ -2,7 +2,13 @@ import type { TelegramClient } from "@mtcute/node";
|
|||||||
import type { MessageContext } from "@mtcute/dispatcher";
|
import type { MessageContext } from "@mtcute/dispatcher";
|
||||||
import type { RuntimeEnv } from "clawdbot/plugin-sdk";
|
import type { RuntimeEnv } from "clawdbot/plugin-sdk";
|
||||||
|
|
||||||
import { resolveAckReaction, resolveMentionGatingWithBypass } from "clawdbot/plugin-sdk";
|
import {
|
||||||
|
formatLocationText,
|
||||||
|
resolveAckReaction,
|
||||||
|
resolveMentionGatingWithBypass,
|
||||||
|
toLocationContext,
|
||||||
|
type NormalizedLocation,
|
||||||
|
} from "clawdbot/plugin-sdk";
|
||||||
import { getTelegramUserRuntime } from "../runtime.js";
|
import { getTelegramUserRuntime } from "../runtime.js";
|
||||||
import type { CoreConfig, TelegramUserAccountConfig } from "../types.js";
|
import type { CoreConfig, TelegramUserAccountConfig } from "../types.js";
|
||||||
import { sendMediaTelegramUser, sendMessageTelegramUser } from "../send.js";
|
import { sendMediaTelegramUser, sendMessageTelegramUser } from "../send.js";
|
||||||
@ -107,6 +113,45 @@ function resolveTelegramUserGroupConfig(
|
|||||||
return { groupConfig, topicConfig };
|
return { groupConfig, topicConfig };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function extractTelegramUserLocation(
|
||||||
|
media: MessageContext["media"],
|
||||||
|
): NormalizedLocation | null {
|
||||||
|
if (!media) return null;
|
||||||
|
const typed = media as { type?: string };
|
||||||
|
if (typed.type === "venue") {
|
||||||
|
const venue = media as {
|
||||||
|
location: { latitude: number; longitude: number; radius?: number };
|
||||||
|
title: string;
|
||||||
|
address: string;
|
||||||
|
};
|
||||||
|
return {
|
||||||
|
latitude: venue.location.latitude,
|
||||||
|
longitude: venue.location.longitude,
|
||||||
|
accuracy: venue.location.radius,
|
||||||
|
name: venue.title,
|
||||||
|
address: venue.address,
|
||||||
|
source: "place",
|
||||||
|
isLive: false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (typed.type === "location" || typed.type === "live_location") {
|
||||||
|
const location = media as {
|
||||||
|
latitude: number;
|
||||||
|
longitude: number;
|
||||||
|
radius?: number;
|
||||||
|
};
|
||||||
|
const isLive = typed.type === "live_location";
|
||||||
|
return {
|
||||||
|
latitude: location.latitude,
|
||||||
|
longitude: location.longitude,
|
||||||
|
accuracy: location.radius,
|
||||||
|
source: isLive ? "live" : "pin",
|
||||||
|
isLive,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
async function resolveMediaAttachment(params: {
|
async function resolveMediaAttachment(params: {
|
||||||
client: TelegramClient;
|
client: TelegramClient;
|
||||||
mediaMaxMb: number;
|
mediaMaxMb: number;
|
||||||
@ -288,6 +333,8 @@ export function createTelegramUserMessageHandler(params: TelegramUserHandlerPara
|
|||||||
const primaryMessage =
|
const primaryMessage =
|
||||||
messageGroup.find((entry) => entry.text?.trim()) ?? msg;
|
messageGroup.find((entry) => entry.text?.trim()) ?? msg;
|
||||||
const text = primaryMessage.text?.trim() ?? "";
|
const text = primaryMessage.text?.trim() ?? "";
|
||||||
|
const locationData = extractTelegramUserLocation(primaryMessage.media);
|
||||||
|
const locationText = locationData ? formatLocationText(locationData) : undefined;
|
||||||
const allMedia = await resolveMediaAttachments({
|
const allMedia = await resolveMediaAttachments({
|
||||||
client,
|
client,
|
||||||
mediaMaxMb,
|
mediaMaxMb,
|
||||||
@ -295,7 +342,8 @@ export function createTelegramUserMessageHandler(params: TelegramUserHandlerPara
|
|||||||
runtime,
|
runtime,
|
||||||
});
|
});
|
||||||
const media = allMedia[0] ?? null;
|
const media = allMedia[0] ?? null;
|
||||||
if (!text && !media) return;
|
const rawBody = [text, locationText].filter(Boolean).join("\n").trim();
|
||||||
|
if (!rawBody && !media) return;
|
||||||
|
|
||||||
core.channel.activity.record({
|
core.channel.activity.record({
|
||||||
channel: "telegram-user",
|
channel: "telegram-user",
|
||||||
@ -414,14 +462,14 @@ export function createTelegramUserMessageHandler(params: TelegramUserHandlerPara
|
|||||||
? buildTelegramUserGroupLabel(groupTitle, chatId, threadId)
|
? buildTelegramUserGroupLabel(groupTitle, chatId, threadId)
|
||||||
: senderName;
|
: senderName;
|
||||||
const mediaSuffix =
|
const mediaSuffix =
|
||||||
!text && allMedia.length > 1 ? ` (${allMedia.length} items)` : "";
|
!rawBody && allMedia.length > 1 ? ` (${allMedia.length} items)` : "";
|
||||||
const body = core.channel.reply.formatAgentEnvelope({
|
const body = core.channel.reply.formatAgentEnvelope({
|
||||||
channel: "Telegram User",
|
channel: "Telegram User",
|
||||||
from: senderName,
|
from: senderName,
|
||||||
timestamp: msg.date,
|
timestamp: msg.date,
|
||||||
previousTimestamp,
|
previousTimestamp,
|
||||||
envelope: envelopeOptions,
|
envelope: envelopeOptions,
|
||||||
body: text || `(media${mediaSuffix})`,
|
body: rawBody || `(media${mediaSuffix})`,
|
||||||
});
|
});
|
||||||
|
|
||||||
const ctxPayload = core.channel.reply.finalizeInboundContext({
|
const ctxPayload = core.channel.reply.finalizeInboundContext({
|
||||||
@ -455,6 +503,7 @@ export function createTelegramUserMessageHandler(params: TelegramUserHandlerPara
|
|||||||
? buildTelegramUserGroupFrom(chatId, threadId)
|
? buildTelegramUserGroupFrom(chatId, threadId)
|
||||||
: `telegram-user:${senderId}`,
|
: `telegram-user:${senderId}`,
|
||||||
WasMentioned: isGroup ? effectiveWasMentioned : undefined,
|
WasMentioned: isGroup ? effectiveWasMentioned : undefined,
|
||||||
|
...(locationData ? toLocationContext(locationData) : undefined),
|
||||||
});
|
});
|
||||||
|
|
||||||
void core.channel.session
|
void core.channel.session
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user