twilio 使用电话号码黑名单拒绝来电 [英] twilio Reject Incoming Calls with a Phone Number Blacklist
问题描述
首先.我很抱歉英语是我的第二语言,所以我为任何错误道歉.第二.我是 Twilio
的新手.
First of all. I'm sorry English is my second language so I apologize for any mistakes.
Second. I'm new with Twilio
.
在此先感谢您的帮助.
我有多个带有 Twilio
的电话号码,我正在尝试对我的所有电话号码实施号码黑名单.目前,我对所有 Twilio
数字单独使用下面的函数.所以理想情况下,我想创建一个文件,其中包含我想列入黑名单的所有电话号码,我可以在函数中读取此文件,而无需在单个函数中写入列入黑名单的号码.
I have multiple phone numbers with Twilio
and I am trying to implement a number blacklist to all my phone numbers. Currently, I use the function bellow individually with all my Twilio
numbers. So ideally I would like to create a file with all the phone numbers that I want to be blacklist and I could read this file in the function and don't need to write the blacklisted numbers in individual functions.
exports.handler = function(context, event, callback) {
// List all blocked phone numbers in quotes and E.164 formatting, separated by a comma
let blacklist = event.blacklist || [ "blacklist numbers","XXXXXXXXXX","XXXXXXXXX" ];
let twiml = new Twilio.twiml.VoiceResponse();
let blocked = true;
if (blacklist.length > 0) {
if (blacklist.indexOf(event.From) === -1) {
blocked = false;
}
}
if (blocked) {
twiml.reject();
}
else {
// if the caller's number is not blocked, redirect to your existing webhook
twiml.redirect("XXXXXX");
}
callback(null, twiml);
};
非常感谢.
推荐答案
你可以有类似下面的代码.然后,您可以将 blacklist.json 作为私有资产上传到您的 Twilio Assets.读取私有资产的代码显示在 Twilio 文档下,读取资产的内容一>.
You could have something like the code below. You would then upload the blacklist.json to your Twilio Assets as a private asset. The code to read a private asset is shown under the Twilio documentation, Read the Contents of an Asset.
blacklist.json 的格式只是一个 JSON 数组:["+14071234567", "+18021234567"]
The format of blacklist.json is just a JSON array: ["+14071234567", "+18021234567"]
const fs = require('fs');
exports.handler = function(context, event, callback) {
let fileName = 'blacklist.json';
let file = Runtime.getAssets()[fileName].path;
let text = fs.readFileSync(file);
let blacklist = JSON.parse(text);
console.log(blacklist);
let twiml = new Twilio.twiml.VoiceResponse();
let blocked = true;
if (blacklist.length > 0) {
if (blacklist.indexOf(event.From) === -1) {
blocked = false;
}
}
if (blocked) {
twiml.reject();
}
else {
// if the caller's number is not blocked, redirect to your existing webhook
twiml.redirect("XXXXXX");
}
callback(null, twiml);
};
这篇关于twilio 使用电话号码黑名单拒绝来电的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!