|
|
|
@ -1,84 +1,91 @@ |
|
|
|
'use strict'; |
|
|
|
const TelegramPolling = require('../src/telegramPolling'); |
|
|
|
|
|
|
|
const Telegram = require('../index'); |
|
|
|
var TelegramPolling = require('../src/telegramPolling'); |
|
|
|
const Promise = require('bluebird'); |
|
|
|
var Telegram = require('../index'); |
|
|
|
const request = require('request-promise'); |
|
|
|
var Promise = require('bluebird'); |
|
|
|
const assert = require('assert'); |
|
|
|
var request = require('request'); |
|
|
|
const fs = require('fs'); |
|
|
|
var should = require('should'); |
|
|
|
const is = require('is'); |
|
|
|
var fs = require('fs'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; |
|
|
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; |
|
|
|
var TOKEN = process.env.TEST_TELEGRAM_TOKEN; |
|
|
|
const TOKEN = process.env.TEST_TELEGRAM_TOKEN; |
|
|
|
if (!TOKEN) { |
|
|
|
if (!TOKEN) { |
|
|
|
throw new Error('Bot token not provided'); |
|
|
|
throw new Error('Bot token not provided'); |
|
|
|
} |
|
|
|
} |
|
|
|
// Telegram service if not User Id
|
|
|
|
|
|
|
|
var USERID = process.env.TEST_USER_ID || 777000; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
describe('Telegram', function () { |
|
|
|
// Telegram service if not User Id
|
|
|
|
|
|
|
|
const USERID = process.env.TEST_USER_ID || 777000; |
|
|
|
|
|
|
|
|
|
|
|
describe('#setWebHook', function () { |
|
|
|
describe('Telegram', function telegramSuite() { |
|
|
|
it('should set a webHook', function (done) { |
|
|
|
describe('#setWebHook', function setWebHookSuite() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
it('should set a webHook', function test() { |
|
|
|
|
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
// Google IP ¯\_(ツ)_/¯
|
|
|
|
// Google IP ¯\_(ツ)_/¯
|
|
|
|
bot.setWebHook('216.58.210.174').then(function (resp) { |
|
|
|
return bot |
|
|
|
resp.should.be.exactly(true); |
|
|
|
.setWebHook('216.58.210.174') |
|
|
|
done(); |
|
|
|
.then(resp => { |
|
|
|
|
|
|
|
assert.equal(resp, true); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should set a webHook with certificate', function (done) { |
|
|
|
it('should set a webHook with certificate', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
var cert = __dirname+'/../examples/crt.pem'; |
|
|
|
const cert = `${__dirname}/../examples/crt.pem`; |
|
|
|
bot.setWebHook('216.58.210.174', cert).then(function (resp) { |
|
|
|
return bot |
|
|
|
resp.should.be.exactly(true); |
|
|
|
.setWebHook('216.58.210.174', cert) |
|
|
|
done(); |
|
|
|
.then(resp => { |
|
|
|
|
|
|
|
assert.equal(resp, true); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should delete the webHook', function (done) { |
|
|
|
it('should delete the webHook', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
bot.setWebHook('').then(function (resp) { |
|
|
|
return bot |
|
|
|
resp.should.be.exactly(true); |
|
|
|
.setWebHook('') |
|
|
|
done(); |
|
|
|
.then(resp => { |
|
|
|
|
|
|
|
assert.equal(resp, true); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('#WebHook', function () { |
|
|
|
describe('#WebHook', function WebHookSuite() { |
|
|
|
it('should reject request if same token not provided', function (done) { |
|
|
|
it('should reject request if same token not provided', function test() { |
|
|
|
var bot = new Telegram(TOKEN, {webHook: true}); |
|
|
|
const bot = new Telegram(TOKEN, { webHook: true }); |
|
|
|
request({ |
|
|
|
|
|
|
|
|
|
|
|
return request({ |
|
|
|
url: 'http://localhost:8443/NOT_REAL_TOKEN', |
|
|
|
url: 'http://localhost:8443/NOT_REAL_TOKEN', |
|
|
|
method: 'POST' |
|
|
|
method: 'POST', |
|
|
|
}, function (error, response, body) { |
|
|
|
simple: false, |
|
|
|
response.statusCode.should.not.be.equal(200); |
|
|
|
resolveWithFullResponse: true |
|
|
|
|
|
|
|
}).then(response => { |
|
|
|
|
|
|
|
assert.notEqual(response.statusCode, 200); |
|
|
|
bot._WebHook._webServer.close(); |
|
|
|
bot._WebHook._webServer.close(); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should reject request if authorized but not a POST', function (done) { |
|
|
|
it('should reject request if authorized but not a POST', function test() { |
|
|
|
var bot = new Telegram(TOKEN, {webHook: true}); |
|
|
|
const bot = new Telegram(TOKEN, { webHook: true }); |
|
|
|
request({ |
|
|
|
return request({ |
|
|
|
url: 'http://localhost:8443/bot'+TOKEN, |
|
|
|
url: `http://localhost:8443/bot${TOKEN}`, |
|
|
|
method: 'GET' |
|
|
|
method: 'GET', |
|
|
|
}, function (error, response, body) { |
|
|
|
simple: false, |
|
|
|
response.statusCode.should.not.be.equal(200); |
|
|
|
resolveWithFullResponse: true |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.then(response => { |
|
|
|
|
|
|
|
assert.notEqual(response.statusCode, 200); |
|
|
|
bot._WebHook._webServer.close(); |
|
|
|
bot._WebHook._webServer.close(); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should emit a `message` on HTTP WebHook', function (done) { |
|
|
|
it('should emit a `message` on HTTP WebHook', function test(done) { |
|
|
|
var bot = new Telegram(TOKEN, {webHook: true}); |
|
|
|
const bot = new Telegram(TOKEN, { webHook: true }); |
|
|
|
bot.on('message', function (msg) { |
|
|
|
bot.on('message', () => { |
|
|
|
bot._WebHook._webServer.close(); |
|
|
|
bot._WebHook._webServer.close(); |
|
|
|
done(); |
|
|
|
done(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
var url = 'http://localhost:8443/bot'+TOKEN; |
|
|
|
|
|
|
|
|
|
|
|
const url = `http://localhost:8443/bot${TOKEN}`; |
|
|
|
request({ |
|
|
|
request({ |
|
|
|
url: url, |
|
|
|
url, |
|
|
|
method: 'POST', |
|
|
|
method: 'POST', |
|
|
|
json: true, |
|
|
|
json: true, |
|
|
|
headers: { |
|
|
|
headers: { |
|
|
|
@ -88,22 +95,22 @@ describe('Telegram', function () { |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should emit a `message` on HTTPS WebHook', function (done) { |
|
|
|
it('should emit a `message` on HTTPS WebHook', function test(done) { |
|
|
|
var opts = { |
|
|
|
const opts = { |
|
|
|
webHook: { |
|
|
|
webHook: { |
|
|
|
port: 8443, |
|
|
|
port: 8443, |
|
|
|
key: __dirname+'/../examples/key.pem', |
|
|
|
key: `${__dirname}/../examples/key.pem`, |
|
|
|
cert: __dirname+'/../examples/crt.pem' |
|
|
|
cert: `${__dirname}/../examples/crt.pem` |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
var bot = new Telegram(TOKEN, opts); |
|
|
|
const bot = new Telegram(TOKEN, opts); |
|
|
|
bot.on('message', function (msg) { |
|
|
|
bot.on('message', () => { |
|
|
|
bot._WebHook._webServer.close(); |
|
|
|
bot._WebHook._webServer.close(); |
|
|
|
done(); |
|
|
|
done(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
var url = 'https://localhost:8443/bot'+TOKEN; |
|
|
|
const url = `https://localhost:8443/bot${TOKEN}`; |
|
|
|
request({ |
|
|
|
request({ |
|
|
|
url: url, |
|
|
|
url, |
|
|
|
method: 'POST', |
|
|
|
method: 'POST', |
|
|
|
json: true, |
|
|
|
json: true, |
|
|
|
headers: { |
|
|
|
headers: { |
|
|
|
@ -115,396 +122,363 @@ describe('Telegram', function () { |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('#getMe', function () { |
|
|
|
describe('#getMe', function getMeSuite() { |
|
|
|
it('should return an User object', function (done) { |
|
|
|
it('should return an User object', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
bot.getMe().then(function (resp) { |
|
|
|
return bot.getMe().then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('#getUpdates', function () { |
|
|
|
describe('#getUpdates', function getUpdatesSuite() { |
|
|
|
it('should return an Array', function (done) { |
|
|
|
it('should return an Array', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
bot.getUpdates().then(function (resp) { |
|
|
|
return bot.getUpdates().then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Array); |
|
|
|
assert.equal(Array.isArray(resp), true); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('#sendMessage', function () { |
|
|
|
describe('#sendMessage', function sendMessageSuite() { |
|
|
|
it('should send a message', function (done) { |
|
|
|
it('should send a message', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
bot.sendMessage(USERID, 'test').then(function (resp) { |
|
|
|
return bot.sendMessage(USERID, 'test').then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('#forwardMessage', function () { |
|
|
|
describe('#forwardMessage', function forwardMessageSuite() { |
|
|
|
it('should forward a message', function (done) { |
|
|
|
it('should forward a message', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
bot.sendMessage(USERID, 'test').then(function (resp) { |
|
|
|
return bot.sendMessage(USERID, 'test').then(resp => { |
|
|
|
var messageId = resp.message_id; |
|
|
|
const messageId = resp.message_id; |
|
|
|
bot.forwardMessage(USERID, USERID, messageId) |
|
|
|
return bot.forwardMessage(USERID, USERID, messageId) |
|
|
|
.then(function (resp) { |
|
|
|
.then(forwarded => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(forwarded)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('#sendPhoto', function () { |
|
|
|
describe('#sendPhoto', function sendPhotoSuite() { |
|
|
|
var photoId; |
|
|
|
let photoId; |
|
|
|
it('should send a photo from file', function (done) { |
|
|
|
it('should send a photo from file', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
var photo = __dirname+'/bot.gif'; |
|
|
|
const photo = `${__dirname}/bot.gif`; |
|
|
|
bot.sendPhoto(USERID, photo).then(function (resp) { |
|
|
|
return bot.sendPhoto(USERID, photo).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
photoId = resp.photo[0].file_id; |
|
|
|
photoId = resp.photo[0].file_id; |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should send a photo from id', function (done) { |
|
|
|
it('should send a photo from id', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
// Send the same photo as before
|
|
|
|
// Send the same photo as before
|
|
|
|
var photo = photoId; |
|
|
|
const photo = photoId; |
|
|
|
bot.sendPhoto(USERID, photo).then(function (resp) { |
|
|
|
return bot.sendPhoto(USERID, photo).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should send a photo from fs.readStream', function (done) { |
|
|
|
it('should send a photo from fs.readStream', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
var photo = fs.createReadStream(__dirname+'/bot.gif'); |
|
|
|
const photo = fs.createReadStream(`${__dirname}/bot.gif`); |
|
|
|
bot.sendPhoto(USERID, photo).then(function (resp) { |
|
|
|
return bot.sendPhoto(USERID, photo).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should send a photo from request Stream', function (done) { |
|
|
|
it('should send a photo from request Stream', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
var photo = request('https://telegram.org/img/t_logo.png'); |
|
|
|
const photo = request('https://telegram.org/img/t_logo.png'); |
|
|
|
bot.sendPhoto(USERID, photo).then(function (resp) { |
|
|
|
return bot.sendPhoto(USERID, photo).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should send a photo from a Buffer', function (done) { |
|
|
|
it('should send a photo from a Buffer', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
var photo = fs.readFileSync(__dirname+'/bot.gif'); |
|
|
|
const photo = fs.readFileSync(`${__dirname}/bot.gif`); |
|
|
|
bot.sendPhoto(USERID, photo).then(function (resp) { |
|
|
|
return bot.sendPhoto(USERID, photo).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('#sendChatAction', function () { |
|
|
|
describe('#sendChatAction', function sendChatActionSuite() { |
|
|
|
it('should send a chat action', function (done) { |
|
|
|
it('should send a chat action', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
var action = 'typing'; |
|
|
|
const action = 'typing'; |
|
|
|
bot.sendChatAction(USERID, action).then(function (resp) { |
|
|
|
return bot.sendChatAction(USERID, action).then(resp => { |
|
|
|
resp.should.be.exactly(true); |
|
|
|
assert.equal(resp, true); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('#sendAudio', function () { |
|
|
|
describe('#sendAudio', function sendAudioSuite() { |
|
|
|
it('should send an OGG audio', function (done) { |
|
|
|
it('should send an OGG audio', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
var audio = request('https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg'); |
|
|
|
const audio = request('https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg'); |
|
|
|
bot.sendAudio(USERID, audio).then(function (resp) { |
|
|
|
return bot.sendAudio(USERID, audio).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('#sendDocument', function () { |
|
|
|
describe('#sendDocument', function sendDocumentSuite() { |
|
|
|
var documentId; |
|
|
|
let documentId; |
|
|
|
it('should send a document from file', function (done) { |
|
|
|
it('should send a document from file', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
var document = __dirname+'/bot.gif'; |
|
|
|
const document = `${__dirname}/bot.gif`; |
|
|
|
bot.sendDocument(USERID, document).then(function (resp) { |
|
|
|
return bot.sendDocument(USERID, document).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
documentId = resp.document.file_id; |
|
|
|
documentId = resp.document.file_id; |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should send a document from id', function (done) { |
|
|
|
it('should send a document from id', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
// Send the same photo as before
|
|
|
|
// Send the same photo as before
|
|
|
|
var document = documentId; |
|
|
|
const document = documentId; |
|
|
|
bot.sendDocument(USERID, document).then(function (resp) { |
|
|
|
return bot.sendDocument(USERID, document).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should send a document from fs.readStream', function (done) { |
|
|
|
it('should send a document from fs.readStream', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
var document = fs.createReadStream(__dirname+'/bot.gif'); |
|
|
|
const document = fs.createReadStream(`${__dirname}/bot.gif`); |
|
|
|
bot.sendDocument(USERID, document).then(function (resp) { |
|
|
|
return bot.sendDocument(USERID, document).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should send a document from request Stream', function (done) { |
|
|
|
it('should send a document from request Stream', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
var document = request('https://telegram.org/img/t_logo.png'); |
|
|
|
const document = request('https://telegram.org/img/t_logo.png'); |
|
|
|
bot.sendDocument(USERID, document).then(function (resp) { |
|
|
|
return bot.sendDocument(USERID, document).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should send a document from a Buffer', function (done) { |
|
|
|
it('should send a document from a Buffer', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
var document = fs.readFileSync(__dirname+'/bot.gif'); |
|
|
|
const document = fs.readFileSync(`${__dirname}/bot.gif`); |
|
|
|
bot.sendDocument(USERID, document).then(function (resp) { |
|
|
|
return bot.sendDocument(USERID, document).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('#sendSticker', function () { |
|
|
|
describe('#sendSticker', function sendStickerSuite() { |
|
|
|
var stickerId; |
|
|
|
let stickerId; |
|
|
|
it('should send a sticker from file', function (done) { |
|
|
|
it('should send a sticker from file', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
var sticker = __dirname+'/sticker.webp'; |
|
|
|
const sticker = `${__dirname}/sticker.webp`; |
|
|
|
bot.sendSticker(USERID, sticker).then(function (resp) { |
|
|
|
return bot.sendSticker(USERID, sticker).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
stickerId = resp.sticker.file_id; |
|
|
|
stickerId = resp.sticker.file_id; |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should send a sticker from id', function (done) { |
|
|
|
it('should send a sticker from id', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
// Send the same photo as before
|
|
|
|
// Send the same photo as before
|
|
|
|
bot.sendSticker(USERID, stickerId).then(function (resp) { |
|
|
|
return bot.sendSticker(USERID, stickerId).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should send a sticker from fs.readStream', function (done) { |
|
|
|
it('should send a sticker from fs.readStream', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
var sticker = fs.createReadStream(__dirname+'/sticker.webp'); |
|
|
|
const sticker = fs.createReadStream(`${__dirname}/sticker.webp`); |
|
|
|
bot.sendSticker(USERID, sticker).then(function (resp) { |
|
|
|
return bot.sendSticker(USERID, sticker).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should send a sticker from request Stream', function (done) { |
|
|
|
it('should send a sticker from request Stream', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
var sticker = request('https://www.gstatic.com/webp/gallery3/1_webp_ll.webp'); |
|
|
|
const sticker = request('https://www.gstatic.com/webp/gallery3/1_webp_ll.webp'); |
|
|
|
bot.sendSticker(USERID, sticker).then(function (resp) { |
|
|
|
return bot.sendSticker(USERID, sticker).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should send a sticker from a Buffer', function (done) { |
|
|
|
it('should send a sticker from a Buffer', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
var sticker = fs.readFileSync(__dirname+'/sticker.webp'); |
|
|
|
const sticker = fs.readFileSync(`${__dirname}/sticker.webp`); |
|
|
|
bot.sendDocument(USERID, sticker).then(function (resp) { |
|
|
|
return bot.sendDocument(USERID, sticker).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('#sendVideo', function () { |
|
|
|
describe('#sendVideo', function sendVideoSuite() { |
|
|
|
var videoId; |
|
|
|
let videoId; |
|
|
|
it('should send a video from file', function (done) { |
|
|
|
it('should send a video from file', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
var video = __dirname+'/video.mp4'; |
|
|
|
const video = `${__dirname}/video.mp4`; |
|
|
|
bot.sendVideo(USERID, video).then(function (resp) { |
|
|
|
return bot.sendVideo(USERID, video).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
videoId = resp.video.file_id; |
|
|
|
videoId = resp.video.file_id; |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should send a video from id', function (done) { |
|
|
|
it('should send a video from id', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
// Send the same photo as before
|
|
|
|
// Send the same photo as before
|
|
|
|
bot.sendVideo(USERID, videoId).then(function (resp) { |
|
|
|
return bot.sendVideo(USERID, videoId).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should send a video from fs.readStream', function (done) { |
|
|
|
it('should send a video from fs.readStream', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
var video = fs.createReadStream(__dirname+'/video.mp4'); |
|
|
|
const video = fs.createReadStream(`${__dirname}/video.mp4`); |
|
|
|
bot.sendVideo(USERID, video).then(function (resp) { |
|
|
|
return bot.sendVideo(USERID, video).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should send a video from request Stream', function (done) { |
|
|
|
it('should send a video from request Stream', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
var sticker = request('http://techslides.com/demos/sample-videos/small.mp4'); |
|
|
|
const sticker = request('http://techslides.com/demos/sample-videos/small.mp4'); |
|
|
|
bot.sendVideo(USERID, sticker).then(function (resp) { |
|
|
|
return bot.sendVideo(USERID, sticker).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should send a video from a Buffer', function (done) { |
|
|
|
it('should send a video from a Buffer', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
var video = fs.readFileSync(__dirname+'/video.mp4'); |
|
|
|
const video = fs.readFileSync(`${__dirname}/video.mp4`); |
|
|
|
bot.sendVideo(USERID, video).then(function (resp) { |
|
|
|
return bot.sendVideo(USERID, video).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('#sendVoice', function () { |
|
|
|
describe('#sendVoice', function sendVoiceSuite() { |
|
|
|
it('should send an OGG audio as voice', function (done) { |
|
|
|
it('should send an OGG audio as voice', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
var voice = request('https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg'); |
|
|
|
const voice = request('https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg'); |
|
|
|
bot.sendVoice(USERID, voice).then(function (resp) { |
|
|
|
return bot.sendVoice(USERID, voice).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('#getUserProfilePhotos', function () { |
|
|
|
describe('#getUserProfilePhotos', function getUserProfilePhotosSuite() { |
|
|
|
it('should get user profile photos', function (done) { |
|
|
|
it('should get user profile photos', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
bot.getUserProfilePhotos(USERID).then(function (resp) { |
|
|
|
return bot.getUserProfilePhotos(USERID).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
resp.total_count.should.be.an.instanceOf(Number); |
|
|
|
assert.ok(is.number(resp.total_count)); |
|
|
|
resp.photos.should.be.an.instanceOf(Array); |
|
|
|
assert.ok(is.array(resp.photos)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('#sendLocation', function () { |
|
|
|
describe('#sendLocation', function sendLocationSuite() { |
|
|
|
it('should send a location', function (done) { |
|
|
|
it('should send a location', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
var lat = 47.5351072; |
|
|
|
const lat = 47.5351072; |
|
|
|
var long = -52.7508537; |
|
|
|
const long = -52.7508537; |
|
|
|
bot.sendLocation(USERID, lat, long).then(function (resp) { |
|
|
|
return bot.sendLocation(USERID, lat, long).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
resp.location.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp.location)); |
|
|
|
resp.location.latitude.should.be.an.instanceOf(Number); |
|
|
|
assert.ok(is.number(resp.location.latitude)); |
|
|
|
resp.location.longitude.should.be.an.instanceOf(Number); |
|
|
|
assert.ok(is.number(resp.location.longitude)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('#getFile', function () { |
|
|
|
describe('#getFile', function getFileSuite() { |
|
|
|
var fileId; |
|
|
|
let fileId; |
|
|
|
|
|
|
|
|
|
|
|
// To get a file we have to send any file first
|
|
|
|
// To get a file we have to send any file first
|
|
|
|
it('should send a photo from file', function (done) { |
|
|
|
it('should send a photo from file', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
var photo = __dirname + '/bot.gif'; |
|
|
|
const photo = `${__dirname}/bot.gif`; |
|
|
|
bot.sendPhoto(USERID, photo).then(function (resp) { |
|
|
|
return bot.sendPhoto(USERID, photo).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
fileId = resp.photo[0].file_id; |
|
|
|
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); |
|
|
|
return bot |
|
|
|
|
|
|
|
.getFile(fileId) |
|
|
|
bot.getFile(fileId).then(function (resp) { |
|
|
|
.then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
resp.file_path.should.be.an.instanceOf(String); |
|
|
|
assert.ok(is.string(resp.file_path)); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('#getFileLink', function () { |
|
|
|
describe('#getFileLink', function getFileLinkSuite() { |
|
|
|
var fileId; |
|
|
|
let fileId; |
|
|
|
|
|
|
|
|
|
|
|
// To get a file we have to send any file first
|
|
|
|
// To get a file we have to send any file first
|
|
|
|
it('should send a photo from file', function (done) { |
|
|
|
it('should send a photo from file', function test() { |
|
|
|
var bot = new Telegram(TOKEN); |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
var photo = __dirname + '/bot.gif'; |
|
|
|
const photo = `${__dirname}/bot.gif`; |
|
|
|
bot.sendPhoto(USERID, photo).then(function (resp) { |
|
|
|
return bot.sendPhoto(USERID, photo).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
fileId = resp.photo[0].file_id; |
|
|
|
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); |
|
|
|
return bot |
|
|
|
|
|
|
|
.getFileLink(fileId) |
|
|
|
bot.getFileLink(fileId).then(function (fileURI) { |
|
|
|
.then(fileURI => { |
|
|
|
fileURI.should.be.an.instanceOf(String); |
|
|
|
assert.ok(is.string(fileURI)); |
|
|
|
fileURI.should.startWith('https'); |
|
|
|
assert.equal(fileURI.indexOf('https'), 0); |
|
|
|
done(); // TODO: validate URL with some library or regexp
|
|
|
|
// 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); |
|
|
|
it('should download a file', function test() { |
|
|
|
var photo = __dirname + '/bot.gif'; |
|
|
|
const bot = new Telegram(TOKEN); |
|
|
|
|
|
|
|
const photo = `${__dirname}/bot.gif`; |
|
|
|
|
|
|
|
|
|
|
|
// Send a file to get the ID
|
|
|
|
// Send a file to get the ID
|
|
|
|
bot.sendPhoto(USERID, photo).then(function (resp) { |
|
|
|
return bot.sendPhoto(USERID, photo).then(resp => { |
|
|
|
resp.should.be.an.instanceOf(Object); |
|
|
|
assert.ok(is.object(resp)); |
|
|
|
var fileId = resp.photo[0].file_id; |
|
|
|
const fileId = resp.photo[0].file_id; |
|
|
|
|
|
|
|
|
|
|
|
bot.downloadFile(fileId, downloadPath) |
|
|
|
return bot |
|
|
|
.then(function (filePath) { |
|
|
|
.downloadFile(fileId, downloadPath) |
|
|
|
filePath.should.be.an.instanceOf(String); |
|
|
|
.then(filePath => { |
|
|
|
fs.existsSync(filePath).should.be.true(); |
|
|
|
assert.ok(is.string(filePath)); |
|
|
|
|
|
|
|
assert.ok(fs.existsSync(filePath)); |
|
|
|
fs.unlinkSync(filePath); // Delete file after test
|
|
|
|
fs.unlinkSync(filePath); // Delete file after test
|
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should call `onText` callback on match', function (done) { |
|
|
|
it('should call `onText` callback on match', function test(done) { |
|
|
|
var bot = new Telegram(TOKEN, {webHook: true}); |
|
|
|
const bot = new Telegram(TOKEN, { webHook: true }); |
|
|
|
bot.onText(/\/echo (.+)/, function (msg, match) { |
|
|
|
bot.onText(/\/echo (.+)/, (msg, match) => { |
|
|
|
bot._WebHook._webServer.close(); |
|
|
|
bot._WebHook._webServer.close(); |
|
|
|
match[1].should.be.exactly('ECHO ALOHA'); |
|
|
|
assert.equal(match[1], 'ECHO ALOHA'); |
|
|
|
done(); |
|
|
|
done(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
var url = 'http://localhost:8443/bot'+TOKEN; |
|
|
|
const url = `http://localhost:8443/bot${TOKEN}`; |
|
|
|
request({ |
|
|
|
request({ |
|
|
|
url: url, |
|
|
|
url, |
|
|
|
method: 'POST', |
|
|
|
method: 'POST', |
|
|
|
json: true, |
|
|
|
json: true, |
|
|
|
headers: { |
|
|
|
headers: { |
|
|
|
@ -515,18 +489,17 @@ describe('Telegram', function () { |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); // End Telegram
|
|
|
|
}); // End Telegram
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
describe('#TelegramBotPolling', function TelegramBotPollingSuite() { |
|
|
|
describe('#TelegramBotPolling', function () { |
|
|
|
it('should call the callback on polling', function test(done) { |
|
|
|
it('should call the callback on polling', function (done) { |
|
|
|
const opts = { interval: 100, timeout: 1 }; |
|
|
|
var opts = {interval: 100, timeout: 1}; |
|
|
|
const polling = new TelegramPolling(TOKEN, opts, (msg) => { |
|
|
|
var polling = new TelegramPolling(TOKEN, opts, function (msg) { |
|
|
|
|
|
|
|
if (msg.update_id === 10) { |
|
|
|
if (msg.update_id === 10) { |
|
|
|
done(); |
|
|
|
done(); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
// The second time _getUpdates is called it will return a message
|
|
|
|
// The second time _getUpdates is called it will return a message
|
|
|
|
// Really dirty but it works
|
|
|
|
// Really dirty but it works
|
|
|
|
polling._getUpdates = function () { |
|
|
|
polling._getUpdates = () => { |
|
|
|
return new Promise.resolve([{ update_id: 10, message: {} }]); |
|
|
|
return new Promise.resolve([{ update_id: 10, message: {} }]); |
|
|
|
}; |
|
|
|
}; |
|
|
|
}); |
|
|
|
}); |
|
|
|
|