简体   繁体   English

Twilio 不在 Firebase 云功能内发送短信

[英]Twilio not sending SMS inside firebase cloud function

i am trying to send sms using twilio api for node.js through a firebase cloud function but the sms is not sent.我正在尝试使用 twilio api 通过 firebase 云功能为 node.js 发送短信,但未发送短信。 however if the same code i run as an independent java script code, then it works fine.can someone please help why this is not happening inside the firebase cloud function.但是,如果我将相同的代码作为独立的 Java 脚本代码运行,那么它可以正常工作。有人可以帮助解释为什么在 firebase 云函数中没有发生这种情况。 the code is attached below:代码附在下面:

 const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(); exports.sendNotfication = functions.database.ref('/kakuh/{pushId}/firstName') .onCreate((snapshot, context) => { const original = snapshot.val(); const accountSid = 'ACb6b4820df073e63312382f95b0314d07'; const authTcoken = 'c60923ca097368662b39dfab470f2fd1'; const client = require('twilio')(accountSid, authToken); client.messages .create({ from: '+16304263296', body: original, to: '+918169813384' }); console.log('Uppercasing', context.params.pushId, original); const uppercase = original.toUpperCase(); return snapshot.ref.parent.child('firstName').set(uppercase); });

You'll need to wait for Twilio to respond, then return to Firebase.您需要等待 Twilio 响应, then return Firebase。

Try this:尝试这个:


const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.sendNotfication = functions.database.ref('/kakuh/{pushId}/firstName')
    .onCreate((snapshot, context) => {

        const original = snapshot.val();
        const accountSid = 'ACb6b4820df073e63312382f95b0314d07';
        const authToken = 'c60923ca097368662b39dfab470f2fd1';
        const client = require('twilio')(accountSid, authToken);

        client.messages
            .create({
                from: '+16304263296',
                body: original,
                to: '+918169813384'
            })
            .then((message) => {
                console.log(message.sid);
                console.log('Uppercasing', context.params.pushId, original);
                const uppercase = original.toUpperCase();
                return snapshot.ref.parent.child('firstName').set(uppercase);
            })
            .catch((err) => {
                throw (err);
            });

    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM