如何使用AWS Lambda使用Telegram Bots发送文件?

时间:2018-04-29 16:42:29

标签: javascript node.js amazon-web-services aws-lambda telegram

我正在尝试使用电报机器人发送一个mp3文件。

当我使用终端中的节点在本地运行它时,这段代码完全正常:

'use strict'
const Telegraf = require('telegraf')
const bot = new Telegraf('Token')

bot.command('audio', (ctx) => {
        ctx.replyWithAudio({source: './media/song.mp3'})
    })

const { PORT = 3000 } = process.env
bot.startWebhook('/', null, PORT)

但是,当我将其部署为AWS Lambda函数时,我收到以下错误:

  Apr 29th 12:24:12pm ERRO staging 4 events.js:183
  Apr 29th 12:24:12pm ERRO staging 4       throw er; // Unhandled 'error' event
  Apr 29th 12:24:12pm ERRO staging 4       ^
  Apr 29th 12:24:12pm ERRO staging 4
  Apr 29th 12:24:12pm ERRO staging 4 Error: write after end
  Apr 29th 12:24:12pm ERRO staging 4     at write_ (_http_outgoing.js:622:15)
  Apr 29th 12:24:12pm ERRO staging 4     at ServerResponse.write (_http_outgoing.js:617:10)
  Apr 29th 12:24:12pm ERRO staging 4     at MultipartStream.ondata (_stream_readable.js:639:20)
  Apr 29th 12:24:12pm ERRO staging 4     at emitOne (events.js:116:13)
  Apr 29th 12:24:12pm ERRO staging 4     at MultipartStream.emit (events.js:211:7)
  Apr 29th 12:24:12pm ERRO staging 4     at MultipartStream.Readable.read (_stream_readable.js:475:10)
  Apr 29th 12:24:12pm ERRO staging 4     at flow (_stream_readable.js:846:34)
  Apr 29th 12:24:12pm ERRO staging 4     at resume_ (_stream_readable.js:828:3)
  Apr 29th 12:24:12pm ERRO staging 4     at _combinedTickCallback (internal/process/next_tick.js:138:11)
  Apr 29th 12:24:12pm ERRO staging 4     at process._tickCallback (internal/process/next_tick.js:180:9)

我很确定这是因为我在部署lambda函数时不理解文件的存储方式。如何保存目录以便我可以检索它们?这甚至可能吗?

我的zip包包含以下目录:

Zip file

对于这个机器人,我使用apex up来部署和telegraf来编写代码。

1 个答案:

答案 0 :(得分:0)

如果你的MP3在你上传的zip包中,那么当lambda运行时你可以在包中找到它和其他任何东西。

在lambda中,您可以获得包含带有process.env.LAMBDA_TASK_ROOT的解压缩包的目录。您的文件将与此相关。

bot.command('audio', (ctx) => {
    ctx.replyWithAudio({
        source: process.env.LAMBDA_TASK_ROOT + '/media/song.mp3'
    })
})