Skip to content

Commit 3d667b4

Browse files
committed
First working version with tests
0 parents  commit 3d667b4

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

index.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
var Duplex = require('stream').Duplex
2+
3+
var inherits = require('inherits')
4+
var Telegram = require('telegram-bot-api')
5+
6+
7+
/**
8+
* Send data as messages to a Telegram chat
9+
*
10+
* @param {Object} [options]
11+
* @param {Integer} [options.worksheet=1]
12+
*/
13+
function TelegramLog(token, chat_id, options)
14+
{
15+
if(!(this instanceof TelegramLog))
16+
return new TelegramLog(token, chat_id, options)
17+
18+
var self = this
19+
20+
options = options || {}
21+
options.objectMode = true
22+
23+
TelegramLog.super_.call(this, options)
24+
25+
26+
if(!token) throw 'Missing token'
27+
if(!chat_id) throw 'Missing chat_id'
28+
29+
30+
var api = new Telegram(
31+
{
32+
token: token,
33+
updates:
34+
{
35+
enabled: true
36+
}
37+
})
38+
39+
api.on('message', function(message)
40+
{
41+
if(message.chat.id !== chat_id)
42+
{
43+
var error = new Error('Received message for not-listening chat')
44+
error.data = message
45+
46+
return self.emit('error', error)
47+
}
48+
49+
self.push(message.text)
50+
})
51+
52+
53+
/**
54+
* Write a streamed row on the worksheet
55+
*
56+
* @param {Object} data
57+
* @param {*} _ - ignored
58+
* @param {Function} callback
59+
*
60+
* @private
61+
*/
62+
this._write = function(chunk, _, callback)
63+
{
64+
if(chunk == null || chunk == '') return callback()
65+
66+
api.sendMessage(
67+
{
68+
chat_id: chat_id,
69+
text: JSON.stringify(chunk)
70+
})
71+
.then(callback.bind(null, null), callback)
72+
}
73+
}
74+
inherits(TelegramLog, Duplex)
75+
76+
77+
TelegramLog.prototype._read = function(){}
78+
79+
80+
module.exports = TelegramLog

package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "telegram-bot-log-stream",
3+
"version": "0.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "mocha"
8+
},
9+
"keywords": [
10+
"Context Broker",
11+
"Telegram",
12+
"stream"
13+
],
14+
"author": "Telefónica I+D",
15+
"contributors": [
16+
"Jesús Leganés Combarro 'piranna' <[email protected]>"
17+
],
18+
"license": "AGPL-3.0",
19+
"devDependencies": {
20+
"mocha": "^2.4.5",
21+
"nock": "^7.0.2"
22+
},
23+
"dependencies": {
24+
"inherits": "^2.0.1",
25+
"telegram-bot-api": "^1.0.0"
26+
}
27+
}

test.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
var assert = require('assert')
2+
3+
var nock = require('nock')
4+
5+
var TelegramLog = require('./index')
6+
7+
nock.disableNetConnect()
8+
9+
10+
const SERVER = 'api.telegram.org'
11+
const TOKEN = 'token'
12+
const CHAT_ID = 'chat_id'
13+
14+
15+
var server = nock('https://'+SERVER)
16+
17+
18+
afterEach(nock.cleanAll)
19+
20+
21+
it('receive update', function(done)
22+
{
23+
var expected = 'asdf'
24+
25+
server.get('/bot'+TOKEN+'/getUpdates').reply(200,
26+
{
27+
result:
28+
[
29+
{
30+
update_id: 0,
31+
message:
32+
{
33+
chat:
34+
{
35+
id: CHAT_ID
36+
},
37+
text: expected
38+
}
39+
}
40+
]
41+
})
42+
43+
44+
var log = TelegramLog(TOKEN, CHAT_ID)
45+
46+
log.on('error', done)
47+
log.on('data', function(data)
48+
{
49+
assert.equal(data, expected)
50+
done()
51+
})
52+
})
53+
54+
it('send message', function(done)
55+
{
56+
var expected = {a: 'b'}
57+
58+
server.get('/bot'+TOKEN+'/getUpdates').reply(200,
59+
{
60+
result:
61+
[{
62+
update_id: 0,
63+
message: {chat: {id: CHAT_ID}}
64+
}]
65+
})
66+
server.post('/bot'+TOKEN+'/sendMessage').reply(function(uri)
67+
{
68+
done()
69+
})
70+
71+
72+
var log = TelegramLog(TOKEN, CHAT_ID)
73+
74+
log.on('error', done)
75+
76+
log.write(expected)
77+
})

0 commit comments

Comments
 (0)