简介:本文为 Thinkai 于 2015 年整理转载的《Web Short Message(com.wangtai.smstwoman)安卓短信接口》调用示例,演示如何让 AutoHotkey 通过手机端 APP 提供的本地 HTTP 管理地址收发短信。脚本封装了三个常用功能:SendMsg() 发送短信、GetNewMsg() 拉取最新回复短信、GetContacts() 读取联系人列表,返回结果均为 JSON 格式,并由文末附带的 json_toobj() 函数解析为 AHK 关联数组,便于脚本直接读取字段(如消息正文 body、号码 address、联系人 person 等)。
使用要点:① APP 每次运行后管理网址都可能变化,请先更新脚本开头的 ManageUrl;② 本文为 AHK v1 语法,若迁移到 AutoHotkey v2 需注意:URLDownloadToVar 可用 ComObject("WinHttp.WinHttpRequest.5.1") 的 Send/Open 替代,StringSplit 改为 StrSplit() 函数,%var% 拼接改为表达式写法。该方案适合通过本地网络在电脑与安卓设备之间桥接收发短信的场景。
;Autohotkey+Web Short Message(com.wangtai.smstwoman)短信接口
;Thinkai@2015-01-05
;注意每次运行APP的管理网址都不一样
ManageUrl = http://192.168.1.106:1984/8119
;获取参数
RegExMatch(ManageUrl,"^(http://.*:d*/)(d{4})",parameter)
global parameter1
global parameter2
;运用实例
;发短信
if (SendMsg(10001,7)=1)
MsgBox, 64, 提示, 短信发送成功
;接收回复
NewMessage := GetNewMsg()
From := NewMessage[1]["person"] ? NewMessage[1]["person"] "(" NewMessage[1]["address"] ")" : "未知(" NewMessage[1]["address"] ")"
MsgBox % "From:" From "`n" NewMessage[1]["body"]
;获取联系人
Contact := GetContacts()
for k,v in Contact
{
MsgBox, 64, 提示, 第一个联系人是%v%`,号码是%k%
Return
}
;获取新消息函数(调用后收到的)
GetNewMsg(){
;返回数组说明
;数组[1]:
; _id:消息ID
; address:号码
; body:短信正文
; date:收信Uinx时间戳
; person:联系人
Loop
{
res := URLDownloadToVar(parameter1 "get_new_msg/" parameter2 "/", "utf-8","POST") ;获取最新短信的JSON数据
if (strlen(res)>2)
Return json_toobj(res)
Sleep, 100
}
}
;获取联系人函数
GetContacts(){
obj := {} ;初始数组
res := URLDownloadToVar(parameter1 "get_contacts/" parameter2 "/", "utf-8","POST") ;获取联系人的JSON数据
;处理格式问题
StringTrimLeft, res, res, 1
StringTrimRight, res, res, 1
StringSplit, var, res, `,
loop % var0
{
tmp_var := var%A_index%
RegExMatch(tmp_var,"""(.*)"":""(.*)""",m)
m1 := RegExReplace(m1,"s","")
m1 := RegExReplace(m1,"+86","")
obj["" m1] := m2 ;注意此处数组key的类型
}
Return obj ;返回数组 obj[电话号码] := 联系人姓名
}
;发短信函数
SendMsg(to,msg){ ;号码,消息
msg := urlencode(msg)
Return URLDownloadToVar(parameter1 "send/" parameter2 "/" to "/", "utf-8","POST","msg=" msg)
}
URLDownloadToVar(url, Encoding = "",Method="GET",postData=""){ ;网址,编码,请求方式,post数据
hObject:=ComObjCreate("WinHttp.WinHttpRequest.5.1")
if Method = GET
{
hObject.Open("GET",url)
Try
hObject.Send()
catch e
return -1
}
else if Method = POST
{
hObject.Open("POST",url,False)
hObject.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
Try
hObject.Send(postData)
catch e
return -1
}
if Encoding
{
oADO := ComObjCreate("adodb.stream")
oADO.Type := 1
oADO.Mode := 3
oADO.Open()
oADO.Write(hObject.ResponseBody)
oADO.Position := 0
oADO.Type := 2
oADO.Charset := Encoding
return oADO.ReadText(), oADO.Close()
}
return hObject.ResponseText
}
urlencode(string){
string := Ansi2UTF8(string)
StringLen, len, string
Loop % len
{
SetFormat, IntegerFast, hex ;运算结果为HEX
StringMid, out, string, %A_Index%, 1
hex := Asc(out) ;获取单字节ascii值
hex2 := hex ;另存变量
StringReplace, hex, hex, 0x, , All
SetFormat, IntegerFast, d
hex2 := hex2 ;十进制化
;判断是否可见单字节字符 是则直接连接 否则编码
If (hex2==33 || (hex2>=39 && hex2 =48 && hex2=65 && hex2=97 && hex2 1
} ; Loop Parse, str, % "[{"
If !--nest
Break
; Insert the newly closed object into the one on top of the stack, then pop the stack
pbj := obj
obj := objs.remove()
obj[key := keys.remove()] := pbj
If ( isarray := isarrays.remove() )
key++
} ; Loop Parse, str, % "]}"
Return obj
}

