new Bot(token, opts)
A JavaScript interface for using the Telegram Bot API
This module also exports a paramTypes variable. See global.html#paramTypes
Parameters:
| Name | Type | Description |
|---|---|---|
token |
String | Your bot's token. More information: (https://core.telegram.org/bots/api#authorizing-your-bot)[https://core.telegram.org/bots/api#authorizing-your-bot] |
opts |
Object | (Optional) Extra options to pass to this bot instance. Available options include:
|
Methods
-
answerInlineQuery(inlineQueryId, results, params)
-
Sends a reply to an inline query
Parameters:
Name Type Description inlineQueryIdNumber ID of the inline query to reply to
resultsArray Array of InlineQueryResult (see link above)
paramsObject Optional extra parameters
Returns:
A promise that resolves with the result once the API request has finished
- Type
- Promise
-
apiPostRequest(method, formData)
-
Sends a POST API request to the specified method
Parameters:
Name Type Description methodString Method to call
formDataObject Form data to send along with the request
Returns:
A promise that resolves with the result once the API request has finished
- Type
- Promise
-
apiRequest(method, params)
-
Sends a GET API request to the specified method
Parameters:
Name Type Description methodString Method to call
paramsObject Query parameters of the request
Returns:
A promise that resolves with the result once the API request has finished
- Type
- Promise
-
forwardMessage(chatId, fromChatId, messageId)
-
Forwards the specified message ID
More info: https://core.telegram.org/bots/api#forwardmessage
Parameters:
Name Type Description chatIdNumber ID of the target chat
fromChatIdNumber ID of the original chat
messageIdNumber ID of the message to forward
Returns:
A promise that resolves with the result once the API request has finished
- Type
- Promise
-
getFile(fileId)
-
Returns information about the specified file ID
More info: https://core.telegram.org/bots/api#getfile
Parameters:
Name Type Description fileIdNumber ID of the file
Returns:
A promise that resolves with the result once the API request has finished
- Type
- Promise
-
getFileURL(filePath)
-
Returns the URL to the specified file. This URL is valid for at least 1 hour.
More info: https://core.telegram.org/bots/api#getfile
Parameters:
Name Type Description filePathString The file path, as returned by getFile
Returns:
The file's URL
- Type
- String
-
getMe()
-
Returns information about the bot user
Note: this is already available via the
infoclass propertyMore info: https://core.telegram.org/bots/api#getme
Returns:
A promise that resolves with the result once the API request has finished
- Type
- Promise
-
getUserProfilePhotos(userId)
-
Returns a list of profile photos for the specified user
More info: https://core.telegram.org/bots/api#getuserprofilephotos
Parameters:
Name Type Description userIdNumber ID of the user
Returns:
A promise that resolves with the result once the API request has finished
- Type
- Promise
-
on(event, cb)
-
Attaches an event listener for the specified event type
Parameters:
Name Type Description eventString Type of event
cbfunction Callback to call when the event fires
Returns:
- Type
- void
-
onCommand(triggerSymbol, cmd, params, cb)
-
Attach an event listener to when a text message with a matching command is received
This function accepts a variable number of arguments: (cmd, cb) (cmd, params, cb) (triggerSymbol, cmd, params, cb)
Parameters:
Name Type Description triggerSymbolString String to prefix the command with; defaults to '/'
cmdString Command to match, WITHOUT the leading triggerSymbol (e.g. '/'); can use regex
paramsArray Parameters the command accepts. Parameters can either be regex strings or the following array construct:
[name, regexString, options]Where name and regexString are strings and options is an optional object. Available options:
- optional Whether the parameter is optional or not; defaults to false
- stripQuotes Whether to strip wrapping quotes from parameters; defaults to true
Parameters are matched as both
triggerSymbol+cmdandtriggerSymbol+cmd@botUsername. For example, both of these will match:/getLocation Londonor/getLocation@myBot LondonThe matched command can always be accessed via params._cmd (especially useful with dynamic commands)
cbfunction Listener to call when the command matches
Returns:
- Type
- void
-
sendAction(chatId, action)
-
Sends a chat action to the specified chat
More info: https://core.telegram.org/bots/api#sendchataction
Parameters:
Name Type Description chatIdNumber ID of the target chat
actionString Type of action to send
Returns:
A promise that resolves with the result once the API request has finished
- Type
- Promise
-
sendAudio(chatId, audio)
-
Sends audio to the specified chat
For uploading an audio file, see allowed parameters here: https://github.com/request/request#multipartform-data-multipart-form-uploads
More info: https://core.telegram.org/bots/api#sendaudio
Parameters:
Name Type Description chatIdNumber ID of the target chat
audiomixed File ID to resend or a file to upload
Returns:
A promise that resolves with the result once the API request has finished
- Type
- Promise
-
sendDocument(chatId, document)
-
Sends a document to the specified chat
For uploading a document file, see allowed parameters here: https://github.com/request/request#multipartform-data-multipart-form-uploads
Parameters:
Name Type Description chatIdNumber ID of the target chat
documentmixed File ID to resend or a file to upload
Returns:
A promise that resolves with the result once the API request has finished
- Type
- Promise
-
sendLocation(chatId, latitude, longitude)
-
Sends a location to the specified chat
Parameters:
Name Type Description chatIdNumber ID of the target chat
latitudeString | Number Latitude of the location
longitudeString | Number Longitude of the location
Returns:
A promise that resolves with the result once the API request has finished
- Type
- Promise
-
sendMessage(chatId, str, params)
-
Sends a message to the specified chat
Parameters:
Name Type Description chatIdNumber ID of the target chat
strString Message text
paramsArray (Optional) Additional params to supply to the request
Returns:
A promise that resolves with the result once the API request has finished
- Type
- Promise
-
sendPhoto(chatId, photo)
-
Sends a photo to the specified chat
For uploading a photo, see allowed parameters here: https://github.com/request/request#multipartform-data-multipart-form-uploads
More info: https://core.telegram.org/bots/api#sendphoto
Parameters:
Name Type Description chatIdNumber ID of the target chat
photomixed File ID to resend or a file to upload
Returns:
A promise that resolves with the result once the API request has finished
- Type
- Promise
-
sendSticker(chatId, sticker)
-
Sends a sticker to the specified chat
For uploading a sticker (image file), see allowed parameters here: https://github.com/request/request#multipartform-data-multipart-form-uploads
Parameters:
Name Type Description chatIdNumber ID of the target chat
stickermixed File ID to resend or a file to upload
Returns:
A promise that resolves with the result once the API request has finished
- Type
- Promise
-
sendVideo(chatId, video)
-
Sends a video to the specified chat
For uploading a video file, see allowed parameters here: https://github.com/request/request#multipartform-data-multipart-form-uploads
More info: https://core.telegram.org/bots/api#sendvideo
Parameters:
Name Type Description chatIdNumber ID of the target chat
videomixed File ID to resend or a file to upload
Returns:
A promise that resolves with the result once the API request has finished
- Type
- Promise
-
sendVoice(chatId, voice)
-
Sends a voice recording to the specified chat
For uploading a voice recording (an audio file), see allowed parameters here: https://github.com/request/request#multipartform-data-multipart-form-uploads
More info: https://core.telegram.org/bots/api#sendvoice
Parameters:
Name Type Description chatIdNumber ID of the target chat
voicemixed File ID to resend or a file to upload
Returns:
A promise that resolves with the result once the API request has finished
- Type
- Promise
-
setWebhook(url, certificate)
-
Sets an HTTPS URL where Telegram will send updates
Note: this WILL disable all updates/messages from the API
For uploading the certificate, see allowed parameters here: https://github.com/request/request#multipartform-data-multipart-form-uploads
Parameters:
Name Type Description urlString HTTPS url to send updates to
certificatemixed Your publickey certificate
Returns:
A promise that resolves with the result once the API request has finished
- Type
- Promise