New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

telegraf

Package Overview
Dependencies
Maintainers
1
Versions
241
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

telegraf - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

148

lib/app.js

@@ -8,6 +8,42 @@ var debug = require('debug')('telegraf:core')

var ware = require('co-ware')
var https = require('http')
var https = require('https')
var matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g
var messageSubTypes = [
'text',
'audio',
'document',
'photo',
'sticker',
'video',
'voice',
'contact',
'location',
'venue',
'new_chat_participant',
'left_chat_participant',
'new_chat_title',
'new_chat_photo',
'delete_chat_photo',
'group_chat_created',
'supergroup_chat_created',
'channel_chat_created',
'migrate_to_chat_id',
'migrate_from_chat_id',
'pinned_message'
]
var shortcuts = [
{ name: 'reply', target: 'sendMessage' },
{ name: 'replyWithPhoto', target: 'sendPhoto' },
{ name: 'replyWithAudio', target: 'sendAudio' },
{ name: 'replyWithDocument', target: 'sendDocument' },
{ name: 'replyWithSticker', target: 'sendSticker' },
{ name: 'replyWithVideo', target: 'sendVideo' },
{ name: 'replyWithVoice', target: 'sendVoice' },
{ name: 'replyWithChatAction', target: 'sendChatAction' },
{ name: 'replyWithLocation', target: 'sendLocation' }
]
var telegraf = Telegraf.prototype

@@ -29,5 +65,6 @@ module.exports = Telegraf

