您如何从Lucee Web应用程序向手机发送txt消息?

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?

小智 6

我不知道有任何方法可以在ColdFusion中本地执行此操作,但是我已经使用Twilio使用Twilio API从ColdFusion发送SMS消息:https ://www.twilio.com/sms

他们提供免费的开发人员帐户,因此您可以在购买前尝试。

  • 这是它正在使用的另一个示例:https://www.bennadel.com/blog/1966-sending-sms-text-messages-from-twilio-using-coldfusion.htm是的,它可以在一天内完成 (2认同)

Ale*_*ban 5

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)

  • 我只是想停下来说一下,我有机会尝试一下,而且效果非常好;只需将代码放入我的 cfc 中即可完成。我没想到答案中有如此完整的解决方案;你节省了我一两个小时的工作时间。谢谢! (2认同)