向函数发送请求后发生一些错误 url

Some error occures after send request to the function url

我设置了一个 firebase 函数来向我的机器人发送消息。但是当 firebase 收到请求时,我可以看到发生了一些错误。

FetchError: request to https://api.telegram.org/bot<BOT-TOKEN>/getMe failed, reason: getaddrinfo EAI_AGAIN api.telegram.org:443
    at ClientRequest.<anonymous> (/srv/node_modules/node-fetch/lib/index.js:1453:11)
    at emitOne (events.js:116:13)
    at ClientRequest.emit (events.js:211:7)
    at TLSSocket.socketErrorListener (_http_client.js:401:9)
    at emitOne (events.js:116:13)
    at TLSSocket.emit (events.js:211:7)
    at emitErrorNT (internal/streams/destroy.js:66:8)
    at _combinedTickCallback (internal/process/next_tick.js:139:11)
    at process._tickDomainCallback (internal/process/next_tick.js:219:9)

Error: Can't set headers after they are sent.
    at validateHeader (_http_outgoing.js:491:11)
    at ServerResponse.setHeader (_http_outgoing.js:498:3)
    at ServerResponse.header (/worker/node_modules/express/lib/response.js:767:10)
    at ServerResponse.contentType (/worker/node_modules/express/lib/response.js:595:15)
    at ServerResponse.sendStatus (/worker/node_modules/express/lib/response.js:357:8)
    at /srv/index.js:30:13
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:229:7)

但是我想第二个不会影响结果。

因此,我的机器人没有收到任何消息。

我使用的下一个脚本:

const functions = require('firebase-functions');
const telegraf = require('telegraf');
const axios = require('axios');

const bot = new telegraf('<BOT-TOKEN>')
bot.start((ctx) => ctx.reply('Welcome!'))
bot.help((ctx) => ctx.reply('Send me a sticker'))
bot.on('sticker', (ctx) => ctx.reply(''))
bot.hears('hi', (ctx) => ctx.reply('Hey there'))
bot.launch()

exports.helloWorld = functions.https.onRequest((request, res) => {
    const token = '<BOT-TOKEN>';
    const url = `https://api.telegram.org/bot${token}/sendMessage`;
    axios.post(url, {
        chat_id: '154866113',
        text: "just do it!"
      })
      .then(function (response) {
        console.log(response);
        res.send({ status: "O"});
        return 'ok';
      })
      .catch(function (error) {
        console.log(error);
        res.sendStatus(500);
      });
    res.send("Hello from Firebase!");
});

我猜 EAI_AGAIN 错误的答案是由于 URI 格式错误造成的。您确定 https://api.telegram.org/bot<BOT-TOKEN>/getMe 是正确的 URI 并且可以从 运行 来自该脚本的任何地方访问吗?

第二个错误是由于您多次调用res.send()方法造成的。 Express res 对象包裹了 Node 的 ServerResponse 对象,一旦通过 writeend 关闭连接,就无法再次发送。尝试删除代码的 res.send("Hello from Firebase!"); 部分并返回。

getaddrinfo EAI_AGAIN 错误意味着您需要切换到 "Flame" 或 "Blaze" 定价计划。

事实上,免费 "Spark" 计划 "allows outbound network requests only to Google-owned services"。请参阅 https://firebase.google.com/pricing/(将鼠标悬停在 "Cloud Functions" 标题后面的问号上)

电报 API 不是 Google-owned 服务,因此您需要切换到 "Flame" 或 "Blaze" 计划。