Comparing version 0.5.0 to 0.6.0
@@ -8,2 +8,3 @@ var debug = require('debug')('telegraf:core') | ||
var https = require('https') | ||
var http = require('http') | ||
var memorySession = require('./memory-session') | ||
@@ -77,12 +78,11 @@ | ||
telegraf.startWebHook = function (path, tlsOptions, port, host) { | ||
this.started = true | ||
if (!tlsOptions) { | ||
throw new Error('tlsOptions required') | ||
} | ||
var self = this | ||
this.webhookServer = https.createServer(tlsOptions, (req, res) => { | ||
if (req.method !== 'POST' || req.url !== `/${path}`) { | ||
telegraf.webHookCallback = function (webHookPath) { | ||
webHookPath = webHookPath || '/' | ||
return (req, res, next) => { | ||
if (req.method !== 'POST' || req.url !== `${webHookPath}`) { | ||
if(next && typeof next === 'function'){ | ||
return next() | ||
} | ||
res.statusCode = 403 | ||
res.end() | ||
return res.end() | ||
} | ||
@@ -96,18 +96,27 @@ var body = '' | ||
var update = JSON.parse(body) | ||
self.handleUpdate(update) | ||
this.handleUpdate(update) | ||
.then(function () { | ||
res.writeHead(200) | ||
res.end('OK') | ||
res.end() | ||
}) | ||
.catch(function (err) { | ||
res.writeHead(500) | ||
res.end(err.message) | ||
res.end() | ||
}) | ||
} catch (error) { | ||
self.onError(error) | ||
res.writeHead(500) | ||
res.end('Error') | ||
this.onError(error) | ||
res.writeHead(415) | ||
res.end() | ||
} | ||
}) | ||
}).listen(port, host) | ||
} | ||
} | ||
telegraf.startWebHook = function (webHookPath, tlsOptions, port, host) { | ||
this.started = true | ||
if (tlsOptions) { | ||
this.webhookServer = https.createServer(tlsOptions, this.webHookCallback(webHookPath)).listen(port, host) | ||
} else { | ||
this.webhookServer = http.createServer(this.webHookCallback(webHookPath)).listen(port, host) | ||
} | ||
return this | ||
@@ -114,0 +123,0 @@ } |
{ | ||
"name": "telegraf", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Telegram bot framework", | ||
@@ -5,0 +5,0 @@ "main": "lib/telegraf.js", |
@@ -159,2 +159,48 @@ # Telegraf | ||
## Telegram WebHook | ||
```js | ||
var telegraf = new Telegraf(process.env.BOT_TOKEN) | ||
// TLS options | ||
var tlsOptions = { | ||
key: fs.readFileSync('server-key.pem'), | ||
cert: fs.readFileSync('server-cert.pem'), | ||
// This is necessary only if the client uses the self-signed certificate. | ||
ca: [ fs.readFileSync('client-cert.pem') ] | ||
} | ||
// Set telegram webhook | ||
telegraf.setWebHook('https://server.tld:8443/secret-path', {content: 'server-cert.pem'}) | ||
// Start https webhook | ||
telegraf.startWebHook('/secret-path', tlsOptions, 8443) | ||
// Http webhook, for nginx/heroku users. | ||
telegraf.startWebHook('/secret-path', null, 5000) | ||
// Use webHookCallback() if you want attach telegraf to existing http server | ||
require('http').createServer(telegraf.webHookCallback('/secret-path')).listen(3000); | ||
require('https').createServer(tlsOptions, telegraf.webHookCallback('/secret-path')).listen(8443); | ||
// Connect/Express.js integration | ||
var express = require('express') | ||
var app = express() | ||
app.use(telegraf.webHookCallback('/hey')) | ||
app.get('/', function (req, res) { | ||
res.send('Hello World!') | ||
}) | ||
app.listen(3000, function () { | ||
console.log('Example app listening on port 3000!') | ||
}) | ||
``` | ||
## Error Handling | ||
@@ -178,4 +224,5 @@ | ||
* [`new Telegraf(token)`](#new) | ||
* [`.webHookCallback(webHookPath)`](#webhookcallback) | ||
* [`.setWebHook(url, cert)`](#setwebhook) | ||
* [`.startWebHook(path, tlsOptions, port, [host])`](#startWebHook) | ||
* [`.startWebHook(webHookPath, tlsOptions, port, [host])`](#startWebHook) | ||
* [`.startPolling(timeout, limit)`](#startPolling) | ||
@@ -221,2 +268,14 @@ * [`.stop()`](#stop) | ||
<a name="webhookcallback"></a> | ||
#### `Telegraf.webHookCallback(webHookPath)` => `Function` | ||
Return a callback function suitable for the http[s].createServer() method to handle a request. | ||
You may also use this callback function to mount your telegraf app in a Koa/Connect/Express app. | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| webHookPath | `String` | Webhook url path (see Telegraf.setWebHook) | | ||
* * * | ||
<a name="setwebhook"></a> | ||
@@ -233,2 +292,3 @@ #### `Telegraf.setWebHook(url, [cert])` => `Promise` | ||
[Related Telegram api docs](https://core.telegram.org/bots/api#setwebhook) | ||
* * * | ||
@@ -243,4 +303,4 @@ | ||
| --- | --- | --- | | ||
| path | `String` | Url path (see Telegraf.setWebHook) | | ||
| tlsOptions | `Object` | [tls server options](https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener) | | ||
| webHookPath | `String` | Webhook url path (see Telegraf.setWebHook) | | ||
| tlsOptions | `Object` | (Optional) Pass null to use http [tls server options](https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener) | | ||
| port | `Int` | Port number | | ||
@@ -247,0 +307,0 @@ | host | `String` | Hostname | |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
39056
501
781