Firestore Cloud功能触发短信

时间:2019-05-21 20:33:29

标签: javascript google-cloud-firestore twilio google-cloud-functions

我具有以下云功能,这些功能将触发使用Twillio发送的短信。它可以按原样工作,但会导致文本消息发送两次。有没有一种方法可以修改我的功能来防止这种情况?请注意,我没有使用Firebase实时数据库。我正在使用Firebase Firestore数据库。此功能已与Ionic 4项目结合使用。

export const textPrayedForNotification = functions.firestore.document('/prayerRequests/{prayerRequestId}').onUpdate(snap => {
    const phone: string = snap.after.get('phoneNumber');
    const receiveNotifications: boolean = snap.after.get('receiveNotifications');

    if (receiveNotifications) {
        return client.messages.create({
            to: phone,
            from: twilioNumber,
            body: 'Someone has prayed for you.'
        }).then((data: any) => {
            console.log(data);
        }).catch((error: any) => {
            console.log(error);
        });
    }
});

更新: 将功能更改为此,现在似乎可以使用了。

export const textPrayedForNotification = functions.firestore.document('/prayerRequests/{prayerRequestId}').onUpdate(snap => {
    const phone: string = snap.after.get('phoneNumber');
    const receiveNotifications: boolean = snap.after.get('receiveNotifications');
    const dateLastPrayedBefore = snap.before.get('dateLastPrayed');
    const dateLastPrayedAfter = snap.after.get('dateLastPrayed');

    if (receiveNotifications) {
        if (dateLastPrayedBefore !== dateLastPrayedAfter) {
            return client.messages.create({
                to: phone,
                from: twilioNumber,
                body: 'Someone has prayed for you.'
            }).then((data: any) => {
                console.log(data);
            }).catch((error: any) => {
                console.log(error);
            });
        }
    }
});

0 个答案:

没有答案
相关问题