【发布时间】:2021-09-10 19:35:43
【问题描述】:
Stack Overflow 的好人。
我正在尝试解决我遇到的涉及 twilio 函数、消息传递服务和数据库的问题。
我正在尝试做的是一次向数据库的所有成员发送消息。
我的代码一团糟,因为 Javascript 不是我的母语,而且我对 twilio 还很陌生。
我认为我遇到的问题是 javascript 的 async/await 功能。
到目前为止,这是我的代码:
// Boiler Plate Deta Code
const { Deta } = require("deta");
// Function to access database and get object of conta
async function FetchDB(db) {
let res = await db.fetch();
allItems = res.items;
// continue fetching until last is not seen
while (res.last){
res = await db.fetch({}, {last: res.last});
allItems = allItems.concat(res.items);
}
}
// Function to get total number of contacts.
async function ReturnNumberOfContacts(allItems) {
number_of_contacts = allItems.length;
}
// Function to send message to contact in database.
async function SendMessages(allItems, message) {
allItems.forEach(contact => {
let users_name = contact.name
client.messages
.create({
body: `Hey ${users_name}! ${message}`,
messagingServiceSid: messaging_service,
to: contact.key
})
});
}
// Function to submit response to broadcaster.
async function SuccessResponse(user_name, number_of_contacts) {
responseObject = {
"actions": [
{
"say": `${user_name}, your broadcast has successfully sent to ${number_of_contacts} contacts.`
},
{
"listen": true
}
]
}
}
// Main Function
exports.handler = async function(context, event, callback) {
// Placeholder for number of contacts
let number_of_contacts;
// Place holder for object from database of all contacts
let allItems;
// Placeholder for users message
let message;
// Placeholder for response to user
let responseObject;
//Twilio and Deta, Etc Const
const client = require('twilio')(context.ACCOUNT_SID, context.AUTH_TOKEN);
const deta = Deta(context.DETA_PROJECT_KEY);
const db = deta.Base("users2");
const messaging_service = context.MESSAGING_SERVICE;
// From Phone Number
const from = event.UserIdentifier;
// Parse memory
const memory = JSON.parse(event.Memory);
// Fetch all items from database and return total number of contacts.
// Update relavent variables
await FetchDB(db, allItems).then(ReturnNumberOfContacts(allItems));
// Figure out if message came from short circuit broadcast or normal
if (memory.triggered) {
message = memory.message;
} else {
message = memory.twilio.collected_data.broadcast_message.answers.message_input.answer;
}
// Check if verified and set name.
const current_user = await db.get(from);
// Get the current users name or set a default value
let user_name = current_user.name || "friend";
// Determine if user is an authorized broadcaster
if (from === context.BROADCAST_NUMBER) {
// Decide if the sending of a message should be cancelled.
if (message.toLowerCase() === "c" || message.toLowerCase() === "cancel") {
responseObject = {
"actions": [
{
"say": `${user_name}, you have canceled your request and no messages have been sent.`
},
{
"listen": false
}
]
}
// Return Callback and end task
callback(null, responseObject);
}
// Move forward with sending a message.
else {
// Send message to users in database and send success message to broadcaster.
await SendMessages(message, client, messaging_service)
.then(SuccessResponse(user_name, number_of_contacts))
return callback(null, responseObject);
}
// The user is not authorized so return this.
}
return callback(null, {
"actions": [
{
"say": "You are not authorized to broadcast."
},
{
"listen": false
}
]
})
};
因此,当触发 Fetch() 函数时,我希望数据库加载每个人的列表,并让 twilio 向他们发送保存在 message 变量中的所需消息。我有代码工作,以便我可以从数据库中读取并获取正确的值,并发送一条带有所需消息的文本消息,但我现在遇到的问题是将它们整合在一起。
如果有人能在这里指出正确的方向,谢谢。
再次,我是 javascript 的新手,更具体地说是异步编程。
【问题讨论】:
-
这是我现在遇到的错误 >>> 函数执行导致错误日志:UnhandledPromiseRejectionWarning:未处理的承诺拒绝:TypeError:无法读取 ReturnNumberOfContacts (/var) 处未定义的属性“长度” /task/handlers/ZN166fcaef71e8b34f546263b2198e8d04.js:18:33) 在 Object.exports.handler (/var/task/handlers/ZN166fcaef71e8b34f546263b2198e8d04.js:78:36) 在 Object.exports.runtime (/var/task/node_modules/ -handler/index.js:310:10) 在 Runtime.exports.handler (/var/task/runtime-handler.js:17:17) 在 Runtime.handleOnce (/var/run...
-
另外,我正在 twilio 函数中构建它。
标签: twilio