Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kody/telegram

src/get-chat.ts

44 lines · 1.4 KB · TypeScript
import { telegramRequest } from './request.ts'
import { inputRecord, requiredChatId, secretNameFrom } from './validation.ts'

type TelegramChat = {
	id?: number
	type?: string
	title?: string
	username?: string
	first_name?: string
	last_name?: string
	description?: string
	invite_link?: string
	is_forum?: boolean
	permissions?: Record<string, unknown>
	[key: string]: unknown
}

/** Read one chat the bot can see (getChat). */
export default async function getChat(params: Record<string, unknown>) {
	const input = inputRecord(params)
	const chatId = requiredChatId(input)
	const secretName = secretNameFrom(input)
	const chat = await telegramRequest<TelegramChat>({
		method: 'getChat',
		secretName,
		params: { chat_id: chatId },
	})

	return {
		ok: true,
		chat: {
			id: typeof chat.id === 'number' ? chat.id : null,
			type: typeof chat.type === 'string' ? chat.type : null,
			title: typeof chat.title === 'string' ? chat.title : null,
			username: typeof chat.username === 'string' ? chat.username : null,
			firstName: typeof chat.first_name === 'string' ? chat.first_name : null,
			lastName: typeof chat.last_name === 'string' ? chat.last_name : null,
			description: typeof chat.description === 'string' ? chat.description : null,
			isForum: chat.is_forum === true,
			hasInviteLink: typeof chat.invite_link === 'string',
			permissions: chat.permissions ?? null,
		},
	}
}