如何使用 twilio 从 MS Access 发送消息?
Posted
技术标签:
【中文标题】如何使用 twilio 从 MS Access 发送消息?【英文标题】:How to send a message from MS Access using twilio? 【发布时间】:2013-06-19 20:03:50 【问题描述】:我做了很多研究,但我一无所获。反正已经这样做了吗?有什么例子吗?
我想使用表单中的按钮发送消息,因此它应该调用 vba 代码并使用相同表单中的一些信息来发送消息。任何想法都会很棒!
我正在使用 Access 2010
【问题讨论】:
能否请您发布您迄今为止所做的尝试? 【参考方案1】:Twilio 布道者在这里。
简短的回答是您需要使用 VBA 发布到 Twilio REST API。
这里有一些示例代码,展示了如何使用 XMLHTTP 来做到这一点:
Public Function GET_MESGS()
Dim Message As String
Dim Number As String
On Error GoTo Error_Handler
Const NOINTERNETAVAILABLE = -2147012889
Dim objSvrHTTP As XMLHTTP
Dim varProjectID, varCatID, strT As String
Set objSvrHTTP = New XMLHTTP
objSvrHTTP.Open "POST", "https://api.twilio.com/2010-04-01/Accounts/[YOUR_ACCOUNT_SID]/SMS/", False, "[YOUR_ACCOUNT_SID]", "[YOUR_AUTH_TOKEN]"
objSvrHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objSvrHTTP.send "message=" & Message & "&from=15555555555&to=" & Number
debug.print objSvrHTTP.responseText
‘If objSvrHTTP.status = 201 Then
‘txtXML = objSvrHTTP.responseText
‘MsgBox "Sent"
ElseIf objSvrHTTP.status = 400 Then
MsgBox "Failed with error# " & _
objSvrHTTP.status & _
" " & objSvrHTTP.statusText & vbCrLf & vbCrLf
ElseIf objSvrHTTP.status = 401 Then
MsgBox "Failed with error# " & objSvrHTTP.status & _
" " & objSvrHTTP.statusText & vbCrLf & vbCrLf
Else
MsgBox "Failed with error# " & objSvrHTTP.status & _
" " & objSvrHTTP.statusText
End If
Exit_Procedure:
On Error Resume Next
Set objSvrHTTP = Nothing
Exit Function
Error_Handler:
Select Case Err.Number
Case NOINTERNETAVAILABLE
MsgBox "Connection to the internet cannot be made or " & _
"Twilio website address is wrong"
Case Else
MsgBox "Error: " & Err.Number & "; Description: " & Err.Description
Resume Exit_Procedure
Resume
End Select
End Function
德文
【讨论】:
谢谢德文。我非常感谢您的帮助。但是,我收到 Failed with error # 400。有什么特殊原因吗? 没关系,它的效果很棒。谢谢德文!我很感激!【参考方案2】:这是一个 VBA 示例,可以很容易地适应 Access。
Sub SendSMS()
'credit to Coding is Fun
'https://www.youtube.com/channel/UCZjRcM1ukeciMZ7_fvzsezQ
'https://www.youtube.com/watch?v=v49tYBmnnFg&list=FLDXOykjNf5zoOlCFtYYyPng&index=3
'Authentication
ACCOUNTSID = ActiveSheet.Range("C4").Value
AUTHTOKEN = ActiveSheet.Range("C5").Value
'Variables
fromNumber = "%2B" & ActiveSheet.Range("C6").Value 'use as variable or something else to call number DO NOT ADD leading +
toNumber = ActiveSheet.Range("C8").Value 'use as variable or something else to call number DO NOT ADD leading +
BodyText = ActiveSheet.Range("C9").Value
'Use XML HTTP
Set Request = CreateObject("MSXML2.ServerXMLHTTP.6.0")
'Specify URL
Url = "https://api.twilio.com/2010-04-01/Accounts/" & ACCOUNTSID & "/Messages.json"
'Post URL with authentication
Request.Open "POST", Url, False, ACCOUNTSID, AUTHTOKEN
'Request Header
Request.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
'Concatenate PostData
postData = "From=" & fromNumber & "&To=%2B" & toNumber & "&Body=" & BodyText
'Send the Post Data
Request.send postData
'[OPTIONAL] Get response text (result)
MsgBox Request.responseText
End Sub
【讨论】:
以上是关于如何使用 twilio 从 MS Access 发送消息?的主要内容,如果未能解决你的问题,请参考以下文章
如何将日期从 C# 存储到 MS-Access 以及如何检索它?