diff --git a/lib/telegram-bot.d.ts b/lib/telegram-bot.d.ts new file mode 100644 index 0000000..ed976d4 --- /dev/null +++ b/lib/telegram-bot.d.ts @@ -0,0 +1,1061 @@ +import Bluebird from 'bluebird'; +import { EventEmitter } from 'events'; + +import { + CallbackQuery, + Chat, + ChatMember, + ChosenInlineResult, + File, + ForceReply, + InlineKeyboardMarkup, + InlineQuery, + InlineQueryResult, + InputFile, + Message, + ReplyKeyboardMarkup, + ReplyKeyboardRemove, + Update, + User, + UserProfilePhotos, + WebhookInfo, +} from 'telegram-typings'; + +/** + * type alias for often used ReplyMarkup data type + */ +type ReplyMarkup = InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply; + +/** + * common signature for simple api methods + */ +type ApiMethod = (callback?: (err: any, value?: T) => void) => Bluebird; + +/** + * common signature for api methods with additional parameters + */ +type ApiMethodWithParams = (params: P, callback?: (err: any, value?: T) => void) => Bluebird; + +/** + * Configuration parameters for a new telegram bot instance + */ +interface ApiParams { + /** + * access token for bot + */ + token: string; + + /** + * proxy settings (optional) + */ + http_proxy?: { + /** + * proxy host + */ + host: string; + + /** + * proxy port + */ + port: number; + + /** + * username for proxy + */ + user?: string; + + /** + * password for proxy + */ + password?: string; + + /** + * true/false whether to use https + */ + https?: boolean; + }; + + updates?: { + /** + * True if you want to receive updates from telegram (default false) + */ + enabled?: boolean; + + /** + * We will fetch updates from Telegram each number of milliseconds + * (default 1000) + */ + get_interval?: number; + + /** + * We will wait for updates during this num of milliseconds at each + * attempt before quit (default 0) + */ + pooling_timeout?: number; + }; + + webhook?: { + /** + * True if you want to receive updates via webhook (one one of + * updates.enabled or webhook.enabled could be true) + */ + enabled?: boolean; + + /** + * URL to the webhook; if not provided `host` is required + */ + url?: string; + + /** + * path to the https certificate for local server + */ + certificate: string; + + /** + * path to the https certificate's private key + */ + privateKey: string; + + /** + * Maximum allowed number of simultaneous HTTPS connections to the + * webhook for update delivery, 1-100. Defaults to 40. Use lower values + * to limit the load on your bot‘s server, and higher values to increase + * your bot’s throughput. + */ + max_connections?: string; + + /** + * List the types of updates you want your bot to receive. Specify an + * empty list to receive all updates regardless of type (default). + */ + allowed_updates?: string[]; + + /** + * hostname/local IP to listen on for webhooks + * if not provided `url` is required + */ + host?: string; + + /** + * port to listen on for webhooks, defaults to 8443 + */ + port?: string; + }; + +} + +declare class TelegramApi extends EventEmitter { + /** + * Instantiate a new bot and start fetching updates if configured so + */ + public constructor(params: ApiParams); + + /** + * add listener for all updates + */ + public on(event: 'update', listener: (update: Update) => void): this; + + /** + * add listener for inline callback queries + */ + public on(event: 'inline.callback.query', listener: (query: CallbackQuery) => void): this; + + /** + * add listener for message edits + */ + public on(event: 'edited.message', listener: (message: Message) => void): this; + + /** + * add listener for inline queries + */ + public on(event: 'inline.query', listener: (query: InlineQuery) => void): this; + + /** + * add listener for chosen inline results + */ + public on(event: 'inline.result', listener: (result: ChosenInlineResult) => void): this; + + /** + * add listener for new incoming messages + */ + public on(event: 'message', listener: (message: Message) => void): this; + + /** + * add listener for generic events (see EventEmitter) + */ + public on(event: string | symbol, listener: (...args: any[]) => void): this; + + /** + * Returns basic information about the bot in form of a User object. + */ + public getMe: ApiMethod; + + /** + * Use this method to send text messages. + * On success, the sent Message is returned. + */ + public sendMessage: ApiMethodWithParams; + + /** + * Use this method to forward messages of any kind. On success, the sent + * Message is returned. + */ + public forwardMessage: ApiMethodWithParams; + + /** + * Use this method to send photos. On success, the sent Message is returned. + */ + public sendPhoto: ApiMethodWithParams; + + /** + * Use this method to send audio files, if you want Telegram clients to + * display them in the music player. + * + * For sending voice messages, use the sendVoice method instead. + */ + public sendAudio: ApiMethodWithParams; + + /** + * Use this method to send audio files, if you want Telegram clients to + * display the file as a playable voice message. + */ + public sendVoice: ApiMethodWithParams; + + /** + * Use this method to send general files. + */ + public sendDocument: ApiMethodWithParams; + + /** + * Use this method to send .webp stickers. + */ + public sendSticker: ApiMethodWithParams; + + /** + * Use this method to send video files, Telegram clients support mp4 videos + * (other formats may be sent as Document). + */ + public sendVideo: ApiMethodWithParams; + + /** + * Use this method to send point on the map. + */ + public sendLocation: ApiMethodWithParams; + + /** + * Use this method to send information about a venue. + */ + public sendVenue: ApiMethodWithParams; + + /** + * Use this method to send phone contacts. + */ + public sendContact: ApiMethodWithParams; + + /** + * Use this method to kick a user from a group, a supergroup or a channel. + */ + public kickChatMember: ApiMethodWithParams; + + /** + * Use this method to unban a previously kicked user in a supergroup or + * channel. + */ + public unbanChatMember: ApiMethodWithParams; + + /** + * Use this method for your bot to leave a group, supergroup or channel. + */ + public leaveChat: ApiMethodWithParams; + + /** + * Use this method to get up to date information about the chat (current + * name of the user for one-on-one conversations, current username of a + * user, group or channel, etc.) + */ + public getChat: ApiMethodWithParams; + + /** + * Use this method to get a list of administrators in a chat. + */ + public getChatAdministrators: ApiMethodWithParams; + + /** + * Use this method to get the number of members in a chat. + */ + public getChatMembersCount: ApiMethodWithParams; + + /** + * Use this method to get information about a member of a chat. + */ + public getChatMember: ApiMethodWithParams; + + /** + * Use this method when you need to tell the user that something is + * happening on the bot's side. + */ + public sendChatAction: ApiMethodWithParams; + + /** + * Use this method to get a list of profile pictures for a user. + */ + public getUserProfilePhotos: ApiMethodWithParams; + + /** + * Use this method to receive incoming updates using long polling. + */ + public getUpdates: ApiMethodWithParams; + + /** + * Use this method to specify a url and receive incoming updates via an + * outgoing webhook. + */ + public setWebhook: ApiMethodWithParams; + + /** + * Use this method to remove webhook integration if you decide to switch + * back to getUpdates. + */ + public deleteWebhook: ApiMethod; + + /** + * Use this method to get current webhook status. + */ + public getWebhookInfo: ApiMethod; + + /** + * Use this method to get basic info about a file and prepare it for + * downloading. + */ + public getFile: ApiMethodWithParams; + + /** + * Use this method to send answers to an inline query. + */ + public answerInlineQuery: ApiMethodWithParams; + + /** + * Use this method to send answers to callback queries sent from inline + * keyboards. + */ + public answerCallbackQuery: ApiMethodWithParams; + + /** + * Use this method to edit text and game messages sent by the bot or via the + * bot (for inline bots). + */ + public editMessageText: ApiMethodWithParams; + + /** + * Use this method to edit captions of messages sent by the bot or via the + * bot (for inline bots). + */ + public editMessageCaption: ApiMethodWithParams; + + /** + * Use this method to edit only the reply markup of messages sent by the bot + * or via the bot (for inline bots). + */ + public editMessageReplyMarkup: ApiMethodWithParams; + + /** + * Use this method to generate a new invite link for a chat; any previously + * generated link is revoked. + */ + public exportChatInviteLink: ApiMethodWithParams; + + /** + * Use this method to delete a message, including service messages + */ + public deleteMessage: ApiMethodWithParams; + + /** + * Use this method to send a game. + */ + public sendGame: ApiMethodWithParams; +} + +export = TelegramApi; diff --git a/package.json b/package.json index cf514eb..ed6a040 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "url": "https://github.com/mast/telegram-bot-api.git" }, "main": "./lib/telegram-bot.js", + "types": "./lib/telegram-bot.d.ts", "keywords": [ "telegram", "nodejs", @@ -32,9 +33,12 @@ ], "license": "MIT", "dependencies": { + "@types/bluebird": "^3.5.25", + "@types/node": "^10.12.24", "bluebird": "^3.4.0", "extend": "2.0.1", "request-promise": "4.2.2", + "telegram-typings": "^4.0.0-beta", "express": "4.16.3", "body-parser": "1.18.2" },