Nic*_*las 3 coldfusion cfml lucee
I would like my application to send a text message to users on certain triggers, preferably using something like a cfmail tag. I've never had to send text message from a web app before, but given the huge number of mobile devices out there today I assumed this would be built into CF/Lucee if I ever needed it. However, now that I do I'm not seeing anything in the docs or first few pages of Google.
Is it possible to send text messages directly from Lucee? I know that I could use cfmail to send messages to the carrier's gateway (ie: xxxxxxxxxx@tmomail.net), but that requires me to know and maintain a list of every carrier's address scheme and know which carrier the recipient is with, which I can't. Is what I want to do only possible with a third party service?
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"
)#' />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
150 次 |
| 最近记录: |