我希望我的应用程序在某些触发器上向用户发送文本消息,最好使用诸如cfmail标记之类的东西。以前我从来没有从网络应用程序发送过短信,但是考虑到当今有大量的移动设备,我认为如果需要的话,它将内置在CF / Lucee中。但是,现在我没有在Google的文档或前几页中看到任何内容。
是否可以直接从Lucee发送短信?我知道我可以使用cfmail将消息发送到运营商的网关(即:[email protected]),但这需要我知道并维护每个运营商的地址方案列表,并且要知道收件人与哪个运营商,我不能。我想做的只有第三方服务吗?
Twilio(https://www.twilio.com/try-twilio)使发送文本消息变得容易。您需要做的只是发出一个HTTP POST请求。
成功发送消息后,Twilio会提供有关该过程的数据,包括消息SID(系统ID)。
这是一些代码,您可以将其放在.cfm
页面中并运行它以发送消息。PLACEHOLDERS
用您的Twilio值替换三个。
在Twilio注册/登录后,您将在“仪表盘”上找到Twilio凭据ACCOUNT_SID
和AUTH_TOKEN
。
YOUR_TWILIO_PHONE_NUMBER
应该以开头+
。
<cffunction name="sendMessageWithTwilio" output="false" access="public" returnType="string">
<cfargument name="aMessage" type="string" required="true" />
<cfargument name="destinationNumber" type="string" required="true" />
<cfset var twilioAccountSid = "YOUR_ACCOUNT_SID" />
<cfset var twilioAuthToken = "YOUR_AUTH_TOKEN" />
<cfset var twilioPhoneNumber = "YOUR_TWILIO_PHONE_NUMBER" />
<cfhttp
result="result"
method="POST"
charset="utf-8"
url="https://api.twilio.com/2010-04-01/Accounts/#twilioAccountSid#/Messages.json"
username="#twilioAccountSid#"
password="#twilioAuthToken#" >
<cfhttpparam name="From" type="formfield" value="#twilioPhoneNumber#" />
<cfhttpparam name="Body" type="formfield" value="#arguments.aMessage#" />
<cfhttpparam name="To" type="formfield" value="#arguments.destinationNumber#" />
</cfhttp>
<cfif result.Statuscode IS "201 CREATED">
<cfreturn deserializeJSON(result.Filecontent.toString()).sid />
<cfelse>
<cfreturn result.Statuscode />
</cfif>
</cffunction>
<cfdump var='#sendMessageWithTwilio(
"This is a test message.",
"+17775553333"
)#' />
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句