【问题标题】:Problem between firebase function and Twilio & Nexmofirebase 功能与 Twilio & Nexmo 之间的问题
【发布时间】:2021-07-19 16:43:46
【问题描述】:

我正在尝试将 javascript 函数部署到 Firebase,其中包含通过 Twilio 发送 SMS 的代码。

在独立的单独文件中测试 Twilio js 代码时运行正常。 将包含 Twilio 代码的完整代码上传到 firebase 函数时发生错误。 我试过 Nexmo 也遇到了问题。 似乎火力基地阻止了 Twilio 和 Nexmo!

有什么建议吗?

编辑: 这是我的完整代码

const functions = require("firebase-functions");

const admin = require('firebase-admin');

admin.initializeApp();

const db = admin.firestore();
const accountSid = 'AC18bda2c8129eedc0c13fb4123761eb44'; 
const authToken = 'xyzxyzyxz'; 
const client = require('twilio')(accountSid, authToken); 
 
exports.realtimefunction=functions.database.ref('/{X}/{Y}/{Z}').onCreate((snapshot,context)=>{


  client.messages 
      .create({ 
         body: 'Hi',  
         messagingServiceSid: 'MGf7sdf39d9f979ssdfeb9f16',      
         to: '+201011111111' 
       }) 
      .then(message => console.log(message.sid)) 
      .done();

  return null;
});

和错误消息:错误:函数未正确部署。

【问题讨论】:

  • 您能分享一下您在运行 Twilio 代码时收到的代码和错误消息吗?
  • 请在此处添加更多详细信息 - 您尝试过的代码,来自 firebase 的错误。任何可能帮助我们找到解决方案的东西。否则,恐怕这个问题可能会被关闭且无法回答:(
  • 我已经用代码和错误信息编辑了帖子
  • 添加Twilio decaration时出现错误。
  • 如果在那个例子中那是你真正的身份验证令牌,那么我推荐你change your auth token now。

标签: javascript firebase google-cloud-functions twilio nexmo


【解决方案1】:

这是一个应该有效的示例。让我知道这是否适合您,或者您是否仍然有错误。这个例子应该允许你给一个号码发短信,它会用你发送的短信回复你。

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const Vonage = require('@vonage/server-sdk');

// Initialize Firebase app for database access
admin.initializeApp();

// get Firebase environment variables for Vonage
const {
  api_key,
  api_secret
} = functions.config().vonage;

// Initialize Vonage with application credentials
const vonage = new Vonage({
  apiKey: api_key,
  apiSecret: api_secret
});

// This function will serve as the webhook for incoming SMS messages,
// and will log the message into the Firebase Realtime Database
exports.inboundSMS = functions.https.onRequest(async (req, res) => {
  await admin.database().ref('/msgq').push(req.body);
  res.send(200);
});

// This function listens for updates to the Firebase Realtime Database
// and sends a message back to the original sender
exports.sendSMS = functions.database.ref('/msgq/{pushId}')
  .onCreate((message) => {
    const { msisdn, text, to } = message.val();
    // the incoming object - 'msisdn' is the your phone number, and 'to' is the Vonage number
    // vonage.message.sendSms(to, msisdn, text);
    return new Promise((respond, reject) => {
        vonage.message.sendSms(to, msisdn, `You sent the following text: ${text}`, 
        (err, res) => {
            if (err) {
                reject(err);
            } else {
                if (res.messages[0]['status'] === "0") {
                    respond("Message sent successfully.");
                } else {
                    reject(`Message failed with error: ${res.messages[0]['error-text']}`);
                }
            }
        })
    })
});

【讨论】:

    【解决方案2】:

    您是否使用 Firebase 的免费 spark 计划? Firebase 仅允许对付费“Blaze”计划进行外部 API 访问,请参阅此问题以获取详细答案。 HTTP request to an external API in Firebase Cloud Functions (Spark Tier) refused

    【讨论】:

    • 我在计划中 Blaze Pay as you go
    猜你喜欢
    • 2018-10-23
    • 1970-01-01
    • 2019-05-16
    • 2019-09-06
    • 1970-01-01
    • 2018-08-09
    • 2020-02-27
    • 2020-06-02
    • 2021-08-15
    相关资源
    最近更新 更多