Skip to content

Commit

Permalink
Socket hangup on GetUpdates
Browse files Browse the repository at this point in the history
Details of change:
 - added new parameter into api._call() method
   allowing to configure connection related properties
 - added parameter timeout to request calls

Issue #74
  • Loading branch information
mast committed Aug 10, 2020
1 parent 20d6883 commit b2429f2
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
6 changes: 4 additions & 2 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class Api extends EventEmitter {
}
}

_call(methodName, params) {
_call(methodName, params, config) {
return new Promise((resolve, reject) => {

for (const key in params) {
Expand All @@ -172,10 +172,12 @@ class Api extends EventEmitter {

debug('Do (%s) with params => %o', methodName, params)

const timeout = config && config.timeout ? config.timeout : 2000
this._request({
method: 'POST',
uri: this._getBaseUrl() + methodName,
formData: params ? params : null
formData: params ? params : null,
timeout: timeout
}, (err, response, body) => {
if (err) {
debug('Telegram error: %o', err)
Expand Down
2 changes: 2 additions & 0 deletions lib/providers/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class UpdateProvider {
limit: this._limit ? this._limit: undefined,
timeout: this._timeout ? this._timeout: undefined,
allowed_updates: this._allowed_updates ? this._allowed_updates: undefined
}, {
timeout: 1000*this._timeout + 1000
})
.then(json => {
if (!Array.isArray(json)) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "telegram-bot-api",
"description": "First Telegram Bot API node.js library",
"version": "2.0.0",
"version": "2.0.1",
"repository": {
"type": "git",
"url": "https://github.com/mast/telegram-bot-api.git"
Expand Down
3 changes: 2 additions & 1 deletion test/api.methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ describe('Api methods', () => {
obj: "{\"a\":1}",
file: this.file
},
uri: 'https://api.telegram.org/bot123/' + method
uri: 'https://api.telegram.org/bot123/' + method,
timeout: 2000
}

this.api[method](callParams)
Expand Down
12 changes: 6 additions & 6 deletions test/provider.getupdate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('Provider => GetUpdate', () => {
limit: undefined,
offset: 0,
timeout: 60
})
}, {timeout: 61000})

expect(mp._offset).toBe(0)
expect(setTimeout).toHaveBeenCalledTimes(2)
Expand Down Expand Up @@ -146,7 +146,7 @@ describe('Provider => GetUpdate', () => {
limit: undefined,
offset: 0,
timeout: 60
})
}, {timeout: 61000})

expect(mp._offset).toBe(2)
expect(api._onUpdate).toHaveBeenLastCalledWith({update_id: 1, data: {}})
Expand All @@ -169,7 +169,7 @@ describe('Provider => GetUpdate', () => {
limit: undefined,
offset: 2,
timeout: 60
})
}, {timeout: 61000})

expect(mp._offset).toBe(51)
expect(setTimeout).toHaveBeenCalledTimes(3)
Expand Down Expand Up @@ -209,7 +209,7 @@ describe('Provider => GetUpdate', () => {
limit: undefined,
offset: 0,
timeout: 60
})
}, {timeout: 61000})

expect(mp._offset).toBe(3)
expect(api._onUpdate.mock.calls.length).toBe(2)
Expand Down Expand Up @@ -244,7 +244,7 @@ describe('Provider => GetUpdate', () => {
limit: undefined,
offset: 0,
timeout: 60
})
}, {timeout: 61000})

expect(mp._offset).toBe(0)
expect(setTimeout).toHaveBeenCalledTimes(2)
Expand Down Expand Up @@ -277,7 +277,7 @@ describe('Provider => GetUpdate', () => {
limit: undefined,
offset: 0,
timeout: 60
})
}, {timeout: 61000})

expect(mp._offset).toBe(0)
expect(setTimeout).toHaveBeenCalledTimes(2)
Expand Down

0 comments on commit b2429f2

Please sign in to comment.