import AxiosError from 'axios-error' ; import axios , { AxiosInstance } from 'axios' ; import difference from 'lodash/difference' ; import invariant from 'ts-invariant' ; import isPlainObject from 'lodash/isPlainObject' ; import pick from 'lodash/pick' ; import warning from 'warning' ; import { OnRequestFunction , camelcaseKeysDeep , createRequestInterceptor , snakecase , snakecaseKeysDeep , } from 'messaging-api-common' ; import * as TelegramTypes from './TelegramTypes' ; export default class TelegramClient { deprecated static connect ( config ) { warning ( false , '`TelegramClient.connect(...)` is deprecated. Use `new TelegramClient(...)` instead.' ) ; return new TelegramClient ( config ) ; } /**
     * The underlying axios instance.
     */ readonly axios : AxiosInstance ; /**
     * The access token used by the client.
     */ readonly accessToken : string ; /**
     * The callback to be called when receiving requests.
     */ private onRequest ? : OnRequestFunction ; constructor ( config ) { invariant ( typeof config !== 'string' , `TelegramClient: do not allow constructing client with ${ config } string. Use object instead.` ) ; this . accessToken = config . accessToken ; this . onRequest = config . onRequest ; const { origin } = config ; this . axios = axios . create ( { baseURL : `${ origin || 'https://api.telegram.org' }/bot${ this . accessToken }/` , headers : { 'Content-Type' : 'application/json' , } , } ) ; this . axios . interceptors . request . use ( createRequestInterceptor ( { onRequest : this . onRequest , } ) ) ; } private async request ( path , body = { } ) { try { const response = await this . axios . post ( path , snakecaseKeysDeep ( body ) ) ; const { data , config , request } = response ; if ( ! data . ok ) { throw new AxiosError ( `Telegram API - ${ data . description || '' }` , { config , request , response , } ) ; } if ( isPlainObject ( data . result ) || Array . isArray ( data . result ) ) { return camelcaseKeysDeep ( data . result ) ; } return data . result ; } catch ( err ) { if ( err . response && err . response . data ) { const { error_code , description } = err . response . data ; const msg = `Telegram API - ${ error_code } ${ description || '' }` ; throw new AxiosError ( msg , err ) ; } throw new AxiosError ( err . message , err ) ; } } private optionWithoutKeys ( option , removeKeys ) { let keys = Object . keys ( option ) ; keys = difference ( keys , removeKeys ) ; keys = difference ( keys , removeKeys . map ( ( key ) => snakecase ( key ) ) ) ; return pick ( option , keys ) ; } param options param options . offset param options . limit param options . timeout param options . allowedUpdates returns see https example getUpdates ( options ? ) { return this . request ( '/getUpdates' , { ... options , } ) ; } returns see https example getWebhookInfo ( ) { return this . request ( '/getWebhookInfo' ) ; } param url param options param options . certificate param options . maxConnections param options . allowedUpdates returns see https example setWebhook ( url , options = { } ) { const optionsWithoutCertificate = this . optionWithoutKeys ( options , [ 'certificate' , ] ) ; return this . request ( '/setWebhook' , { url , ... optionsWithoutCertificate , } ) ; } returns see https example deleteWebhook ( ) { return this . request ( '/deleteWebhook' ) ; } returns see https example getMe ( ) { return this . request ( '/getMe' ) ; } param chatId param text param options returns see https example sendMessage ( chatId , text , options ? ) { return this . request ( '/sendMessage' , { chatId , text , ... options , } ) ; } param chatId param fromChatId param messageId param options param options . disableNotification returns see https example forwardMessage ( chatId , fromChatId , messageId , options ? ) { return this . request ( '/forwardMessage' , { chatId , fromChatId , messageId , ... options , } ) ; } param chatId param photo param options returns see https example sendPhoto ( chatId , photo , options = { } ) { return this . request ( '/sendPhoto' , { chatId , photo , ... options , } ) ; } param chatId param audio param options returns see https example sendAudio ( chatId , audio , options = { } ) { const optionsWithoutThumb = pick ( options , [ 'caption' , 'parse_mode' , 'parseMode' , 'duration' , 'performer' , 'title' , 'disable_notification' , 'disableNotification' , 'reply_to_message_id' , 'replyToMessageId' , 'reply_markup' , 'replyMarkup' , ] ) ; return this . request ( '/sendAudio' , { chatId , audio , ... optionsWithoutThumb , } ) ; } param chatId param document param options returns see https example sendDocument ( chatId , document , options = { } ) { const optionsWithoutThumb = this . optionWithoutKeys ( options , [ 'thumb' ] ) ; return this . request ( '/sendDocument' , { chatId , document , ... optionsWithoutThumb , } ) ; } param chatId param video param options returns see https example sendVideo ( chatId , video , options = { } ) { const optionsWithoutThumb = this . optionWithoutKeys ( options , [ 'thumb' ] ) ; return this . request ( '/sendVideo' , { chatId , video , ... optionsWithoutThumb , } ) ; } param chatId param animation param options returns see https example sendAnimation ( chatId , animation , options = { } ) { const optionsWithoutThumb = this . optionWithoutKeys ( options , [ 'thumb' ] ) ; return this . request ( '/sendAnimation' , { chatId , animation , ... optionsWithoutThumb , } ) ; } param chatId param voice param options returns see https example sendVoice ( chatId , voice , options = { } ) { return this . request ( '/sendVoice' , { chatId , voice , ... options , } ) ; } param chatId param videoNote param options returns see https example sendVideoNote ( chatId , videoNote , options = { } ) { const optionsWithoutThumb = this . optionWithoutKeys ( options , [ 'thumb' ] ) ; return this . request ( '/sendVideoNote' , { chatId , videoNote , ... optionsWithoutThumb , } ) ; } param chatId param media param options returns see https example sendMediaGroup ( chatId , media , options ? ) { const mediaWithoutThumb = media . map ( ( m ) => this . optionWithoutKeys ( m , [ 'thumb' ] ) ) ; return this . request ( '/sendMediaGroup' , { chatId , media : mediaWithoutThumb , ... options , } ) ; } param chatId param location param location . latitude param location . longitude param options returns see https example sendLocation ( chatId , location , options ? ) { return this . request ( '/sendLocation' , { chatId , latitude : location . latitude , longitude : location . longitude , ... options , } ) ; } param location param location . latitude param location . longitude param options param options . chatId param options . messageId param options . inlineMessageId param options . replyMarkup see https example editMessageLiveLocation ( location , options ) { return this . request ( '/editMessageLiveLocation' , { latitude : location . latitude , longitude : location . longitude , ... options , } ) ; } param options param options . chatId param options . messageId param options . inlineMessageId param options . replyMarkup see https example stopMessageLiveLocation ( options ) { return this . request ( '/stopMessageLiveLocation' , { ... options , } ) ; } param chatId param venue param venue . latitude param venue . longitude param venue . title param venue . address param options returns see https example sendVenue ( chatId , venue , options ? ) { return this . request ( '/sendVenue' , { chatId , ... venue , ... options , } ) ; } param chatId param requiredOption . phoneNumber param requiredOption . firstName param options returns see https example sendContact ( chatId , requiredOption , options ? ) { return this . request ( '/sendContact' , { chatId , ... requiredOption , ... options , } ) ; } param chatId param question param options param otherOptions returns see https example sendPoll ( chatId , question , options , otherOptions ? ) { return this . request ( '/sendPoll' , { chatId , question , options , ... otherOptions , } ) ; } param chatId param action param Returns see https example sendChatAction ( chatId , action ) { return this . request ( '/sendChatAction' , { chatId , action , } ) ; } param userId param options returns see https example getUserProfilePhotos ( userId , options ? ) { return this . request ( '/getUserProfilePhotos' , { userId , ... options , } ) ; } param fileId returns see https example getFile ( fileId ) { return this . request ( '/getFile' , { fileId , } ) ; } param fileId returns see https example getFileLink ( fileId ) { return this . getFile ( fileId ) . then ( ( result ) => `https://api.telegram.org/file/bot${ this . accessToken }/${ result . filePath }` ) ; } param chatId param userId param options . untilDate returns see https example kickChatMember ( chatId , userId , options ? ) { return this . request ( '/kickChatMember' , { chatId , userId , ... options , } ) ; } param chatId param userId returns see https example unbanChatMember ( chatId , userId ) { return this . request ( '/unbanChatMember' , { chatId , userId , } ) ; } param chatId param userId param permissions param options param options . untilDate returns see https example restrictChatMember ( chatId , userId , permissions , options ? ) { return this . request ( '/restrictChatMember' , { chatId , userId , permissions , ... options , } ) ; } param chatId param userId param options returns see https example promoteChatMember ( chatId , userId , options ? ) { return this . request ( '/promoteChatMember' , { chatId , userId , ... options , } ) ; } param chatId param permissions returns see https example setChatPermissions ( chatId , permissions ) { return this . request ( '/setChatPermissions' , { chatId , permissions , } ) ; } param chatId returns see https example exportChatInviteLink ( chatId ) { return this . request ( '/exportChatInviteLink' , { chatId , } ) ; } param chatId param photo returns see https param chatId returns see https example deleteChatPhoto ( chatId ) { return this . request ( '/deleteChatPhoto' , { chatId , } ) ; } param chatId param title returns see https example setChatTitle ( chatId , title ) { return this . request ( '/setChatTitle' , { chatId , title , } ) ; } param chatId param description returns see https example setChatDescription ( chatId , description ) { return this . request ( '/setChatDescription' , { chatId , description , } ) ; } param chatId param messageId param options returns see https example pinChatMessage ( chatId , messageId , options ? ) { return this . request ( '/pinChatMessage' , { chatId , messageId , ... options , } ) ; } param chatId returns see https example unpinChatMessage ( chatId ) { return this . request ( '/unpinChatMessage' , { chatId , } ) ; } param chatId returns see https example leaveChat ( chatId ) { return this . request ( '/leaveChat' , { chatId , } ) ; } param chatId return see https example getChat ( chatId ) { return this . request ( '/getChat' , { chatId , } ) ; } param chatId returns see https example getChatAdministrators ( chatId ) { return this . request ( '/getChatAdministrators' , { chatId , } ) ; } param chatId returns see https example getChatMembersCount ( chatId ) { return this . request ( '/getChatMembersCount' , { chatId , } ) ; } param chatId param userId returns see https example getChatMember ( chatId , userId ) { return this . request ( '/getChatMember' , { chatId , userId , } ) ; } param chatId param stickerSetName returns see https example setChatStickerSet ( chatId , stickerSetName ) { return this . request ( '/setChatStickerSet' , { chatId , stickerSetName , } ) ; } param chatId returns see https example deleteChatStickerSet ( chatId ) { return this . request ( '/deleteChatStickerSet' , { chatId , } ) ; } param callbackQueryId param options see https example answerCallbackQuery ( callbackQueryId , options ) { return this . request ( '/answerCallbackQuery' , { callbackQueryId , ... options , } ) ; } param text param options param options . chatId param options . messageId param options . inlineMessageId returns see https example editMessageText ( text , options ? ) { return this . request ( '/editMessageText' , { text , ... options , } ) ; } param caption param options param options . chatId param options . messageId param options . inlineMessageId returns see https example editMessageCaption ( caption , options ? ) { return this . request ( '/editMessageCaption' , { caption , ... options , } ) ; } param media param options see https example editMessageMedia ( media , options ) { return this . request ( '/editMessageMedia' , { media , ... options , } ) ; } param replyMarkup param options param options . chatId param options . messageId param options . inlineMessageId returns see https example editMessageReplyMarkup ( replyMarkup , options ? ) { return this . request ( '/editMessageReplyMarkup' , { replyMarkup , ... options , } ) ; } param chatId param messageId param options returns see https example stopPoll ( chatId , messageId , options ? ) { return this . request ( '/stopPoll' , { chatId , messageId , ... options , } ) ; } param chatId param messageId returns see https example deleteMessage ( chatId , messageId ) { return this . request ( '/deleteMessage' , { chatId , messageId , } ) ; } param chatId param sticker param options returns see https example sendSticker ( chatId , sticker , options ? ) { return this . request ( '/sendSticker' , { chatId , sticker , ... options , } ) ; } param name returns see https example getStickerSet ( name ) { return this . request ( '/getStickerSet' , { name } ) ; } see https param userId param name param title param pngSticker returns see https example createNewStickerSet ( userId , name , title , pngSticker , emojis , options ? ) { return this . request ( '/createNewStickerSet' , { userId , name , title , pngSticker , emojis , ... options , } ) ; } param userId param name param pngSticker param emojis param options returns see https example addStickerToSet ( userId , name , pngSticker , emojis , options ? ) { return this . request ( '/addStickerToSet' , { userId , name , pngSticker , emojis , ... options , } ) ; } param sticker param position returns see https example setStickerPositionInSet ( sticker , position ) { return this . request ( 'setStickerPositionInSet' , { sticker , position , } ) ; } param sticker returns see https example deleteStickerFromSet ( sticker ) { return this . request ( '/deleteStickerFromSet' , { sticker } ) ; } param inlineQueryId param results param options returns see https example answerInlineQuery ( inlineQueryId , results , options ? ) { return this . request ( '/answerInlineQuery' , { inlineQueryId , results , ... options , } ) ; } param chatId param product . title param product . description param product . payload param product . providerToken param product . startParameter param product . currency param product . prices param options returns see https example sendInvoice ( chatId , product , options ? ) { return this . request ( '/sendInvoice' , { chatId , ... product , ... options , } ) ; } param shippingQueryId param ok param options see https example answerShippingQuery ( shippingQueryId , ok , options ? ) { return this . request ( '/answerShippingQuery' , { shippingQueryId , ok , ... options , } ) ; } param preCheckoutQueryId param ok param options returns see https example answerPreCheckoutQuery ( preCheckoutQueryId , ok , options ? ) { return this . request ( '/answerPreCheckoutQuery' , { preCheckoutQueryId , ok , ... options , } ) ; } see https param chatId param gameShortName param options returns see https example sendGame ( chatId , gameShortName , options ? ) { return this . request ( '/sendGame' , { chatId , gameShortName , ... options , } ) ; } param userId param score param options returns see https example setGameScore ( userId , score , options ? ) { return this . request ( '/setGameScore' , { userId , score , ... options , } ) ; } param userId param options returns see https example getGameHighScores ( userId , options ? ) { return this . request ( '/getGameHighScores' , { userId , ... options , } ) ; } }