node-telegram-bot-api
Advanced tools
Comparing version 0.13.0 to 0.14.0
{ | ||
"name": "node-telegram-bot-api", | ||
"version": "0.13.0", | ||
"version": "0.14.0", | ||
"description": "Telegram Bot API", | ||
@@ -19,3 +19,4 @@ "main": "index.js", | ||
"test-cov": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage", | ||
"gen-doc": "./node_modules/.bin/jsdoc2md --src src/telegram.js -t README.hbs > README.md" | ||
"gen-doc": "./node_modules/.bin/jsdoc2md --src src/telegram.js -t README.hbs > README.md", | ||
"jshint": "./node_modules/.bin/jshint src test" | ||
}, | ||
@@ -25,16 +26,16 @@ "author": "Yago Pérez <yagoperezs@gmail.com>", | ||
"dependencies": { | ||
"bluebird": "^2.9.30", | ||
"bluebird": "^2.10.1", | ||
"debug": "^2.2.0", | ||
"mime": "^1.3.4", | ||
"request": "^2.58.0" | ||
"request": "^2.64.0" | ||
}, | ||
"devDependencies": { | ||
"contributor": "^0.1.25", | ||
"coveralls": "^2.11.2", | ||
"del": "^2.0.2", | ||
"istanbul": "^0.3.17", | ||
"coveralls": "^2.11.4", | ||
"istanbul": "^0.3.21", | ||
"jsdoc-to-markdown": "^1.2.0", | ||
"mocha": "^2.2.5", | ||
"mocha-lcov-reporter": "0.0.2", | ||
"should": "^7.0.1" | ||
"jshint": "^2.8.0", | ||
"mocha": "^2.3.3", | ||
"mocha-lcov-reporter": "^1.0.0", | ||
"should": "^7.1.0" | ||
}, | ||
@@ -41,0 +42,0 @@ "repository": { |
@@ -29,3 +29,13 @@ [![Build Status](https://travis-ci.org/yagop/node-telegram-bot-api.svg?branch=master)](https://travis-ci.org/yagop/node-telegram-bot-api) [![Build status](https://ci.appveyor.com/api/projects/status/ujko6bsum3g5msjh/branch/master?svg=true)](https://ci.appveyor.com/project/yagop/node-telegram-bot-api/branch/master) [![Coverage Status](https://coveralls.io/repos/yagop/node-telegram-bot-api/badge.svg?branch=master)](https://coveralls.io/r/yagop/node-telegram-bot-api?branch=master) [![bitHound Score](https://www.bithound.io/github/yagop/node-telegram-bot-api/badges/score.svg)](https://www.bithound.io/github/yagop/node-telegram-bot-api) | ||
### WebHooks | ||
Telegram only supports HTTPS connections to WebHooks, in order to set a WebHook a private key file and public certificate must be used. Since August 29, 2015 Telegram supports self signed ones, to generate them: | ||
```bash | ||
# Our private cert will be key.pem, keep in private this file. | ||
openssl genrsa -out key.pem 2048 | ||
# Our public certificate will be crt.pem | ||
openssl req -new -sha256 -key key.pem -out crt.pem | ||
``` | ||
Once they are generated, the `crt.pem` can be provided to `telegramBot.setWebHook(url, crt)` as `crt`. | ||
## API Reference | ||
@@ -42,3 +52,3 @@ <a name="TelegramBot"></a> | ||
* [.getMe()](#TelegramBot+getMe) ⇒ <code>Promise</code> | ||
* [.setWebHook(url)](#TelegramBot+setWebHook) | ||
* [.setWebHook(url, [cert])](#TelegramBot+setWebHook) | ||
* [.getUpdates([timeout], [limit], [offset])](#TelegramBot+getUpdates) ⇒ <code>Promise</code> | ||
@@ -63,3 +73,3 @@ * [.sendMessage(chatId, text, [options])](#TelegramBot+sendMessage) ⇒ <code>Promise</code> | ||
Both request method to obtain messages are implemented. To use standard polling, set `polling: true` | ||
on `options`. Notice that [webHook](https://core.telegram.org/bots/api#setwebhook) will need a valid (not self signed) SSL certificate. | ||
on `options`. Notice that [webHook](https://core.telegram.org/bots/api#setwebhook) will need a SSL certificate. | ||
Emits `message` when a message arrives. | ||
@@ -76,4 +86,4 @@ | ||
| [options.webHook] | <code>Boolean</code> | <code>Object</code> | <code>false</code> | Set true to enable WebHook or set options | | ||
| [options.webHook.key] | <code>String</code> | | PEM private key to webHook server | | ||
| [options.webHook.cert] | <code>String</code> | | PEM certificate key to webHook server | | ||
| [options.webHook.key] | <code>String</code> | | PEM private key to webHook server. | | ||
| [options.webHook.cert] | <code>String</code> | | PEM certificate (public) to webHook server. | | ||
@@ -87,3 +97,3 @@ <a name="TelegramBot+getMe"></a> | ||
<a name="TelegramBot+setWebHook"></a> | ||
### telegramBot.setWebHook(url) | ||
### telegramBot.setWebHook(url, [cert]) | ||
Specify an url to receive incoming updates via an outgoing webHook. | ||
@@ -97,2 +107,3 @@ | ||
| url | <code>String</code> | URL where Telegram will make HTTP Post. Leave empty to delete webHook. | | ||
| [cert] | <code>String</code> | <code>stream.Stream</code> | PEM certificate key (public). | | ||
@@ -99,0 +110,0 @@ <a name="TelegramBot+getUpdates"></a> |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var TelegramBotWebHook = require('./telegramWebHook'); | ||
@@ -5,4 +7,4 @@ var TelegramBotPolling = require('./telegramPolling'); | ||
var EventEmitter = require('events').EventEmitter; | ||
var Promise = require("bluebird"); | ||
var request = require("request"); | ||
var Promise = require('bluebird'); | ||
var request = require('request'); | ||
var stream = require('stream'); | ||
@@ -19,3 +21,3 @@ var util = require('util'); | ||
* Both request method to obtain messages are implemented. To use standard polling, set `polling: true` | ||
* on `options`. Notice that [webHook](https://core.telegram.org/bots/api#setwebhook) will need a valid (not self signed) SSL certificate. | ||
* on `options`. Notice that [webHook](https://core.telegram.org/bots/api#setwebhook) will need a SSL certificate. | ||
* Emits `message` when a message arrives. | ||
@@ -31,4 +33,4 @@ * | ||
* @param {Boolean|Object} [options.webHook=false] Set true to enable WebHook or set options | ||
* @param {String} [options.webHook.key] PEM private key to webHook server | ||
* @param {String} [options.webHook.cert] PEM certificate key to webHook server | ||
* @param {String} [options.webHook.key] PEM private key to webHook server. | ||
* @param {String} [options.webHook.cert] PEM certificate (public) to webHook server. | ||
* @see https://core.telegram.org/bots/api | ||
@@ -62,3 +64,3 @@ */ | ||
this._polling.abort = true; | ||
this._polling.lastRequest.cancel("Polling restart"); | ||
this._polling.lastRequest.cancel('Polling restart'); | ||
} | ||
@@ -113,3 +115,3 @@ this._polling = new TelegramBotPolling(this.token, this.options.polling, this.processUpdate); | ||
TelegramBot.prototype._buildURL = function(path) { | ||
return URL.format({ | ||
return URL.format({ | ||
protocol: 'https', | ||
@@ -119,3 +121,3 @@ host: 'api.telegram.org', | ||
}); | ||
} | ||
}; | ||
@@ -136,8 +138,18 @@ /** | ||
* delete webHook. | ||
* @param {String|stream.Stream} [cert] PEM certificate key (public). | ||
* @see https://core.telegram.org/bots/api#setwebhook | ||
*/ | ||
TelegramBot.prototype.setWebHook = function (url) { | ||
TelegramBot.prototype.setWebHook = function (url, cert) { | ||
var path = 'setWebHook'; | ||
var qs = {url: url}; | ||
return this._request(path, {qs: qs}) | ||
var opts = { | ||
qs: {url: url} | ||
}; | ||
if (cert) { | ||
var content = this._formatSendData('certificate', cert); | ||
opts.formData = content[0]; | ||
opts.qs.certificate = content[1]; | ||
} | ||
return this._request(path, opts) | ||
.then(function (resp) { | ||
@@ -160,3 +172,3 @@ if (!resp) { | ||
TelegramBot.prototype.getUpdates = function (timeout, limit, offset) { | ||
var query = { | ||
var form = { | ||
offset: offset, | ||
@@ -167,3 +179,3 @@ limit: limit, | ||
return this._request('getUpdates', {qs: query}); | ||
return this._request('getUpdates', {form: form}); | ||
}; | ||
@@ -180,6 +192,6 @@ | ||
TelegramBot.prototype.sendMessage = function (chatId, text, options) { | ||
var query = options || {}; | ||
query.chat_id = chatId; | ||
query.text = text; | ||
return this._request('sendMessage', {qs: query}); | ||
var form = options || {}; | ||
form.chat_id = chatId; | ||
form.text = text; | ||
return this._request('sendMessage', {form: form}); | ||
}; | ||
@@ -196,3 +208,3 @@ | ||
TelegramBot.prototype.forwardMessage = function (chatId, fromChatId, messageId) { | ||
var query = { | ||
var form = { | ||
chat_id: chatId, | ||
@@ -202,3 +214,3 @@ from_chat_id: fromChatId, | ||
}; | ||
return this._request('forwardMessage', {qs: query}); | ||
return this._request('forwardMessage', {form: form}); | ||
}; | ||
@@ -370,7 +382,7 @@ | ||
TelegramBot.prototype.sendChatAction = function (chatId, action) { | ||
var query = { | ||
var form = { | ||
chat_id: chatId, | ||
action: action | ||
}; | ||
return this._request('sendChatAction', {qs: query}); | ||
return this._request('sendChatAction', {form: form}); | ||
}; | ||
@@ -389,3 +401,3 @@ | ||
TelegramBot.prototype.getUserProfilePhotos = function (userId, offset, limit) { | ||
var query = { | ||
var form = { | ||
user_id: userId, | ||
@@ -395,3 +407,3 @@ offset: offset, | ||
}; | ||
return this._request('getUserProfilePhotos', {qs: query}); | ||
return this._request('getUserProfilePhotos', {form: form}); | ||
}; | ||
@@ -411,7 +423,7 @@ | ||
TelegramBot.prototype.sendLocation = function (chatId, latitude, longitude, options) { | ||
var query = options || {}; | ||
query.chat_id = chatId; | ||
query.latitude = latitude; | ||
query.longitude = longitude; | ||
return this._request('sendLocation', {qs: query}); | ||
var form = options || {}; | ||
form.chat_id = chatId; | ||
form.latitude = latitude; | ||
form.longitude = longitude; | ||
return this._request('sendLocation', {form: form}); | ||
}; | ||
@@ -429,6 +441,4 @@ | ||
TelegramBot.prototype.getFile = function(fileId) { | ||
var query = { file_id: fileId }; | ||
return this._request('getFile', {qs: query}); | ||
var form = {file_id: fileId}; | ||
return this._request('getFile', {form: form}); | ||
}; | ||
@@ -484,4 +494,4 @@ | ||
} | ||
}; | ||
module.exports = TelegramBot; |
@@ -0,3 +1,5 @@ | ||
'use strict'; | ||
var debug = require('debug')('node-telegram-bot-api'); | ||
var Promise = require("bluebird"); | ||
var Promise = require('bluebird'); | ||
var request = require('request'); | ||
@@ -10,3 +12,3 @@ var URL = require('url'); | ||
options = options || {}; | ||
if (typeof options === "function") { | ||
if (typeof options === 'function') { | ||
callback = options; | ||
@@ -13,0 +15,0 @@ options = {}; |
@@ -0,5 +1,6 @@ | ||
'use strict'; | ||
var debug = require('debug')('node-telegram-bot-api'); | ||
var https = require('https'); | ||
var http = require('http'); | ||
var util = require('util'); | ||
var fs = require('fs'); | ||
@@ -29,3 +30,3 @@ | ||
this._webServer.listen(options.port, options.host, function () { | ||
debug("WebHook listening on port %s", options.port); | ||
debug('WebHook listening on port %s', options.port); | ||
}); | ||
@@ -32,0 +33,0 @@ }; |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var TelegramPolling = require('../src/telegramPolling'); | ||
@@ -7,3 +9,3 @@ var Telegram = require('../index'); | ||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; | ||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; | ||
var TOKEN = process.env.TEST_TELEGRAM_TOKEN; | ||
@@ -21,2 +23,3 @@ if (!TOKEN) { | ||
var bot = new Telegram(TOKEN); | ||
// Google IP ¯\_(ツ)_/¯ | ||
bot.setWebHook('216.58.210.174').then(function (resp) { | ||
@@ -28,2 +31,11 @@ resp.should.be.exactly(true); | ||
it('should set a webHook with certificate', function (done) { | ||
var bot = new Telegram(TOKEN); | ||
var cert = __dirname+'/../examples/crt.pem'; | ||
bot.setWebHook('216.58.210.174', cert).then(function (resp) { | ||
resp.should.be.exactly(true); | ||
done(); | ||
}); | ||
}); | ||
it('should delete the webHook', function (done) { | ||
@@ -75,3 +87,3 @@ var bot = new Telegram(TOKEN); | ||
headers: { | ||
"content-type": "application/json", | ||
'content-type': 'application/json', | ||
}, | ||
@@ -101,3 +113,3 @@ body: {update_id: 0, message: {text: 'test'}} | ||
headers: { | ||
"content-type": "application/json", | ||
'content-type': 'application/json', | ||
}, | ||
@@ -198,3 +210,3 @@ rejectUnhauthorized: false, | ||
var bot = new Telegram(TOKEN); | ||
var action = "typing"; | ||
var action = 'typing'; | ||
bot.sendChatAction(USERID, action).then(function (resp) { | ||
@@ -201,0 +213,0 @@ resp.should.be.exactly(true); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
559626
22
1073
309
Updatedbluebird@^2.10.1
Updatedrequest@^2.64.0