mirror of
https://github.com/yagop/node-telegram-bot-api
synced 2025-08-22 09:57:10 +00:00
refactor: rewrite to babel, es6 & eslint
This commit is contained in:
parent
b9649c00bb
commit
83e42201ee
11
.babelrc
Normal file
11
.babelrc
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"plugins": [
|
||||
"transform-es2015-spread",
|
||||
"transform-es2015-destructuring",
|
||||
"transform-strict-mode",
|
||||
"transform-es2015-parameters",
|
||||
"transform-es2015-shorthand-properties",
|
||||
"transform-object-rest-spread",
|
||||
"transform-class-properties"
|
||||
]
|
||||
}
|
13
.editorconfig
Normal file
13
.editorconfig
Normal file
@ -0,0 +1,13 @@
|
||||
# http://editorconfig.org
|
||||
root = true
|
||||
|
||||
[*]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
end_of_line = lf
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
|
||||
[*.md]
|
||||
trim_trailing_whitespace = false
|
2
.eslintignore
Normal file
2
.eslintignore
Normal file
@ -0,0 +1,2 @@
|
||||
bin
|
||||
*.md
|
18
.eslintrc
Normal file
18
.eslintrc
Normal file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"extends": "airbnb/base",
|
||||
"parser": "babel-eslint",
|
||||
"rules": {
|
||||
"new-cap": 0,
|
||||
"prefer-arrow-callback": 0,
|
||||
"no-param-reassign": [2,{"props":false}],
|
||||
"max-len": [2, 150],
|
||||
"arrow-body-style": 0,
|
||||
"comma-dangle": 0
|
||||
},
|
||||
"plugins": [
|
||||
"mocha"
|
||||
],
|
||||
"env": {
|
||||
"mocha": true
|
||||
}
|
||||
}
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -4,3 +4,4 @@ npm-debug.log
|
||||
.package.json
|
||||
output.md
|
||||
output/
|
||||
lib/
|
11
.jshintrc
11
.jshintrc
@ -1,11 +0,0 @@
|
||||
{
|
||||
"curly" : true,
|
||||
"eqeqeq" : true,
|
||||
"indent" : 2,
|
||||
"latedef" : true,
|
||||
"newcap" : true,
|
||||
"quotmark": "single",
|
||||
"strict" : true,
|
||||
"undef" : true,
|
||||
"node" : true
|
||||
}
|
@ -1,8 +1,7 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "stable"
|
||||
- "0.12"
|
||||
- "0.10"
|
||||
- "5"
|
||||
- "4"
|
||||
after_success:
|
||||
- npm run test-cov
|
||||
cache:
|
||||
|
31
package.json
31
package.json
@ -2,7 +2,7 @@
|
||||
"name": "node-telegram-bot-api",
|
||||
"version": "0.21.1",
|
||||
"description": "Telegram Bot API",
|
||||
"main": "index.js",
|
||||
"main": "./lib/telegram.js",
|
||||
"directories": {
|
||||
"example": "examples",
|
||||
"test": "test"
|
||||
@ -14,29 +14,46 @@
|
||||
"bot"
|
||||
],
|
||||
"scripts": {
|
||||
"prepublish": "./node_modules/.bin/babel -d ./lib src",
|
||||
"test": "./node_modules/.bin/mocha test/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",
|
||||
"jshint": "./node_modules/.bin/jshint src test"
|
||||
"eslint": "./node_modules/.bin/eslint ./src"
|
||||
},
|
||||
"author": "Yago Pérez <yagoperezs@gmail.com>",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"bluebird": "^2.10.2",
|
||||
"bl": "^1.1.2",
|
||||
"bluebird": "^3.3.4",
|
||||
"debug": "^2.2.0",
|
||||
"eventemitter3": "^1.2.0",
|
||||
"file-type": "^3.8.0",
|
||||
"mime": "^1.3.4",
|
||||
"request": "^2.69.0"
|
||||
"pump": "^1.0.1",
|
||||
"request": "^2.69.0",
|
||||
"request-promise": "^2.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-cli": "^6.6.5",
|
||||
"babel-eslint": "^5.0.0",
|
||||
"babel-plugin-transform-class-properties": "^6.6.0",
|
||||
"babel-plugin-transform-es2015-destructuring": "^6.6.5",
|
||||
"babel-plugin-transform-es2015-parameters": "^6.7.0",
|
||||
"babel-plugin-transform-es2015-shorthand-properties": "^6.5.0",
|
||||
"babel-plugin-transform-es2015-spread": "^6.6.5",
|
||||
"babel-plugin-transform-object-rest-spread": "^6.6.5",
|
||||
"babel-plugin-transform-strict-mode": "^6.6.5",
|
||||
"babel-register": "^6.7.2",
|
||||
"contributor": "^0.1.25",
|
||||
"coveralls": "^2.11.8",
|
||||
"eslint": "^2.4.0",
|
||||
"eslint-config-airbnb": "^6.1.0",
|
||||
"eslint-plugin-mocha": "^2.0.0",
|
||||
"is": "^3.1.0",
|
||||
"istanbul": "^0.4.2",
|
||||
"jsdoc-to-markdown": "^1.3.3",
|
||||
"jshint": "^2.9.1",
|
||||
"mocha": "^2.4.5",
|
||||
"mocha-lcov-reporter": "^1.2.0",
|
||||
"should": "^8.2.2"
|
||||
"mocha-lcov-reporter": "^1.2.0"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
1046
src/telegram.js
1046
src/telegram.js
File diff suppressed because it is too large
Load Diff
@ -1,90 +1,103 @@
|
||||
'use strict';
|
||||
const Promise = require('bluebird');
|
||||
const debug = require('debug')('node-telegram-bot-api');
|
||||
const request = require('request-promise');
|
||||
const URL = require('url');
|
||||
|
||||
var debug = require('debug')('node-telegram-bot-api');
|
||||
var Promise = require('bluebird');
|
||||
var request = require('request');
|
||||
var util = require('util');
|
||||
var URL = require('url');
|
||||
class TelegramBotPolling {
|
||||
|
||||
var requestPromise = Promise.promisify(request);
|
||||
|
||||
var TelegramBotPolling = function (token, options, callback) {
|
||||
options = options || {};
|
||||
if (typeof options === 'function') {
|
||||
callback = options;
|
||||
options = {};
|
||||
}
|
||||
this.offset = 0;
|
||||
this.token = token;
|
||||
this.callback = callback;
|
||||
this.timeout = options.timeout || 10;
|
||||
this.interval = (typeof options.interval === 'number') ? options.interval : 300;
|
||||
this.lastUpdate = 0;
|
||||
this.lastRequest = null;
|
||||
this.abort = false;
|
||||
this._polling();
|
||||
};
|
||||
|
||||
TelegramBotPolling.prototype._polling = function () {
|
||||
this.lastRequest = this._getUpdates()
|
||||
.then(function (updates) {
|
||||
this.lastUpdate = Date.now();
|
||||
debug('polling data %j', updates);
|
||||
updates.forEach(function (update, index) {
|
||||
this.offset = update.update_id;
|
||||
debug('updated offset: %s', this.offset);
|
||||
this.callback(update);
|
||||
}.bind(this));
|
||||
}.bind(this))
|
||||
.catch(function (err) {
|
||||
debug('polling error: %s', err.message);
|
||||
throw err;
|
||||
})
|
||||
.finally(function () {
|
||||
if (this.abort) {
|
||||
debug('Polling is aborted!');
|
||||
} else {
|
||||
debug('setTimeout for %s miliseconds', this.interval);
|
||||
setTimeout(this._polling.bind(this), this.interval);
|
||||
}
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
TelegramBotPolling.prototype._getUpdates = function () {
|
||||
var opts = {
|
||||
qs: {
|
||||
offset: this.offset+1,
|
||||
limit: this.limit,
|
||||
timeout: this.timeout
|
||||
},
|
||||
url: URL.format({
|
||||
protocol: 'https',
|
||||
host: 'api.telegram.org',
|
||||
pathname: '/bot'+this.token+'/getUpdates'
|
||||
})
|
||||
};
|
||||
debug('polling with options: %j', opts);
|
||||
return requestPromise(opts)
|
||||
.cancellable()
|
||||
.timeout((10 + this.timeout) * 1000)
|
||||
.then(function (resp) {
|
||||
if (resp[0].statusCode !== 200) {
|
||||
throw new Error(resp[0].statusCode+' '+resp[0].body);
|
||||
}
|
||||
var data;
|
||||
try {
|
||||
data = JSON.parse(resp[0].body);
|
||||
} catch (err) {
|
||||
var text = util.format('Error parsing Telegram response: %s',
|
||||
resp[0].body);
|
||||
throw new Error(text);
|
||||
}
|
||||
if (data.ok) {
|
||||
return data.result;
|
||||
} else {
|
||||
throw new Error(data.error_code+' '+data.description);
|
||||
}
|
||||
constructor(token, options = {}, callback) {
|
||||
// enable cancellation
|
||||
Promise.config({
|
||||
cancellation: true,
|
||||
});
|
||||
};
|
||||
|
||||
if (typeof options === 'function') {
|
||||
callback = options; // eslint-disable-line no-param-reassign
|
||||
options = {}; // eslint-disable-line no-param-reassign
|
||||
}
|
||||
|
||||
this.offset = 0;
|
||||
this.token = token;
|
||||
this.callback = callback;
|
||||
this.timeout = options.timeout || 10;
|
||||
this.interval = (typeof options.interval === 'number') ? options.interval : 300;
|
||||
this.lastUpdate = 0;
|
||||
this.lastRequest = null;
|
||||
this.abort = false;
|
||||
this._polling();
|
||||
}
|
||||
|
||||
_polling() {
|
||||
this.lastRequest = this
|
||||
._getUpdates()
|
||||
.then(updates => {
|
||||
this.lastUpdate = Date.now();
|
||||
debug('polling data %j', updates);
|
||||
updates.forEach(update => {
|
||||
this.offset = update.update_id;
|
||||
debug('updated offset: %s', this.offset);
|
||||
this.callback(update);
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
debug('polling error: %s', err.message);
|
||||
throw err;
|
||||
})
|
||||
.finally(() => {
|
||||
if (this.abort) {
|
||||
debug('Polling is aborted!');
|
||||
} else {
|
||||
debug('setTimeout for %s miliseconds', this.interval);
|
||||
setTimeout(() => this._polling(), this.interval);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// used so that other funcs are not non-optimizable
|
||||
_safeParse(json) {
|
||||
try {
|
||||
return JSON.parse(json);
|
||||
} catch (err) {
|
||||
throw new Error(`Error parsing Telegram response: ${String(json)}`);
|
||||
}
|
||||
}
|
||||
|
||||
_getUpdates() {
|
||||
const opts = {
|
||||
qs: {
|
||||
offset: this.offset + 1,
|
||||
limit: this.limit,
|
||||
timeout: this.timeout
|
||||
},
|
||||
url: URL.format({
|
||||
protocol: 'https',
|
||||
host: 'api.telegram.org',
|
||||
pathname: `/bot${this.token}/getUpdates`
|
||||
}),
|
||||
simple: false,
|
||||
resolveWithFullResponse: true,
|
||||
forever: true,
|
||||
};
|
||||
debug('polling with options: %j', opts);
|
||||
|
||||
return request(opts)
|
||||
.promise()
|
||||
.timeout((10 + this.timeout) * 1000)
|
||||
.then(resp => {
|
||||
if (resp.statusCode !== 200) {
|
||||
throw new Error(`${resp.statusCode} ${resp.body}`);
|
||||
}
|
||||
|
||||
const data = this._safeParse(resp.body);
|
||||
|
||||
if (data.ok) {
|
||||
return data.result;
|
||||
}
|
||||
|
||||
throw new Error(`${data.error_code} ${data.description}`);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = TelegramBotPolling;
|
||||
|
@ -1,67 +1,85 @@
|
||||
'use strict';
|
||||
const debug = require('debug')('node-telegram-bot-api');
|
||||
const https = require('https');
|
||||
const http = require('http');
|
||||
const fs = require('fs');
|
||||
const bl = require('bl');
|
||||
|
||||
var debug = require('debug')('node-telegram-bot-api');
|
||||
var https = require('https');
|
||||
var http = require('http');
|
||||
var fs = require('fs');
|
||||
class TelegramBotWebHook {
|
||||
|
||||
var TelegramBotWebHook = function (token, options, callback) {
|
||||
this.token = token;
|
||||
this.callback = callback;
|
||||
if (typeof options === 'boolean') {
|
||||
options = {};
|
||||
}
|
||||
options.port = options.port || 8443;
|
||||
var binded = this._requestListener.bind(this);
|
||||
constructor(token, options, callback) {
|
||||
this.token = token;
|
||||
this.callback = callback;
|
||||
this.regex = new RegExp(this.token);
|
||||
|
||||
if (options.key && options.cert) { // HTTPS Server
|
||||
debug('HTTPS WebHook enabled');
|
||||
var opts = {
|
||||
key: fs.readFileSync(options.key),
|
||||
cert: fs.readFileSync(options.cert)
|
||||
};
|
||||
this._webServer = https.createServer(opts, binded);
|
||||
} else {
|
||||
debug('HTTP WebHook enabled');
|
||||
this._webServer = http.createServer(binded);
|
||||
}
|
||||
// define opts
|
||||
if (typeof options === 'boolean') {
|
||||
options = {}; // eslint-disable-line no-param-reassign
|
||||
}
|
||||
options.port = options.port || 8443;
|
||||
|
||||
this._webServer.listen(options.port, options.host, function () {
|
||||
debug('WebHook listening on port %s', options.port);
|
||||
});
|
||||
};
|
||||
if (options.key && options.cert) { // HTTPS Server
|
||||
debug('HTTPS WebHook enabled');
|
||||
const opts = {
|
||||
key: fs.readFileSync(options.key),
|
||||
cert: fs.readFileSync(options.cert)
|
||||
};
|
||||
this._webServer = https.createServer(opts, this._requestListener);
|
||||
} else {
|
||||
debug('HTTP WebHook enabled');
|
||||
this._webServer = http.createServer(this._requestListener);
|
||||
}
|
||||
|
||||
TelegramBotWebHook.prototype._requestListener = function (req, res) {
|
||||
var self = this;
|
||||
var regex = new RegExp(this.token);
|
||||
|
||||
debug('WebHook request URL:', req.url);
|
||||
debug('WebHook request headers: %j', req.headers);
|
||||
// If there isn't token on URL
|
||||
if (!regex.test(req.url)) {
|
||||
debug('WebHook request unauthorized');
|
||||
res.statusCode = 401;
|
||||
res.end();
|
||||
} else if (req.method === 'POST') {
|
||||
var fullBody = '';
|
||||
req.on('data', function (chunk) {
|
||||
fullBody += chunk.toString();
|
||||
this._webServer.listen(options.port, options.host, () => {
|
||||
debug('WebHook listening on port %s', options.port);
|
||||
});
|
||||
req.on('end', function () {
|
||||
try {
|
||||
debug('WebHook request fullBody', fullBody);
|
||||
var data = JSON.parse(fullBody);
|
||||
self.callback(data);
|
||||
} catch (error) {
|
||||
debug(error);
|
||||
}
|
||||
res.end('OK');
|
||||
});
|
||||
} else { // Authorized but not a POST
|
||||
debug('WebHook request isn\'t a POST');
|
||||
res.statusCode = 418; // I'm a teabot!
|
||||
res.end();
|
||||
}
|
||||
};
|
||||
|
||||
// used so that other funcs are not non-optimizable
|
||||
_safeParse(json) {
|
||||
try {
|
||||
return JSON.parse(json);
|
||||
} catch (err) {
|
||||
debug(err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// pipe+parse body
|
||||
_parseBody = (err, body) => {
|
||||
if (err) {
|
||||
return debug(err);
|
||||
}
|
||||
|
||||
const data = this._safeParse(body);
|
||||
if (data) {
|
||||
return this.callback(data);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// bound req listener
|
||||
_requestListener = (req, res) => {
|
||||
debug('WebHook request URL:', req.url);
|
||||
debug('WebHook request headers: %j', req.headers);
|
||||
|
||||
// If there isn't token on URL
|
||||
if (!this.regex.test(req.url)) {
|
||||
debug('WebHook request unauthorized');
|
||||
res.statusCode = 401;
|
||||
res.end();
|
||||
} else if (req.method === 'POST') {
|
||||
req
|
||||
.pipe(bl(this._parseBody))
|
||||
.on('end', () => res.end('OK'));
|
||||
} else {
|
||||
// Authorized but not a POST
|
||||
debug('WebHook request isn\'t a POST');
|
||||
res.statusCode = 418; // I'm a teabot!
|
||||
res.end();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = TelegramBotWebHook;
|
||||
|
@ -1,12 +0,0 @@
|
||||
{
|
||||
"mocha" : true,
|
||||
"curly" : true,
|
||||
"eqeqeq" : true,
|
||||
"indent" : 2,
|
||||
"latedef" : true,
|
||||
"newcap" : true,
|
||||
"quotmark": "single",
|
||||
"strict" : true,
|
||||
"undef" : true,
|
||||
"node" : true
|
||||
}
|
611
test/index.js
611
test/index.js
@ -1,533 +1,506 @@
|
||||
'use strict';
|
||||
|
||||
var TelegramPolling = require('../src/telegramPolling');
|
||||
var Telegram = require('../index');
|
||||
var Promise = require('bluebird');
|
||||
var request = require('request');
|
||||
var should = require('should');
|
||||
var fs = require('fs');
|
||||
const TelegramPolling = require('../src/telegramPolling');
|
||||
const Telegram = require('../index');
|
||||
const Promise = require('bluebird');
|
||||
const request = require('request-promise');
|
||||
const assert = require('assert');
|
||||
const fs = require('fs');
|
||||
const is = require('is');
|
||||
|
||||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
|
||||
var TOKEN = process.env.TEST_TELEGRAM_TOKEN;
|
||||
const TOKEN = process.env.TEST_TELEGRAM_TOKEN;
|
||||
if (!TOKEN) {
|
||||
throw new Error('Bot token not provided');
|
||||
}
|
||||
|
||||
// Telegram service if not User Id
|
||||
var USERID = process.env.TEST_USER_ID || 777000;
|
||||
const USERID = process.env.TEST_USER_ID || 777000;
|
||||
|
||||
describe('Telegram', function () {
|
||||
|
||||
describe('#setWebHook', function () {
|
||||
it('should set a webHook', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
describe('Telegram', function telegramSuite() {
|
||||
describe('#setWebHook', function setWebHookSuite() {
|
||||
it('should set a webHook', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
// Google IP ¯\_(ツ)_/¯
|
||||
bot.setWebHook('216.58.210.174').then(function (resp) {
|
||||
resp.should.be.exactly(true);
|
||||
done();
|
||||
});
|
||||
return bot
|
||||
.setWebHook('216.58.210.174')
|
||||
.then(resp => {
|
||||
assert.equal(resp, 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 set a webHook with certificate', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const cert = `${__dirname}/../examples/crt.pem`;
|
||||
return bot
|
||||
.setWebHook('216.58.210.174', cert)
|
||||
.then(resp => {
|
||||
assert.equal(resp, true);
|
||||
});
|
||||
});
|
||||
|
||||
it('should delete the webHook', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
bot.setWebHook('').then(function (resp) {
|
||||
resp.should.be.exactly(true);
|
||||
done();
|
||||
});
|
||||
it('should delete the webHook', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
return bot
|
||||
.setWebHook('')
|
||||
.then(resp => {
|
||||
assert.equal(resp, true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#WebHook', function () {
|
||||
it('should reject request if same token not provided', function (done) {
|
||||
var bot = new Telegram(TOKEN, {webHook: true});
|
||||
request({
|
||||
describe('#WebHook', function WebHookSuite() {
|
||||
it('should reject request if same token not provided', function test() {
|
||||
const bot = new Telegram(TOKEN, { webHook: true });
|
||||
|
||||
return request({
|
||||
url: 'http://localhost:8443/NOT_REAL_TOKEN',
|
||||
method: 'POST'
|
||||
}, function (error, response, body) {
|
||||
response.statusCode.should.not.be.equal(200);
|
||||
method: 'POST',
|
||||
simple: false,
|
||||
resolveWithFullResponse: true
|
||||
}).then(response => {
|
||||
assert.notEqual(response.statusCode, 200);
|
||||
bot._WebHook._webServer.close();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should reject request if authorized but not a POST', function (done) {
|
||||
var bot = new Telegram(TOKEN, {webHook: true});
|
||||
request({
|
||||
url: 'http://localhost:8443/bot'+TOKEN,
|
||||
method: 'GET'
|
||||
}, function (error, response, body) {
|
||||
response.statusCode.should.not.be.equal(200);
|
||||
it('should reject request if authorized but not a POST', function test() {
|
||||
const bot = new Telegram(TOKEN, { webHook: true });
|
||||
return request({
|
||||
url: `http://localhost:8443/bot${TOKEN}`,
|
||||
method: 'GET',
|
||||
simple: false,
|
||||
resolveWithFullResponse: true
|
||||
})
|
||||
.then(response => {
|
||||
assert.notEqual(response.statusCode, 200);
|
||||
bot._WebHook._webServer.close();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should emit a `message` on HTTP WebHook', function (done) {
|
||||
var bot = new Telegram(TOKEN, {webHook: true});
|
||||
bot.on('message', function (msg) {
|
||||
it('should emit a `message` on HTTP WebHook', function test(done) {
|
||||
const bot = new Telegram(TOKEN, { webHook: true });
|
||||
bot.on('message', () => {
|
||||
bot._WebHook._webServer.close();
|
||||
done();
|
||||
});
|
||||
var url = 'http://localhost:8443/bot'+TOKEN;
|
||||
|
||||
const url = `http://localhost:8443/bot${TOKEN}`;
|
||||
request({
|
||||
url: url,
|
||||
url,
|
||||
method: 'POST',
|
||||
json: true,
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
body: {update_id: 0, message: {text: 'test'}}
|
||||
body: { update_id: 0, message: { text: 'test' } }
|
||||
});
|
||||
});
|
||||
|
||||
it('should emit a `message` on HTTPS WebHook', function (done) {
|
||||
var opts = {
|
||||
it('should emit a `message` on HTTPS WebHook', function test(done) {
|
||||
const opts = {
|
||||
webHook: {
|
||||
port: 8443,
|
||||
key: __dirname+'/../examples/key.pem',
|
||||
cert: __dirname+'/../examples/crt.pem'
|
||||
key: `${__dirname}/../examples/key.pem`,
|
||||
cert: `${__dirname}/../examples/crt.pem`
|
||||
}
|
||||
};
|
||||
var bot = new Telegram(TOKEN, opts);
|
||||
bot.on('message', function (msg) {
|
||||
const bot = new Telegram(TOKEN, opts);
|
||||
bot.on('message', () => {
|
||||
bot._WebHook._webServer.close();
|
||||
done();
|
||||
});
|
||||
var url = 'https://localhost:8443/bot'+TOKEN;
|
||||
const url = `https://localhost:8443/bot${TOKEN}`;
|
||||
request({
|
||||
url: url,
|
||||
url,
|
||||
method: 'POST',
|
||||
json: true,
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
rejectUnhauthorized: false,
|
||||
body: {update_id: 0, message: {text: 'test'}}
|
||||
body: { update_id: 0, message: { text: 'test' } }
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getMe', function () {
|
||||
it('should return an User object', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
bot.getMe().then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
done();
|
||||
describe('#getMe', function getMeSuite() {
|
||||
it('should return an User object', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
return bot.getMe().then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getUpdates', function () {
|
||||
it('should return an Array', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
bot.getUpdates().then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Array);
|
||||
done();
|
||||
describe('#getUpdates', function getUpdatesSuite() {
|
||||
it('should return an Array', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
return bot.getUpdates().then(resp => {
|
||||
assert.equal(Array.isArray(resp), true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#sendMessage', function () {
|
||||
it('should send a message', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
bot.sendMessage(USERID, 'test').then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
done();
|
||||
describe('#sendMessage', function sendMessageSuite() {
|
||||
it('should send a message', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
return bot.sendMessage(USERID, 'test').then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#forwardMessage', function () {
|
||||
it('should forward a message', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
bot.sendMessage(USERID, 'test').then(function (resp) {
|
||||
var messageId = resp.message_id;
|
||||
bot.forwardMessage(USERID, USERID, messageId)
|
||||
.then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
done();
|
||||
describe('#forwardMessage', function forwardMessageSuite() {
|
||||
it('should forward a message', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
return bot.sendMessage(USERID, 'test').then(resp => {
|
||||
const messageId = resp.message_id;
|
||||
return bot.forwardMessage(USERID, USERID, messageId)
|
||||
.then(forwarded => {
|
||||
assert.ok(is.object(forwarded));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#sendPhoto', function () {
|
||||
var photoId;
|
||||
it('should send a photo from file', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
var photo = __dirname+'/bot.gif';
|
||||
bot.sendPhoto(USERID, photo).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
describe('#sendPhoto', function sendPhotoSuite() {
|
||||
let photoId;
|
||||
it('should send a photo from file', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const photo = `${__dirname}/bot.gif`;
|
||||
return bot.sendPhoto(USERID, photo).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
photoId = resp.photo[0].file_id;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should send a photo from id', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
it('should send a photo from id', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
// Send the same photo as before
|
||||
var photo = photoId;
|
||||
bot.sendPhoto(USERID, photo).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
done();
|
||||
const photo = photoId;
|
||||
return bot.sendPhoto(USERID, photo).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
});
|
||||
});
|
||||
|
||||
it('should send a photo from fs.readStream', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
var photo = fs.createReadStream(__dirname+'/bot.gif');
|
||||
bot.sendPhoto(USERID, photo).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
done();
|
||||
it('should send a photo from fs.readStream', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const photo = fs.createReadStream(`${__dirname}/bot.gif`);
|
||||
return bot.sendPhoto(USERID, photo).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
});
|
||||
});
|
||||
|
||||
it('should send a photo from request Stream', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
var photo = request('https://telegram.org/img/t_logo.png');
|
||||
bot.sendPhoto(USERID, photo).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
done();
|
||||
it('should send a photo from request Stream', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const photo = request('https://telegram.org/img/t_logo.png');
|
||||
return bot.sendPhoto(USERID, photo).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
});
|
||||
});
|
||||
|
||||
it('should send a photo from a Buffer', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
var photo = fs.readFileSync(__dirname+'/bot.gif');
|
||||
bot.sendPhoto(USERID, photo).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
done();
|
||||
it('should send a photo from a Buffer', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const photo = fs.readFileSync(`${__dirname}/bot.gif`);
|
||||
return bot.sendPhoto(USERID, photo).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#sendChatAction', function () {
|
||||
it('should send a chat action', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
var action = 'typing';
|
||||
bot.sendChatAction(USERID, action).then(function (resp) {
|
||||
resp.should.be.exactly(true);
|
||||
done();
|
||||
describe('#sendChatAction', function sendChatActionSuite() {
|
||||
it('should send a chat action', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const action = 'typing';
|
||||
return bot.sendChatAction(USERID, action).then(resp => {
|
||||
assert.equal(resp, true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#sendAudio', function () {
|
||||
it('should send an OGG audio', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
var audio = request('https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg');
|
||||
bot.sendAudio(USERID, audio).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
done();
|
||||
describe('#sendAudio', function sendAudioSuite() {
|
||||
it('should send an OGG audio', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const audio = request('https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg');
|
||||
return bot.sendAudio(USERID, audio).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#sendDocument', function () {
|
||||
var documentId;
|
||||
it('should send a document from file', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
var document = __dirname+'/bot.gif';
|
||||
bot.sendDocument(USERID, document).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
describe('#sendDocument', function sendDocumentSuite() {
|
||||
let documentId;
|
||||
it('should send a document from file', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const document = `${__dirname}/bot.gif`;
|
||||
return bot.sendDocument(USERID, document).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
documentId = resp.document.file_id;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should send a document from id', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
it('should send a document from id', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
// Send the same photo as before
|
||||
var document = documentId;
|
||||
bot.sendDocument(USERID, document).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
done();
|
||||
const document = documentId;
|
||||
return bot.sendDocument(USERID, document).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
});
|
||||
});
|
||||
|
||||
it('should send a document from fs.readStream', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
var document = fs.createReadStream(__dirname+'/bot.gif');
|
||||
bot.sendDocument(USERID, document).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
done();
|
||||
it('should send a document from fs.readStream', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const document = fs.createReadStream(`${__dirname}/bot.gif`);
|
||||
return bot.sendDocument(USERID, document).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
});
|
||||
});
|
||||
|
||||
it('should send a document from request Stream', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
var document = request('https://telegram.org/img/t_logo.png');
|
||||
bot.sendDocument(USERID, document).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
done();
|
||||
it('should send a document from request Stream', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const document = request('https://telegram.org/img/t_logo.png');
|
||||
return bot.sendDocument(USERID, document).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
});
|
||||
});
|
||||
|
||||
it('should send a document from a Buffer', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
var document = fs.readFileSync(__dirname+'/bot.gif');
|
||||
bot.sendDocument(USERID, document).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
done();
|
||||
it('should send a document from a Buffer', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const document = fs.readFileSync(`${__dirname}/bot.gif`);
|
||||
return bot.sendDocument(USERID, document).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#sendSticker', function () {
|
||||
var stickerId;
|
||||
it('should send a sticker from file', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
var sticker = __dirname+'/sticker.webp';
|
||||
bot.sendSticker(USERID, sticker).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
describe('#sendSticker', function sendStickerSuite() {
|
||||
let stickerId;
|
||||
it('should send a sticker from file', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const sticker = `${__dirname}/sticker.webp`;
|
||||
return bot.sendSticker(USERID, sticker).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
stickerId = resp.sticker.file_id;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should send a sticker from id', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
it('should send a sticker from id', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
// Send the same photo as before
|
||||
bot.sendSticker(USERID, stickerId).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
done();
|
||||
return bot.sendSticker(USERID, stickerId).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
});
|
||||
});
|
||||
|
||||
it('should send a sticker from fs.readStream', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
var sticker = fs.createReadStream(__dirname+'/sticker.webp');
|
||||
bot.sendSticker(USERID, sticker).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
done();
|
||||
it('should send a sticker from fs.readStream', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const sticker = fs.createReadStream(`${__dirname}/sticker.webp`);
|
||||
return bot.sendSticker(USERID, sticker).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
});
|
||||
});
|
||||
|
||||
it('should send a sticker from request Stream', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
var sticker = request('https://www.gstatic.com/webp/gallery3/1_webp_ll.webp');
|
||||
bot.sendSticker(USERID, sticker).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
done();
|
||||
it('should send a sticker from request Stream', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const sticker = request('https://www.gstatic.com/webp/gallery3/1_webp_ll.webp');
|
||||
return bot.sendSticker(USERID, sticker).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
});
|
||||
});
|
||||
|
||||
it('should send a sticker from a Buffer', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
var sticker = fs.readFileSync(__dirname+'/sticker.webp');
|
||||
bot.sendDocument(USERID, sticker).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
done();
|
||||
it('should send a sticker from a Buffer', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const sticker = fs.readFileSync(`${__dirname}/sticker.webp`);
|
||||
return bot.sendDocument(USERID, sticker).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#sendVideo', function () {
|
||||
var videoId;
|
||||
it('should send a video from file', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
var video = __dirname+'/video.mp4';
|
||||
bot.sendVideo(USERID, video).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
describe('#sendVideo', function sendVideoSuite() {
|
||||
let videoId;
|
||||
it('should send a video from file', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const video = `${__dirname}/video.mp4`;
|
||||
return bot.sendVideo(USERID, video).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
videoId = resp.video.file_id;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should send a video from id', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
it('should send a video from id', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
// Send the same photo as before
|
||||
bot.sendVideo(USERID, videoId).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
done();
|
||||
return bot.sendVideo(USERID, videoId).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
});
|
||||
});
|
||||
|
||||
it('should send a video from fs.readStream', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
var video = fs.createReadStream(__dirname+'/video.mp4');
|
||||
bot.sendVideo(USERID, video).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
done();
|
||||
it('should send a video from fs.readStream', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const video = fs.createReadStream(`${__dirname}/video.mp4`);
|
||||
return bot.sendVideo(USERID, video).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
});
|
||||
});
|
||||
|
||||
it('should send a video from request Stream', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
var sticker = request('http://techslides.com/demos/sample-videos/small.mp4');
|
||||
bot.sendVideo(USERID, sticker).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
done();
|
||||
it('should send a video from request Stream', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const sticker = request('http://techslides.com/demos/sample-videos/small.mp4');
|
||||
return bot.sendVideo(USERID, sticker).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
});
|
||||
});
|
||||
|
||||
it('should send a video from a Buffer', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
var video = fs.readFileSync(__dirname+'/video.mp4');
|
||||
bot.sendVideo(USERID, video).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
done();
|
||||
it('should send a video from a Buffer', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const video = fs.readFileSync(`${__dirname}/video.mp4`);
|
||||
return bot.sendVideo(USERID, video).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#sendVoice', function () {
|
||||
it('should send an OGG audio as voice', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
var voice = request('https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg');
|
||||
bot.sendVoice(USERID, voice).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
done();
|
||||
describe('#sendVoice', function sendVoiceSuite() {
|
||||
it('should send an OGG audio as voice', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const voice = request('https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg');
|
||||
return bot.sendVoice(USERID, voice).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getUserProfilePhotos', function () {
|
||||
it('should get user profile photos', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
bot.getUserProfilePhotos(USERID).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
resp.total_count.should.be.an.instanceOf(Number);
|
||||
resp.photos.should.be.an.instanceOf(Array);
|
||||
done();
|
||||
describe('#getUserProfilePhotos', function getUserProfilePhotosSuite() {
|
||||
it('should get user profile photos', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
return bot.getUserProfilePhotos(USERID).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
assert.ok(is.number(resp.total_count));
|
||||
assert.ok(is.array(resp.photos));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#sendLocation', function () {
|
||||
it('should send a location', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
var lat = 47.5351072;
|
||||
var long = -52.7508537;
|
||||
bot.sendLocation(USERID, lat, long).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
resp.location.should.be.an.instanceOf(Object);
|
||||
resp.location.latitude.should.be.an.instanceOf(Number);
|
||||
resp.location.longitude.should.be.an.instanceOf(Number);
|
||||
done();
|
||||
describe('#sendLocation', function sendLocationSuite() {
|
||||
it('should send a location', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const lat = 47.5351072;
|
||||
const long = -52.7508537;
|
||||
return bot.sendLocation(USERID, lat, long).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
assert.ok(is.object(resp.location));
|
||||
assert.ok(is.number(resp.location.latitude));
|
||||
assert.ok(is.number(resp.location.longitude));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getFile', function () {
|
||||
var fileId;
|
||||
describe('#getFile', function getFileSuite() {
|
||||
let fileId;
|
||||
|
||||
// To get a file we have to send any file first
|
||||
it('should send a photo from file', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
var photo = __dirname + '/bot.gif';
|
||||
bot.sendPhoto(USERID, photo).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
it('should send a photo from file', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const photo = `${__dirname}/bot.gif`;
|
||||
return bot.sendPhoto(USERID, photo).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
fileId = resp.photo[0].file_id;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should get a file', function (done) {
|
||||
it('should get a file', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
|
||||
var bot = new Telegram(TOKEN);
|
||||
|
||||
bot.getFile(fileId).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
resp.file_path.should.be.an.instanceOf(String);
|
||||
done();
|
||||
});
|
||||
return bot
|
||||
.getFile(fileId)
|
||||
.then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
assert.ok(is.string(resp.file_path));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getFileLink', function () {
|
||||
var fileId;
|
||||
describe('#getFileLink', function getFileLinkSuite() {
|
||||
let fileId;
|
||||
|
||||
// To get a file we have to send any file first
|
||||
it('should send a photo from file', function (done) {
|
||||
var bot = new Telegram(TOKEN);
|
||||
var photo = __dirname + '/bot.gif';
|
||||
bot.sendPhoto(USERID, photo).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
it('should send a photo from file', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const photo = `${__dirname}/bot.gif`;
|
||||
return bot.sendPhoto(USERID, photo).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
fileId = resp.photo[0].file_id;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should get a file link', function (done) {
|
||||
it('should get a file link', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
|
||||
var bot = new Telegram(TOKEN);
|
||||
|
||||
bot.getFileLink(fileId).then(function (fileURI) {
|
||||
fileURI.should.be.an.instanceOf(String);
|
||||
fileURI.should.startWith('https');
|
||||
done(); // TODO: validate URL with some library or regexp
|
||||
});
|
||||
return bot
|
||||
.getFileLink(fileId)
|
||||
.then(fileURI => {
|
||||
assert.ok(is.string(fileURI));
|
||||
assert.equal(fileURI.indexOf('https'), 0);
|
||||
// TODO: validate URL with some library or regexp
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#downloadFile', function () {
|
||||
describe('#downloadFile', function downloadFileSuite() {
|
||||
const downloadPath = __dirname;
|
||||
|
||||
var downloadPath = __dirname;
|
||||
|
||||
it('should download a file', function (done) {
|
||||
|
||||
var bot = new Telegram(TOKEN);
|
||||
var photo = __dirname + '/bot.gif';
|
||||
it('should download a file', function test() {
|
||||
const bot = new Telegram(TOKEN);
|
||||
const photo = `${__dirname}/bot.gif`;
|
||||
|
||||
// Send a file to get the ID
|
||||
bot.sendPhoto(USERID, photo).then(function (resp) {
|
||||
resp.should.be.an.instanceOf(Object);
|
||||
var fileId = resp.photo[0].file_id;
|
||||
return bot.sendPhoto(USERID, photo).then(resp => {
|
||||
assert.ok(is.object(resp));
|
||||
const fileId = resp.photo[0].file_id;
|
||||
|
||||
bot.downloadFile(fileId, downloadPath)
|
||||
.then(function (filePath) {
|
||||
filePath.should.be.an.instanceOf(String);
|
||||
fs.existsSync(filePath).should.be.true();
|
||||
return bot
|
||||
.downloadFile(fileId, downloadPath)
|
||||
.then(filePath => {
|
||||
assert.ok(is.string(filePath));
|
||||
assert.ok(fs.existsSync(filePath));
|
||||
fs.unlinkSync(filePath); // Delete file after test
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it('should call `onText` callback on match', function (done) {
|
||||
var bot = new Telegram(TOKEN, {webHook: true});
|
||||
bot.onText(/\/echo (.+)/, function (msg, match) {
|
||||
it('should call `onText` callback on match', function test(done) {
|
||||
const bot = new Telegram(TOKEN, { webHook: true });
|
||||
bot.onText(/\/echo (.+)/, (msg, match) => {
|
||||
bot._WebHook._webServer.close();
|
||||
match[1].should.be.exactly('ECHO ALOHA');
|
||||
assert.equal(match[1], 'ECHO ALOHA');
|
||||
done();
|
||||
});
|
||||
var url = 'http://localhost:8443/bot'+TOKEN;
|
||||
const url = `http://localhost:8443/bot${TOKEN}`;
|
||||
request({
|
||||
url: url,
|
||||
url,
|
||||
method: 'POST',
|
||||
json: true,
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
body: {update_id: 0, message: {text: '/echo ECHO ALOHA'}}
|
||||
body: { update_id: 0, message: { text: '/echo ECHO ALOHA' } }
|
||||
});
|
||||
});
|
||||
}); // End Telegram
|
||||
|
||||
|
||||
describe('#TelegramBotPolling', function () {
|
||||
it('should call the callback on polling', function (done) {
|
||||
var opts = {interval: 100, timeout: 1};
|
||||
var polling = new TelegramPolling(TOKEN, opts, function (msg) {
|
||||
describe('#TelegramBotPolling', function TelegramBotPollingSuite() {
|
||||
it('should call the callback on polling', function test(done) {
|
||||
const opts = { interval: 100, timeout: 1 };
|
||||
const polling = new TelegramPolling(TOKEN, opts, (msg) => {
|
||||
if (msg.update_id === 10) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
// The second time _getUpdates is called it will return a message
|
||||
// Really dirty but it works
|
||||
polling._getUpdates = function () {
|
||||
return new Promise.resolve([{update_id: 10, message: {}}]);
|
||||
polling._getUpdates = () => {
|
||||
return new Promise.resolve([{ update_id: 10, message: {} }]);
|
||||
};
|
||||
});
|
||||
});
|
||||
|
1
test/mocha.opts
Normal file
1
test/mocha.opts
Normal file
@ -0,0 +1 @@
|
||||
--require babel-register
|
Loading…
x
Reference in New Issue
Block a user