Comparing version 0.6.1 to 0.6.2
@@ -56,3 +56,2 @@ var debug = require('debug')('telegraf:core') | ||
Telegraf.memorySession = memorySession | ||
var telegraf = Telegraf.prototype | ||
@@ -82,3 +81,3 @@ | ||
if (req.method !== 'POST' || req.url !== `${webHookPath}`) { | ||
if(next && typeof next === 'function'){ | ||
if (next && typeof next === 'function') { | ||
return next() | ||
@@ -167,3 +166,3 @@ } | ||
if (result) { | ||
this.__defineGetter__('match', () => { | ||
this.__defineGetter__('match', function () { | ||
return result || [] | ||
@@ -192,6 +191,3 @@ }) | ||
telegraf.getFileLink = function (fileId) { | ||
return this.getFile(fileId) | ||
.then((response) => { | ||
return `https://api.telegram.org/file/bot${this.token}/${response.file_path}` | ||
}) | ||
return this.getFile(fileId).then((response) => `https://api.telegram.org/file/bot${this.token}/${response.file_path}`) | ||
} | ||
@@ -404,5 +400,3 @@ | ||
return fetch(`https://api.telegram.org/bot${this.token}/${path}`, payload) | ||
.then((res) => { | ||
return res.json() | ||
}) | ||
.then((res) => res.json()) | ||
.then((data) => { | ||
@@ -434,4 +428,3 @@ if (data.ok) { | ||
} | ||
var self = this | ||
var tasks = this.handlers | ||
var handlerMiddlewares = this.handlers | ||
.filter((handler) => { | ||
@@ -442,12 +435,10 @@ return handler.eventTypes.filter((n) => { | ||
}) | ||
.map((handler) => { | ||
return handler.middleware | ||
}) | ||
.map((handler) => handler.middleware) | ||
var context = self.createContext(currentTypes[currentTypes.length - 1], payload) | ||
context.use(compose(self.middlewares)) | ||
context.use(compose(tasks)) | ||
context.on('error', self.onError) | ||
var context = this.createContext(currentTypes[currentTypes.length - 1], payload) | ||
context.use(compose(this.middlewares)) | ||
context.use(compose(handlerMiddlewares)) | ||
context.on('error', this.onError) | ||
return context.run().then(() => { | ||
self.offset = update.update_id + 1 | ||
this.offset = update.update_id + 1 | ||
}) | ||
@@ -460,11 +451,15 @@ } | ||
} | ||
var handleUpdate = this.handleUpdate.bind(this) | ||
var updateLoop = this.updateLoop.bind(this) | ||
this.getUpdates(this.options.timeout, this.options.limit, this.offset) | ||
.then(function (updates) { | ||
return Promise.all(updates.map(handleUpdate)) | ||
.catch((err) => { | ||
console.log('Telegraf: network error', err) | ||
return new Promise((resolve) => { | ||
setTimeout(() => resolve([]), 100) | ||
}) | ||
}) | ||
.then(updateLoop) | ||
.catch(function (err) { | ||
debug('Telegraf polling error', err) | ||
.then((updates) => { | ||
return Promise.all(updates.map((update) => this.handleUpdate(update))) | ||
}) | ||
.then(() => this.updateLoop()) | ||
.catch((err) => { | ||
console.log('Telegraf: polling error', err) | ||
this.started = false | ||
@@ -514,2 +509,4 @@ }) | ||
Telegraf.memorySession = memorySession | ||
module.exports = Telegraf |
{ | ||
"name": "telegraf", | ||
"version": "0.6.1", | ||
"description": "Telegram bot framework", | ||
"version": "0.6.2", | ||
"description": "📢 Modern Telegram bot framework", | ||
"main": "lib/telegraf.js", | ||
@@ -6,0 +6,0 @@ "repository": { |
168
readme.md
@@ -1,6 +0,5 @@ | ||
# Telegraf | ||
[![Build Status](https://img.shields.io/travis/telegraf/telegraf.svg?branch=master&style=flat-square)](https://travis-ci.org/telegraf/telegraf) | ||
[![NPM Version](https://img.shields.io/npm/v/telegraf.svg?style=flat-square)](https://www.npmjs.com/package/telegraf) | ||
Modern Telegram bot framework for node.js | ||
📢 Modern Telegram bot framework for node.js | ||
@@ -94,3 +93,3 @@ ## Installation | ||
## Context | ||
### Context | ||
@@ -108,2 +107,3 @@ A Telegraf Context encapsulates telegram message. | ||
this.callbackQuery // Received callback query | ||
this.match // Regex match (available only for `hears` handler) | ||
}); | ||
@@ -127,3 +127,3 @@ ``` | ||
## State | ||
### State | ||
@@ -145,3 +145,3 @@ The recommended namespace to share information between middlewares. | ||
## Session | ||
### Session | ||
@@ -163,3 +163,3 @@ For development you can use `Telegraf.memorySession()`, but session will be lost on app restart. | ||
## Telegram WebHook | ||
### Telegram WebHook | ||
@@ -198,3 +198,3 @@ ```js | ||
app.use(telegraf.webHookCallback('/hey')) | ||
app.use(telegraf.webHookCallback('/secret-path')) | ||
@@ -211,3 +211,3 @@ app.get('/', function (req, res) { | ||
## Error Handling | ||
### Error Handling | ||
@@ -226,2 +226,33 @@ By default Telegraf will print all errors to stderr and rethrow error. | ||
### Shortcuts | ||
Telegraf context have many handy shortcuts. | ||
Note: shortcuts are not available for `inline_query` and `chosen_inline_result` events. | ||
```js | ||
var telegraf = new Telegraf(process.env.BOT_TOKEN) | ||
telegraf.on('text', function * (){ | ||
// Simple usage | ||
telegraf.sendMessage(this.message.chat.id, `Hello ${this.state.role}`) | ||
// Using shortcut | ||
this.reply(`Hello ${this.state.role}`) | ||
// If you want to mark message as reply to source message | ||
this.reply(`Hello ${this.state.role}`, { reply_to_message_id: this.message.id }) | ||
}) | ||
``` | ||
* `reply()` -> `telegraf.sendMessage()` | ||
* `replyWithPhoto()` -> `telegraf.sendPhoto()` | ||
* `replyWithAudio()` -> `telegraf.sendAudio()` | ||
* `replyWithDocument()` -> `telegraf.sendDocument()` | ||
* `replyWithSticker()` -> `telegraf.sendSticker()` | ||
* `replyWithVideo()` -> `telegraf.sendVideo()` | ||
* `replyWithVoice()` -> `telegraf.sendVoice()` | ||
* `replyWithChatAction()` -> `telegraf.sendChatAction()` | ||
* `replyWithLocation()` -> `telegraf.sendLocation()` | ||
## API reference | ||
@@ -263,3 +294,3 @@ | ||
<a name="new"></a> | ||
#### `Telegraf.new(token)` | ||
### `Telegraf.new(token)` | ||
@@ -276,3 +307,3 @@ Initialize new Telegraf app. | ||
<a name="webhookcallback"></a> | ||
#### `Telegraf.webHookCallback(webHookPath)` => `Function` | ||
### `Telegraf.webHookCallback(webHookPath)` => `Function` | ||
@@ -289,3 +320,3 @@ Return a callback function suitable for the http[s].createServer() method to handle a request. | ||
<a name="setwebhook"></a> | ||
#### `Telegraf.setWebHook(url, [cert])` => `Promise` | ||
### `Telegraf.setWebHook(url, [cert])` => `Promise` | ||
@@ -304,3 +335,3 @@ Specifies an url to receive incoming updates via an outgoing webHook. | ||
<a name="startWebHook"></a> | ||
#### `Telegraf.startWebHook(token, tlsOptions, port, [host])` | ||
### `Telegraf.startWebHook(token, tlsOptions, port, [host])` | ||
@@ -312,3 +343,3 @@ Start listening @ `https://host:port/token` for Telegram calls. | ||
| 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) | | ||
| tlsOptions | `[TLS server options](https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener)` | (Optional) Pass null to use http | | ||
| port | `Int` | Port number | | ||
@@ -320,3 +351,3 @@ | host | `String` | Hostname | | ||
<a name="startPolling"></a> | ||
#### `Telegraf.startPolling(timeout, limit)` | ||
### `Telegraf.startPolling(timeout, limit)` | ||
@@ -333,3 +364,3 @@ Start poll updates. | ||
<a name="stop"></a> | ||
#### `Telegraf.stop()` | ||
### `Telegraf.stop()` | ||
@@ -341,3 +372,3 @@ Stop WebHook and polling | ||
<a name="use"></a> | ||
#### `Telegraf.use(middleware)` | ||
### `Telegraf.use(middleware)` | ||
@@ -353,3 +384,3 @@ Registers a middleware. | ||
<a name="on"></a> | ||
#### `Telegraf.on(eventType, handler)` | ||
### `Telegraf.on(eventType, handler)` | ||
@@ -366,3 +397,3 @@ Registers handler for provided [event type](#events). | ||
<a name="hears"></a> | ||
#### `Telegraf.hears(pattern, handler)` | ||
### `Telegraf.hears(pattern, handler)` | ||
@@ -376,6 +407,8 @@ Registers handler only for `text` events using string pattern or RegEx. | ||
* * * | ||
<a name="sendmessage"></a> | ||
#### `Telegraf.sendMessage(chatId, text, extra)` => `Promise` | ||
### `Telegraf.sendMessage(chatId, text, extra)` => `Promise` | ||
@@ -389,7 +422,7 @@ Sends text message. | ||
| extra | `Object` | [Optional parameters](https://core.telegram.org/bots/api#sendmessage)| | ||
[Related Telegram api docs](https://core.telegram.org/bots/api#sendmessage) | ||
* * * | ||
<a name="forwardmessage"></a> | ||
#### `Telegraf.forwardMessage(chatId, fromChatId, messageId, extra)` => `Promise` | ||
### `Telegraf.forwardMessage(chatId, fromChatId, messageId, extra)` => `Promise` | ||
@@ -405,7 +438,6 @@ Forwards message. | ||
[Related Telegram api docs](https://core.telegram.org/bots/api#forwardmessage) | ||
* * * | ||
<a name="sendlocation"></a> | ||
#### `Telegraf.sendLocation(chatId, latitude, longitude, extra)` => `Promise` | ||
### `Telegraf.sendLocation(chatId, latitude, longitude, extra)` => `Promise` | ||
@@ -421,7 +453,6 @@ Sends location. | ||
[Related Telegram api docs](https://core.telegram.org/bots/api#sendlocation) | ||
* * * | ||
<a name="sendphoto"></a> | ||
#### `Telegraf.sendPhoto(chatId, photo, extra)` => `Promise` | ||
### `Telegraf.sendPhoto(chatId, photo, extra)` => `Promise` | ||
@@ -436,7 +467,6 @@ Sends photo. | ||
[Related Telegram api docs](https://core.telegram.org/bots/api#sendphoto) | ||
* * * | ||
<a name="senddocument"></a> | ||
#### `Telegraf.sendDocument(chatId, doc, extra)` => `Promise` | ||
### `Telegraf.sendDocument(chatId, doc, extra)` => `Promise` | ||
@@ -451,7 +481,6 @@ Sends document. | ||
[Related Telegram api docs](https://core.telegram.org/bots/api#senddocument) | ||
* * * | ||
<a name="sendaudio"></a> | ||
#### `Telegraf.sendAudio(chatId, audio, extra)` => `Promise` | ||
### `Telegraf.sendAudio(chatId, audio, extra)` => `Promise` | ||
@@ -466,7 +495,6 @@ Sends audio. | ||
[Related Telegram api docs](https://core.telegram.org/bots/api#sendaudio) | ||
* * * | ||
<a name="sendsticker"></a> | ||
#### `Telegraf.sendSticker(chatId, sticker, extra)` => `Promise` | ||
### `Telegraf.sendSticker(chatId, sticker, extra)` => `Promise` | ||
@@ -481,7 +509,6 @@ Sends sticker. | ||
[Related Telegram api docs](https://core.telegram.org/bots/api#sendsticker) | ||
* * * | ||
<a name="sendvideo"></a> | ||
#### `Telegraf.sendVideo(chatId, video, extra)` => `Promise` | ||
### `Telegraf.sendVideo(chatId, video, extra)` => `Promise` | ||
@@ -496,7 +523,6 @@ Sends video. | ||
[Related Telegram api docs](https://core.telegram.org/bots/api#sendvideo) | ||
* * * | ||
<a name="sendvoice"></a> | ||
#### `Telegraf.sendVoice(chatId, voice, extra)` => `Promise` | ||
### `Telegraf.sendVoice(chatId, voice, extra)` => `Promise` | ||
@@ -511,7 +537,6 @@ Sends voice. | ||
[Related Telegram api docs](https://core.telegram.org/bots/api#sendvoice) | ||
* * * | ||
<a name="sendchataction"></a> | ||
#### `Telegraf.sendChatAction(chatId, action)` => `Promise` | ||
### `Telegraf.sendChatAction(chatId, action)` => `Promise` | ||
@@ -525,7 +550,6 @@ Sends chat action. | ||
[Related Telegram api docs](https://core.telegram.org/bots/api#sendchataction) | ||
* * * | ||
<a name="getme"></a> | ||
#### `Telegraf.getMe()` => `Promise` | ||
### `Telegraf.getMe()` => `Promise` | ||
@@ -535,6 +559,7 @@ Returns basic information about the bot. | ||
[Related Telegram api docs](https://core.telegram.org/bots/api#getme) | ||
* * * | ||
<a name="getuserprofilephotos"></a> | ||
#### `Telegraf.getUserProfilePhotos(userId, offset, limit)` => `Promise` | ||
### `Telegraf.getUserProfilePhotos(userId, offset, limit)` => `Promise` | ||
@@ -550,6 +575,7 @@ Returns profiles photos for provided user. | ||
[Related Telegram api docs](https://core.telegram.org/bots/api#getuserprofilephotos) | ||
* * * | ||
<a name="getfile"></a> | ||
#### `Telegraf.getFile(fileId)` => `Promise` | ||
### `Telegraf.getFile(fileId)` => `Promise` | ||
@@ -563,6 +589,7 @@ Returns basic info about a file and prepare it for downloading. | ||
[Related Telegram api docs](https://core.telegram.org/bots/api#getfile) | ||
* * * | ||
<a name="getFileLink"></a> | ||
#### `Telegraf.getFileLink(fileId)` => `Promise` | ||
### `Telegraf.getFileLink(fileId)` => `Promise` | ||
@@ -577,6 +604,7 @@ Returns link to file. | ||
[Related Telegram api docs](https://core.telegram.org/bots/api#getFileLink) | ||
* * * | ||
<a name="removewebhook"></a> | ||
#### `Telegraf.removeWebHook()` => `Promise` | ||
### `Telegraf.removeWebHook()` => `Promise` | ||
@@ -589,3 +617,3 @@ Removes webhook. Shortcut for `Telegraf.setWebHook('')` | ||
<a name="kickchatmember"></a> | ||
#### `Telegraf.kickChatMember(chatId, userId)` => `Promise` | ||
### `Telegraf.kickChatMember(chatId, userId)` => `Promise` | ||
@@ -600,6 +628,7 @@ Use this method to kick a user from a group or a supergroup. | ||
[Related Telegram api docs](https://core.telegram.org/bots/api#kickchatmember) | ||
* * * | ||
<a name="unbanchatmember"></a> | ||
#### `Telegraf.unbanChatMember(chatId, userId)` => `Promise` | ||
### `Telegraf.unbanChatMember(chatId, userId)` => `Promise` | ||
@@ -617,3 +646,3 @@ Use this method to unban a previously kicked user in a supergroup. | ||
<a name="answerinlinequery"></a> | ||
#### `Telegraf.answerInlineQuery(inlineQueryId, results, extra)` => `Promise` | ||
### `Telegraf.answerInlineQuery(inlineQueryId, results, extra)` => `Promise` | ||
@@ -628,7 +657,6 @@ Use this method to send answers to an inline query. | ||
[Related Telegram api docs](https://core.telegram.org/bots/api#answerinlinequery) | ||
* * * | ||
<a name="answercallbackquery"></a> | ||
#### `Telegraf.answerCallbackQuery(callbackQueryId, text, showAlert)` => `Promise` | ||
### `Telegraf.answerCallbackQuery(callbackQueryId, text, showAlert)` => `Promise` | ||
@@ -644,6 +672,7 @@ Use this method to send answers to callback queries. | ||
[Related Telegram api docs](https://core.telegram.org/bots/api#answercallbackquery) | ||
* * * | ||
<a name="editmessagetext"></a> | ||
#### `Telegraf.editMessageText(chatId, messageId, text, extra)` => `Promise` | ||
### `Telegraf.editMessageText(chatId, messageId, text, extra)` => `Promise` | ||
@@ -659,7 +688,6 @@ Use this method to edit text messages sent by the bot or via the bot. | ||
[Related Telegram api docs](https://core.telegram.org/bots/api#editmessagetext) | ||
* * * | ||
<a name="editmessagecaption"></a> | ||
#### `Telegraf.editMessageCaption(chatId, messageId, caption, extra)` => `Promise` | ||
### `Telegraf.editMessageCaption(chatId, messageId, caption, extra)` => `Promise` | ||
@@ -675,7 +703,6 @@ Use this method to edit captions of messages sent by the bot or via the bot | ||
[Related Telegram api docs](https://core.telegram.org/bots/api#editmessagecaption) | ||
* * * | ||
<a name="editmessagereplymarkup"></a> | ||
#### `Telegraf.editMessageReplyMarkup(chatId, messageId, markup, extra)` => `Promise` | ||
### `Telegraf.editMessageReplyMarkup(chatId, messageId, markup, extra)` => `Promise` | ||
@@ -691,6 +718,4 @@ Use this method to edit only the reply markup of messages sent by the bot or via the bot. | ||
[Related Telegram api docs](https://core.telegram.org/bots/api#editmessagereplymarkup) | ||
### File | ||
#### File | ||
This object represents the contents of a file to be uploaded. | ||
@@ -768,33 +793,2 @@ | ||
### Shortcuts | ||
Telegraf context have many handy shortcuts. | ||
Note: shortcuts are not available for `inline_query` and `chosen_inline_result` events. | ||
```js | ||
var telegraf = new Telegraf(process.env.BOT_TOKEN) | ||
telegraf.on('text', function * (){ | ||
// Simple usage | ||
telegraf.sendMessage(this.message.chat.id, `Hello ${this.state.role}`) | ||
// Using shortcut | ||
this.reply(`Hello ${this.state.role}`) | ||
// If you want to mark message as reply to source message | ||
this.reply(`Hello ${this.state.role}`, { reply_to_message_id: this.message.id }) | ||
}) | ||
``` | ||
* `reply()` -> `telegraf.sendMessage()` | ||
* `replyWithPhoto()` -> `telegraf.sendPhoto()` | ||
* `replyWithAudio()` -> `telegraf.sendAudio()` | ||
* `replyWithDocument()` -> `telegraf.sendDocument()` | ||
* `replyWithSticker()` -> `telegraf.sendSticker()` | ||
* `replyWithVideo()` -> `telegraf.sendVideo()` | ||
* `replyWithVoice()` -> `telegraf.sendVoice()` | ||
* `replyWithChatAction()` -> `telegraf.sendChatAction()` | ||
* `replyWithLocation()` -> `telegraf.sendLocation()` | ||
## License | ||
@@ -801,0 +795,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38091
497
775