使用Firebase Cloud Function的POST请求发送SMS

问题描述 投票:0回答:1

我有一个来自textlocal的SMS API,当在Firestore集合中创建新文档时,我想使用它通过Cloud功能发送SMS。集合名称:预订短信。它将创建一个带有“数字”,“名称”和“服务”字段的文档现在,SMY API只需很少的参数即可发送SMS。API网址:“ https://api.textlocal.in/send/”1. apikey2.发件人3.消息。该消息将被构造为'嗨,'姓名',您对'service'的预订已确认。

名称,服务和编号将来自Firestone文档和apikey,发件人将使用cloudfunction代码进行编码。现在,我想创建cloudfunction触发器,该触发器将在创建文档时发送短信。下面是我尝试的不完整代码,请帮助我完成它。

//1. Deploys as dbUsersOnUpdate
const functions = require('firebase-functions')
const admin = require('firebase-admin')


// 2. Admin SDK can only be initialized once
try {admin.initializeApp(functions.config().firebase)} catch(e) {
        console.log('initializeApp failure')
}

// 3. variable used:

const apikey = asasasasasasasasas
const sender = DDDDDD


// 4. Watch for new booking
exports = module.exports = functions.database.ref('/BookingSMS/{uid}').onCreate((event) => {
        const snapshot = event.data
        const user = snapshot.val()

        return bookSMS(user);
})

function bookSMS(user) {
        // 5. Send booking SMS to users
        const smsoption = {
                apikey: 'asasasasasasasasas',
        sender: 'DDDDDD'
                to: '${user.phone}',
                message: 'Welcome!',

        }
        // 6. Process the sending of this SMS

}

我也想在数字前加上+91。并使用名称和服务构造消息。

firebase google-cloud-firestore google-cloud-functions
1个回答
0
投票

根据documentation,不直接支持node.js。您应该可以使用他们的get请求。 (希望您熟悉异步/等待)试试:

async function bookSMS(user) {
        // 5. Send booking SMS to users
        const smsoption = {
                apikey: 'asasasasasasasasas',
        sender: 'DDDDDD'
                to: '${user.phone}',
                message: 'Welcome!',

        }
        // 6. Process the sending of this SMS
        await fetch(`https://api.textlocal.in/send/?apikey=${apiKey}&numbers=${user.phone}&message=Welcome!&sender=DDDDDD`)
}

您必须启用结算功能才能访问Firebase中的外部API

© www.soinside.com 2019 - 2024. All rights reserved.