Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize error handling #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,27 @@ api.on('update', function(message)
console.log(message);
});

```
### Error messages

Also you can catch and handle errors

```
api.on('polling_error', function(error)
{
// Handle error
});

api.on('parse_error', function(error)
{
// Handle error
});

api.on('webhook_error', function(error)
{
// Handle error
});

```

## Example (send photo)
Expand Down Expand Up @@ -176,6 +197,7 @@ You should pass configuration object to API constructor, which have following fi
| updates.enabled | Optional | `true` – API will listen for messages and provide you with callback. `false` – API will not listen for messages, care about it by yourself. Default `false` |
| updates.get_interval | Optional | This number of milliseconds API will poll Telegram servers for messages. Default `1000` |
| updates.pooling_timeout | Optional | This number of milliseconds API will keep connection with Telegram servers alive. Default `0` |
| logger | Optional | Logger. Default `console` |

Example of configuration object

Expand Down
23 changes: 13 additions & 10 deletions lib/telegram-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Promise.onPossiblyUnhandledRejection(function(error) {
* webhook
* enabled True if you want to receive updates via webhook
* (one one of updates.enabled or webhook.enabled could be true)
* logger logger for bot (default console)
*/

var TelegramApi = function (params)
Expand Down Expand Up @@ -81,7 +82,8 @@ var TelegramApi = function (params)
allowed_updates: [],
host: '0.0.0.0',
port: 8443
}
},
logger: console
};

// Extend default settings with passed params
Expand All @@ -90,7 +92,7 @@ var TelegramApi = function (params)
// Warn use in case if he miss some important params
if (!_settings.token)
{
console.error('[TelegramBot]: you should pass access token in params');
_settings.logger.error('[TelegramBot]: you should pass access token in params');
}

// Validate params
Expand Down Expand Up @@ -187,8 +189,8 @@ var TelegramApi = function (params)
})
.catch(function(err)
{
console.error('[TelegramBot]: Failed to get updates from Telegram servers');
console.error(err)
_settings.logger.error('[TelegramBot]: Failed to get updates from Telegram servers');
self.emit('polling_error', err);

setTimeout(internalGetUpdates, _settings.updates.get_interval);
});
Expand Down Expand Up @@ -1787,8 +1789,8 @@ var TelegramApi = function (params)
}
}
catch(err) {
console.log(err)
console.log('[TelegramApi]: failed to parse update object')
_settings.logger.error('[TelegramApi]: failed to parse update object')
self.emit('parse_error', err)
}

res.status(200)
Expand All @@ -1806,7 +1808,7 @@ var TelegramApi = function (params)
// Started listening
// setWebhook
var url = _settings.webhook.url ? _settings.webhook.url + ':' +_settings.webhook.port : 'https://' + _settings.webhook.host + ':' + _settings.webhook.port
console.log(url)
_settings.logger.info(url)

this.setWebhook({
url: url + '/' + _settings.token,
Expand All @@ -1815,11 +1817,12 @@ var TelegramApi = function (params)
allowed_updates: _settings.webhook.allowed_updates
})
.then((d) => {
console.log('DONE')
console.log(d)
_settings.logger.info('DONE')
_settings.logger.info(d)
})
.catch((err) => {
console.log(err)
_settings.logger.error('[TelegramApi]: failed to set webhook')
self.emit('webhook_error', err)
//throw err
})

Expand Down