Skip to content

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

Package listing

@kody/telegram

src/get-me.ts

41 lines · 1.2 KB · TypeScript
import { inputRecord, secretNameFrom } from './validation.ts'
import { telegramRequest } from './request.ts'

export type TelegramUser = {
	id?: number
	is_bot?: boolean
	first_name?: string
	username?: string
	can_join_groups?: boolean
	can_read_all_group_messages?: boolean
	supports_inline_queries?: boolean
	[key: string]: unknown
}

export function summarizeBot(bot: TelegramUser) {
	return {
		id: typeof bot.id === 'number' ? bot.id : null,
		isBot: bot.is_bot === true,
		hasUsername: typeof bot.username === 'string' && bot.username.length > 0,
		username: typeof bot.username === 'string' ? bot.username : null,
		firstName: typeof bot.first_name === 'string' ? bot.first_name : null,
		canJoinGroups: bot.can_join_groups === true,
		canReadAllGroupMessages: bot.can_read_all_group_messages === true,
		supportsInlineQueries: bot.supports_inline_queries === true,
	}
}

/** Return the authenticated bot identity via getMe. */
export default async function getMe(params: Record<string, unknown> = {}) {
	const input = inputRecord(params)
	const secretName = secretNameFrom(input)
	const bot = await telegramRequest<TelegramUser>({
		method: 'getMe',
		secretName,
	})
	return {
		ok: true,
		secretName,
		bot: summarizeBot(bot),
	}
}