Huge News!Announcing our $40M Series B led by Abstract Ventures.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.9.5 to 0.9.6

7

lib/constants.js
module.exports = {
defaultExtensions: {
photo: 'jpg',
audio: 'mp3',
voice: 'ogg',
sticker: 'webp',
video: 'mp4'
},
updateTypes: [

@@ -3,0 +10,0 @@ 'message',

176

lib/telegraf.js
var debug = require('debug')('telegraf:core')
var crypto = require('crypto')
var fetch = require('node-fetch')
var FormData = require('form-data')
var fs = require('fs')
var http = require('http')
var https = require('https')
var path = require('path')
var isStream = require('is-stream')
var Multipart = require('multipart-stream')
var ware = require('co-ware')

@@ -11,2 +12,6 @@ var memorySession = require('./memory-session')

// TODO: inline
var http = require('http')
var https = require('https')
/**

@@ -160,3 +165,3 @@ * Represents a Telegraf app.

.catch((err) => {
debug(err)
debug('webhook error', err)
res.writeHead(500)

@@ -188,3 +193,3 @@ res.end()

this.webhookServer.listen(port, host, () => {
debug('WebHook listening on port: %s', options.port)
debug('WebHook listening on port: %s', port)
})

@@ -294,3 +299,4 @@ return this

telegraf.getFileLink = function (fileId) {
return this.getFile(fileId).then((response) => `${this.options.apiRoot}/file/bot${this.options.token}/${response.file_path}`)
return this.getFile(fileId)
.then((response) => `${this.options.apiRoot}/file/bot${this.options.token}/${response.file_path}`)
}

@@ -557,3 +563,5 @@

/**
* Use this method to get up to date information about the chat (current name of the user for one-on-one conversations, current username of a user, group or channel, etc.).
* Use this method to get up to date information about the chat
* (current name of the user for one-on-one conversations,
* current username of a user, group or channel, etc.).
*

@@ -569,3 +577,7 @@ * @param {(string|number)} chatId

/**
* Use this method to get a list of administrators in a chat. On success, returns Promise with Array of ChatMember objects that contains information about all chat administrators except other bots. If the chat is a group or a supergroup and no administrators were appointed, only the creator will be returned.
* Use this method to get a list of administrators in a chat.
* On success, returns Promise with Array of ChatMember objects that contains
* information about all chat administrators except other bots.
* If the chat is a group or a supergroup and no administrators were appointed,
* only the creator will be returned.
*

@@ -764,10 +776,14 @@ * @param {(string|number)} chatId

*/
telegraf.sendRequest = function (method, options, response) {
telegraf.sendRequest = function (method, options, res) {
options = Object.assign({}, options)
var isFileRequest = Object.keys(options).filter((x) => options[x] && options[x].source).length > 0
if (response && !response.finished && !isFileRequest && constants.webHookAnswerBlacklist.indexOf(method) === -1) {
var isMultipart = Object.keys(options)
.filter((x) => {
return options[x] && (options[x].source || options[x].url)
}).length > 0
if (res && !res.finished && !isMultipart && constants.webHookAnswerBlacklist.indexOf(method) === -1) {
debug('▷ webhook', method)
options.method = method
response.setHeader('Content-Type', 'application/json')
response.end(JSON.stringify(options))
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(options))
return Promise.resolve({

@@ -782,14 +798,13 @@ result: true,

debug('▶︎ http', method)
var payload = {
method: 'POST',
headers: isFileRequest ? {} : {'Content-Type': 'application/json'},
body: isFileRequest ? this.buildFormData(options) : JSON.stringify(options)
}
return fetch(`${this.options.apiRoot}/bot${this.options.token}/${method}`, payload)
var buildPayload = isMultipart
? this.buildFormDataPayload(options)
: this.buildJSONPayload(options)
return buildPayload
.then((payload) => {
return fetch(`${this.options.apiRoot}/bot${this.options.token}/${method}`, payload)
})
.then((res) => res.json())
.then((data) => {
if (data.ok) {
return data.result
return Promise.resolve(data.result)
} else {

@@ -802,40 +817,96 @@ throw new Error(`${data.error_code}: ${data.description}`)

/**
* Build json payload from options
*
* @param {Object} options - object
* @return {Promise} payload
* @api private
*/
telegraf.buildJSONPayload = function (options) {
return Promise.resolve({
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(options)
})
}
/**
* Build Form-Data from options
*
* @param {Object} options - Payload object
* @return {Object} Form-Data
* @param {Object} options - object
* @return {Promise} payload
* @api private
*/
telegraf.buildFormData = function (options) {
telegraf.buildFormDataPayload = function (options) {
if (options.reply_markup && typeof options.reply_markup !== 'string') {
options.reply_markup = JSON.stringify(options.reply_markup)
}
var form = new FormData()
Object.keys(options).forEach((key) => {
var value = options[key]
if (typeof value === 'undefined' || value == null) {
return
}
if (typeof value === 'object') {
var data = value.source
if (data) {
if (Buffer.isBuffer(data)) {
form.append(key, data, {
knownLength: data.length
var boundary = crypto.randomBytes(30).toString('hex')
var formData = new Multipart(boundary)
var tasks = Object.keys(options)
.filter((key) => {
return options[key]
})
.map((key) => {
var value = options[key]
var valueType = typeof value
if (valueType === 'string' || valueType === 'boolean' || valueType === 'number') {
formData.addPart({
headers: {
'content-disposition': `form-data; name="${key}"`
},
body: '' + value
})
return Promise.resolve()
}
var extension = value.extension || constants.defaultExtensions[key] || 'dat'
var fileName = `${key}.${extension}`
if (value.source) {
if (isStream(value.source) || Buffer.isBuffer(value.source)) {
formData.addPart({
headers: {
'content-disposition': `form-data; name="${key}";filename="${fileName}"`
},
body: value.source
})
} else if (fs.existsSync(data)) {
form.append(key, fs.createReadStream(data), {
knownLength: fs.statSync(data).size
return Promise.resolve()
} else if (fs.existsSync(value.source)) {
fileName = `${key}.${path.extname(value.source)}`
formData.addPart({
headers: {
'content-disposition': `form-data; name="${key}";filename="${fileName}"`
},
body: fs.createReadStream(value.source)
})
} else {
form.append(key, data)
return Promise.resolve()
}
}
} else if (typeof value === 'boolean') {
form.append(key, value.toString())
} else {
form.append(key, value)
}
})
return form
if (value.url) {
return fetch(value.url)
.then((res) => {
formData.addPart({
headers: {
'content-disposition': `form-data; name="${key}";filename="${fileName}"`
},
body: res.body
})
})
}
return Promise.reject('Invalid file')
})
return Promise.all(tasks)
.then(() => {
var payload = {
method: 'POST',
headers: {
'Content-Type': `multipart/form-data; boundary=${boundary}`
},
body: formData
}
return Promise.resolve(payload)
})
}

@@ -908,4 +979,4 @@

debug('⚡ update', update.type, update.subType || '-')
var chat = {}
var sender = {}
var chat = {id: ''}
var sender = {id: ''}
if (update.payload.from) {

@@ -941,3 +1012,4 @@ sender = update.payload.from

var payload = update.payload
var chatId = (payload.chat && payload.chat.id) || (payload.message && payload.message.chat && payload.message.chat.id)
var chatId = (payload.chat && payload.chat.id) ||
(payload.message && payload.message.chat && payload.message.chat.id)

@@ -944,0 +1016,0 @@ if (chatId) {

{
"name": "telegraf",
"version": "0.9.5",
"version": "0.9.6",
"description": "📢 Modern Telegram bot framework",

@@ -41,3 +41,4 @@ "main": "lib/telegraf.js",

"debug": "^2.2.0",
"form-data": "^1.0.0-rc4",
"is-stream": "^1.1.0",
"multipart-stream": "^2.0.1",
"node-fetch": "^1.5.1"

@@ -47,3 +48,6 @@ },

"eslint": "^2.10.2",
"eslint-config-standard": "^5.3.1",
"eslint-plugin-mocha": "^2.2.0",
"eslint-plugin-promise": "^1.1.0",
"eslint-plugin-standard": "^1.3.2",
"mocha": "^2.4.5",

@@ -50,0 +54,0 @@ "should": "^8.3.1",

@@ -965,20 +965,35 @@ [![npm](https://img.shields.io/npm/l/telegraf.svg?style=flat-square)](https://www.npmjs.com/package/telegraf)

- `Existing file_id`
- `File path`
- `Url`
- `Buffer`
- `ReadStream`
- `Existing file_id`
Example:
```js
// resend existing file by file_id
telegraf.sendSticker('chatId', '123123jkbhj6b')
// send file
telegraf.sendVideo('chatId', {source: '/path/to/video.mp4'}})
telegraf.sendVideo('chatId', {
source: '/path/to/video.mp4'
})
// send stream
telegraf.sendVideo('chatId', {
source: fs.createReadStream('/path/to/video.mp4'),
extension: 'mp4'
})
// send buffer
telegraf.sendVoice('chatId', {source: new Buffer(...)})
telegraf.sendVoice('chatId', {
source: new Buffer()
})
// send stream
telegraf.sendAudio('chatId', {source: fs.createReadStream('/path/to/video.mp4')})
// send url
telegraf.sendAudio('chatId', {
url: 'http://lorempixel.com/image_output/cats-q-c-640-480-7.jpg'
})
// resend existing file
telegraf.sendSticker('chatId', '123123jkbhj6b')
```

@@ -985,0 +1000,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