Fix Telegram contact avatars in CRM list
This commit is contained in:
@@ -2,6 +2,7 @@ import { createHash } from "node:crypto";
|
||||
import type { JsonObject, OmniInboundEnvelopeV1 } from "./types";
|
||||
|
||||
const MAX_TEXT_LENGTH = 4096;
|
||||
const TELEGRAM_FILE_MARKER = "tg-file:";
|
||||
|
||||
function asObject(value: unknown): JsonObject {
|
||||
return value && typeof value === "object" && !Array.isArray(value) ? (value as JsonObject) : {};
|
||||
@@ -67,6 +68,11 @@ function normalizeId(value: unknown) {
|
||||
return text || null;
|
||||
}
|
||||
|
||||
function pickTelegramChatPhotoFileId(source: JsonObject | null | undefined) {
|
||||
const photo = asObject(asObject(source).photo);
|
||||
return normalizeString(photo.small_file_id) ?? normalizeString(photo.big_file_id);
|
||||
}
|
||||
|
||||
type TelegramMediaInfo = {
|
||||
kind: "voice" | "audio" | "video_note" | null;
|
||||
fileId: string | null;
|
||||
@@ -219,6 +225,7 @@ export function parseTelegramBusinessUpdate(raw: unknown): OmniInboundEnvelopeV1
|
||||
}
|
||||
|
||||
const fallbackContactSource = contactSource === chat ? from : chat;
|
||||
const contactAvatarFileId = pickTelegramChatPhotoFileId(contactSource);
|
||||
|
||||
const threadExternalId =
|
||||
chat.id != null
|
||||
@@ -293,7 +300,7 @@ export function parseTelegramBusinessUpdate(raw: unknown): OmniInboundEnvelopeV1
|
||||
contactFirstName: normalizeString(contactSource.first_name),
|
||||
contactLastName: normalizeString(contactSource.last_name),
|
||||
contactTitle: normalizeString(contactSource.title),
|
||||
contactAvatarUrl: normalizeString(contactSource.photo_url),
|
||||
contactAvatarUrl: contactAvatarFileId ? `${TELEGRAM_FILE_MARKER}${contactAvatarFileId}` : null,
|
||||
fromUsername: typeof from.username === "string" ? from.username : null,
|
||||
fromFirstName: typeof from.first_name === "string" ? from.first_name : null,
|
||||
fromLastName: typeof from.last_name === "string" ? from.last_name : null,
|
||||
|
||||
Reference in New Issue
Block a user