Cloud Code Functions
Integrations
通过云函数实现Twilio SMS发送:开发者指南
8 分
使用云函数和twilio api发送短信 介绍 本指南解释了如何使用twilio rest api发送短信。在完成此逐步教程后,您可以使用云代码函数向您的设备发送短信。 先决条件 要完成此教程,您需要: 在back4app创建的应用程序。 请遵循 创建新应用程序教程 以了解如何在back4app上创建应用程序。 与项目配置的back4app命令行工具。 请遵循 设置云代码教程 以了解如何为项目设置云代码。 在 twilio 创建的账户。 让我们开始吧! 以下是您在编写函数以向用户和电话号码发送短信时需要遵循的一些步骤。 要了解如何在twilio中创建或访问账户,请查看下面提供的链接: 创建一个新账户 登录到您的账户 1 激活您的电话号码 登录或创建新账户后,您将被重定向到您的项目。在左侧,您需要点击 #电话号码 #电话号码 。接下来,点击最后一个链接‘入门指南’,然后点击按钮‘获取您的第一个twilio电话号码’。与下面显示的相同 之后,您将收到您的第一个twilio账户电话号码。如果找不到您的电话号码,请转到#电话号码并管理号码。 2 获取账户sid和身份验证令牌 要找到您的 账户sid 账户sid 和 身份验证令牌 身份验证令牌 ,请登录到您的账户,转到仪表板并点击设置。关于您项目的所有重要信息将在该部分中提供;如下图所示 现在,您可以复制您的 sid 和身份验证令牌以用于云代码。 3 从 twilio 安装模块 在您的计算机上为命令行界面配置环境后,创建一个名为 package json 的文件,在此文件中,您需要安装 twilio 模块,如下所示: 1 { 2 "dependencies" { 3 "twilio" " " 4 } 5 } 4 实现云代码 parse server 3 x 1 parse cloud define("sendsms", async(request) => { 2 3 // requiring the values to send 4 let 5 getmessage = request params message, 6 getphoneto = '+target test phone number', 7 getphonefrom = "+your first phone number", 8 accountsid = 'accountsid', 9 authtoken = 'authtoken'; 10 11 //require the twilio module and create a rest client 12 let client = require('twilio')(accountsid, authtoken); 13 14 return await client messages 15 create({ 16 body getmessage, // any number twilio can deliver to 17 from getphonefrom, // a number you bought from twilio and can use for outbound communication 18 to getphoneto // body of the sms message 19 }); 20 }); parse server 2 x 1 parse cloud define("sendsms",function(request,response){ 2 3 // requiring the values to send 4 var 5 getmessage = request params message, 6 getphoneto = '+target test phone number', 7 getphonefrom = "+your first phone number", 8 accountsid = 'accountsid', 9 authtoken = 'authtoken'; 10 11 12 //require the twilio module and create a rest client 13 var client = require('twilio')(accountsid, authtoken); 14 15 client messages 16 create({ 17 body getmessage, // any number twilio can deliver to 18 from getphonefrom, // a number you bought from twilio and can use for outbound communication 19 to getphoneto // body of the sms message 20 }) 21 then(function(results) { 22 response success(results sid); 23 }) 24 catch(function(error) { 25 response error(error); 26 }) 27 }); 5 测试函数 “sendsms” 您还可以在客户端 sdk 中测试该功能,但现在我们将使用 rest api 命令来发送它: curl x post \\ h "x parse application id app id" \\ h "x parse rest api key rest key" \\ h "content type application/json" \\ d '{ "message" "now, i can send sms from cloud code using twilio", "phone" "+target test phone number" }' \\ https //parseapi back4app com/functions/sendsms 结果将类似于以下内容: 6 完成! 通过上述指南,您将能够在 back4app 中使用 twilio 和 cloud code 函数,并向您的客户发送 sms! 如果您需要任何帮助或某个功能/链接无法正常工作,请通过聊天与我们的团队联系!