telegraf.startPolling = function (timeout) {
telegraf.startPolling = function (timeout, limit) {
this.started = true
this.options.timeout = timeout
this.options.timeout = timeout || 0
this.options.limit = limit || 100
this.updateLoop()

@@ -37,12 +74,10 @@ return this

telegraf.startWebHook = function (port, path, key, cert) {
telegraf.startWebHook = function (token, tlsOptions, port, host) {
this.started = true
var options = { }
if (key && cert) {
options.key = fs.readFileSync(key)
options.cert = fs.readFileSync(cert)
if (!tlsOptions) {
throw new Error('tlsOptions required')
}
var self = this
this.webhookServer = https.createServer(options, function (req, res) {
if (req.method !== 'POST' || req.url !== `/${path}`) {
this.webhookServer = https.createServer(tlsOptions, function (req, res) {
if (req.method !== 'POST' || req.url !== `/${token}`) {
res.statusCode = 403

@@ -65,3 +100,3 @@ res.end()

})
}).listen(port)
}).listen(port, host)
return this

@@ -90,6 +125,6 @@ }

telegraf.on = function (messageType) {
telegraf.on = function (eventType) {
var fns = [].slice.call(arguments, 1)
this.bus.on(messageType, function (msg) {
var context = this.createContext(msg)
this.bus.on(eventType, function (payload) {
var context = this.createContext(eventType, payload)
context.use(compose(this.middleware))

@@ -109,6 +144,6 @@ context.use(compose(fns))

this.on('text', function * () {
if (!this.msg.text) {
if (!this.message.text) {
return
}
var result = regex.exec(this.msg.text)
var result = regex.exec(this.message.text)
if (result) {

@@ -345,2 +380,3 @@ this.__defineGetter__('match', function () {

}
debug('fetch', `https://api.telegram.org/bot${this.token}/${path}`)
return fetch(`https://api.telegram.org/bot${this.token}/${path}`, payload)

@@ -360,28 +396,5 @@ .then(function (res) {

telegraf.handleUpdate = function (update) {
var messageTypes = [
'text',
'audio',
'document',
'photo',
'sticker',
'video',
'voice',
'contact',
'location',
'venue',
'new_chat_participant',
'left_chat_participant',
'new_chat_title',
'new_chat_photo',
'delete_chat_photo',
'group_chat_created',
'supergroup_chat_created',
'channel_chat_created',
'migrate_to_chat_id',
'migrate_from_chat_id',
'pinned_message'
]
var bus = this.bus
var offset = update.update_id + 1
debug(update)
Object.keys(update).forEach(function (key) {

@@ -391,3 +404,3 @@ bus.emit(key, update[key])

if (update.message) {
messageTypes.forEach(function (messageType) {
messageSubTypes.forEach(function (messageType) {
if (update.message[messageType]) {

@@ -408,3 +421,3 @@ bus.emit(messageType, update.message)

var errorHandler = this.onerror.bind(this)
this.getUpdates(this.options.timeout, 100, this.offset)
this.getUpdates(this.options.timeout, this.options.limit, this.offset)
.then(function (updates) {

@@ -421,28 +434,28 @@ updates.forEach(handleUpdate)

telegraf.createContext = function (msg) {
telegraf.createContext = function (payloadType, payload) {
var state = {}
var context = Object.assign({}, this.context)
var shortcuts = [
'sendPhoto',
'sendAudio',
'sendDocument',
'sendSticker',
'sendVideo',
'sendVoice',
'sendChatAction',
'sendLocation'
]
shortcuts.forEach(function (methodName) {
context[methodName.replace('send', 'replyWith')] = function (obj, options, extra) {
return this[methodName](msg.chat.id, obj, options, extra)
}.bind(this)
}.bind(this))
context.reply = function (message, options) {
return this.sendMessage(msg.chat.id, message, options)
}.bind(this)
var state = {}
context.__defineGetter__('msg', function () {
return msg
})
if (payloadType === 'message' || messageSubTypes.indexOf(payloadType) !== -1) {
shortcuts.forEach(function (shortcut) {
context[shortcut.name] = this[shortcut.target].bind(this, payload.chat.id)
}.bind(this))
context.__defineGetter__('message', function () {
return payload
})
} else if (payloadType === 'callback_query') {
shortcuts.forEach(function (shortcut) {
context[shortcut.name] = this[shortcut.target].bind(this, payload.message.chat.id)
}.bind(this))
context.__defineGetter__('callbackQuery', function () {
return payload
})
} else if (payloadType === 'inline_query') {
context.__defineGetter__('inlineQuery', function () {
return payload
})
} else if (payloadType === 'chosen_inline_result') {
context.__defineGetter__('chosenInlineResult', function () {
return payload
})
}
context.__defineGetter__('state', function () {

@@ -454,3 +467,2 @@ return state

})
var ctx = ware()

@@ -457,0 +469,0 @@ ctx.context = context

{
"name": "telegraf",
"version": "0.1.1",
"version": "0.2.0",
"description": "Telegram bot framework",

@@ -5,0 +5,0 @@ "main": "lib/app.js",

@@ -106,3 +106,6 @@ # Telegraf

app.use(function * (){
this.msg; // Received message
this.message; // Received message
this.inlineQuery; // Received inline query
this.chosenInlineResult; // Received inline query result
this.callbackQuery; // Received callback query
});

@@ -121,4 +124,4 @@ ```

app.on('text', function * (){
var scores = this.db.getScores(this.msg.from.username)
this.reply(`${this.msg.from.username}: ${score}`)
var scores = this.db.getScores(this.message.from.username)
this.reply(`${this.message.from.username}: ${score}`)
})

@@ -135,3 +138,3 @@ ```

app.use(function * (next) {
this.state.role = getUserRole(this.msg)
this.state.role = getUserRole(this.message)
yield next

@@ -159,4 +162,4 @@ })

* [`new Telegraf(token)`](#new)
* [`.startPolling(timeout)`](#startPolling)
* [`.startWebHook(port, path, key, cert)`](#startWebHook)
* [`.startPolling(timeout, limit)`](#startPolling)
* [`.startWebHook(token, tlsOptions, port, [host])`](#startWebHook)
* [`.stop()`](#stop)

@@ -202,3 +205,3 @@ * [`.use(function)`](#use)

<a name="startPolling"></a>
#### `Telegraf.startPolling(timeout)`
#### `Telegraf.startPolling(timeout, limit)`

@@ -210,2 +213,3 @@ Start poll updates.

| timeout | `Int` | 0 | Poll timeout |
| limit | `Int` | 100 | Limits the number of updates to be retrieved |

@@ -215,12 +219,12 @@ * * *

<a name="startWebHook"></a>
#### `Telegraf.startWebHook(port, path, key, cert)`
#### `Telegraf.startWebHook(token, tlsOptions, port, [host])`
Start WebHook.
Start listening @ `https://host:port/token` for Telegram calls.
| Param | Type | Description |
| --- | --- | --- |
| token | `String` | Token |
| tlsOptions | `Object` | [tls server options](https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener) |
| port | `Int` | Port number |
| path | `String` | Path |
| key | `String` | Key file path|
| cert | `String` | Certificate file path |
| host | `String` | Hostname |

@@ -646,4 +650,6 @@ * * *

Telegraf context have handy shortcuts.
Telegraf context have many handy shortcuts.
Note: shortcuts are not available for `inline_query` and `chosen_inline_result` events.
```js

@@ -654,3 +660,3 @@ var app = Telegraf('BOT TOKEN')

// Simple usage
app.sendMessage(this.msg.chat.id, `Hello ${this.state.role}`)
app.sendMessage(this.message.chat.id, `Hello ${this.state.role}`)

@@ -661,3 +667,3 @@ // Using shortcut

// If you want to mark message as reply to source message
this.reply(`Hello ${this.state.role}`, {reply_to_message_id: this.msg.id })
this.reply(`Hello ${this.state.role}`, { reply_to_message_id: this.message.id })
})

@@ -664,0 +670,0 @@ ```

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc