【chrome插件】web版微信接入圖靈機器人API實現自動回復


小賤雞自動回復API已經不可以用了,現在改良接入圖靈機器人API

360chrome瀏覽器團隊翻譯了部分谷歌插件開發文檔

地址:http://open.chrome.360.cn/extension_dev/overview.html

具體封裝插件的方法請參考開發文檔。

具體代碼如下:

background.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//在當前URL下運行 https://wx.qq.com/
var  status = 0;  //0停止 1運行
//消息傳遞監聽 onRequest
chrome.extension.onRequest.addListener(
     function  (request, sender, sendResponse) {
     var  text = request.text;
     var  nick = request.nick;
     console.log(sender.tab ?
         "from a content script:"  + sender.tab.url :
         "from the extension" );
     console.log( "收到消息:"  + text);
     if  (text !=  null  && text.length > 0) {
         console.log( "請求機器人獲取回復" );
         //請求機器人獲取回復
         tulingQuery(nick,text,  function  (content,data) {
             sendResponse({
                 dstContent : content, 
                 reply : data
             });
         });
     else  {
         sendResponse({
             reply :  "未識別出留言內容"
         });
     }
 
});
 
chrome.browserAction.onClicked.addListener( function  (tab) {
     if  (tab.url.indexOf( "wx.qq.com" ) >= 0) {
         //對 contentScript 發送消息
         if  (status == 0)
             status = 1;
         else
             status = 0;
         console.log( "send request" );
         chrome.tabs.sendRequest(tab.id, {
             status : status
         },  function  (response) {
             console.log(response);
         });
         //run();
     else  {
         alert( "這個只能在微信web運行!" );
         console.log( "只能在微信web運行" );
     }
});
 
//圖靈機器人回復
function  tulingQuery(userid, txt, callback) {
     $.ajax({
         url :  'http://www.tuling123.com/openapi/api' ,
         type :  'get' ,
         data : {
             key :  'e64ad48100081ab77b668aa3105fe552' ,
             info : txt,
             userid : userid
         },
         success :  function  (data) {
             console.log(data);
             if  (callback)
                 callback(txt, data.text);
         },
         error :  function  () {}
     });
}
 
 
//  chrome.tabs.executeScript(null, {code: "console.log(666);"});

content_script.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
window.confirm =  function  () {}
var  GSid, wx =  new  WX();
//消息傳遞監聽 onRequest
chrome.extension.onRequest.addListener(
     function  (request, sender, sendResponse) {
     var  status = request.status;
     console.log(sender.tab ?
         "from a content script:"  + sender.tab.url :
         "from the extension" );
     console.log( "收到消息:"  + status);
     if  (status == 1) {
         //運行
         GSid = setInterval( function  () {
                 wx.reload();
                 var  item = wx.getReply();
                 if  (item.title ==  "紅包家族" ) {
                     wx.openLatestRoom();
                     return ;
                 }
 
                 if  (!wx.isReplied(item) && item.text !=  "" ) {
                     console.log( "判斷是否回復過了" );
                     console.log(item);
                     console.log( "未回復過" );
                     wx.replyComplete(item);
                     sendToExtension(item,  function  (content, reply) {
                         //提交回復
                         console.log( "機器人回復:"  + reply);
                         wx.putReply(item, reply +  "[機器人測試回復]" );
                     });
                 else  {
                     wx.replyComplete(item);
                     wx.openLatestRoom();
                 }
 
                 console.log( "掃描回復ing..." );
             }, 3000);
 
         alert( "自動回復啟動" );
         console.log( "自動回復啟動" );
     else  {
         //停止
         clearInterval(GSid);
         alert( "自動回復停止" );
         console.log( "自動回復停止" );
     }
     sendResponse({
         farewell :  "contentScript 收到了消息"
     });
});
 
//對插件發送消息
function  sendToExtension(item, callbackFun) {
     console.log( "send request" );
     chrome.extension.sendRequest({
         text : item.text,
         nick : item.nick
     },  function  (response) {
         var  reply = response.reply;
         var  content = response.dstContent;
         console.log(reply);
         if  (callbackFun)
             callbackFun(content, reply);
     });
}
 
//微信操作對象 var wx = new WX();
function  WX() {
 
     var  thisObj =  this ;
 
     //微信所有者
     this .wxOwner =  function  () {
         return  $( ".display_name" ).text();
     };
 
     //獲得最新的回復內容
     this .getReply =  function  () {
         var  reply = {};
         var  $lst = $( "[ng-switch] .you" );  //未回復的列表
         var  $info = $lst.not( '[reply]' ).first();  //未回復的記錄
         if  ($info !=  null  && $info.length > 0) {
             reply[ "text" ] = $info.find( ".js_message_plain" ).text();  //未回復的內容
             reply[ "nick" ] = $info.find( ".avatar" ).attr( "title" );  //未回復的備注名稱
             reply[ "title" ] = $info.parents( '.chat_bd.scroll-wrapper:first' ).prev().find( '.title_name' ).text();  //聊天框title
             reply[ "touser" ] = $info.parents( '[jquery-scrollbar]' ).attr( 'data-cm' );
             reply[ "touser" ] = JSON.parse(reply[ "touser" ]).username;
             reply[ "index" ] = $lst.index($info);
             reply[ "item" ] = $info;
         }
         return  reply;
     };
 
     //提交回復
     this .putReply =  function  (item, replyContent) {
         //$("#editArea").html("回復:[[" + dstContent + "]]        " + replyContent);
         //$(".btn_send").click();
         var  json = tl.getJsonMsg(item.touser,  "回復:[["  + item.text +  "]]     "  + replyContent);
         tl.sendMsg(json);
     };
 
     //已經回復過集合
     this .repliedArray = [];
 
     //判斷是否回復過
     this .isReplied =  function  (obj) {
         var  md5Str = faultylabs.MD5(obj.title +  ""  + obj.nick +  ""  + obj.text);
         if  (!thisObj.repliedArray.contains(md5Str)) {
             return  false ;
         else  {
             return  true ;
         }
     }
 
     //完成回復
     this .replyComplete =  function  (obj) {
         var  md5Str = faultylabs.MD5(obj.title +  ""  + obj.nick +  ""  + obj.text);
         if  (!thisObj.repliedArray.contains(md5Str)) {
             thisObj.repliedArray.push(md5Str);
         }
     }
 
     //重載處理回復列表
     this .reload =  function  (callbackFun) {
         var  $lst = $( "[ng-switch] .you" );  //未回復的列表
         $.each($lst.not( "[reply]" ),  function  (idx, item) {
             var  $info = $(item);
             var  reply = {};
             if  ($info !=  null  && $info.length > 0) {
                 reply[ "text" ] = $info.find( ".js_message_plain" ).text();  //未回復的內容
                 reply[ "nick" ] = $info.find( ".avatar" ).attr( "title" );  //未回復的備注名稱
                 reply[ "title" ] = $info.parents( '.chat_bd.scroll-wrapper:first' ).prev().find( '.title_name' ).text();  //聊天框title
                 reply[ "touser" ] = $info.parents( '[jquery-scrollbar]' ).attr( 'data-cm' );
                 reply[ "touser" ] = JSON.parse(reply[ "touser" ]).username;
                 reply[ "index" ] = $lst.index($info);
                 reply[ "item" ] = $info;
             }
             if  (thisObj.isReplied(reply))
                 $(item).attr( "reply" "" );
             if  (callbackFun)
                 callbackFun();
         });
     }
 
     //打開最新的聊天框
     this .openLatestRoom =  function  () {
         $( "[ng-repeat='chatContact in chatList track by chatContact.UserName'] .chat_item .icon:first" ).click();
     }
}
 
//數組拓展方法
Array.prototype.contains =  function  (obj) {
     var  i =  this .length;
     while  (i--) {
         if  ( this [i] === obj) {
             return  true ;
         }
     }
     return  false ;
}
 
//圖靈對象
var  tl = {
     getJsonMsg :  function  (activeroom, text) {
         var  json = {
             "BaseRequest"  : {
                 "Uin"  : +util.getCookie( "wxuin" ),
                 "Sid"  : util.getCookie( "wxsid" ),
                 "Skey"  : util.getSkey(),
                 "DeviceID"  : util.getDeviceID()
             },
             "Msg"  : {
                 "Type"  : 1,
                 "Content"  : text,
                 "FromUserName"  : util.getFromUserName(),
                 "ToUserName"  : activeroom,
                 "LocalID"  : util.Now(),
                 "ClientMsgId"  : util.Now()
             }
         };
         return  json;
     },  //
     sendMsg :  function  (json, cbfunc) {
         var  jsonStr = JSON.stringify(json);
         $.ajax({
             url :  'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg' ,
             data : jsonStr,
             type :  'POST' ,
             dataType :  'json' ,
             success :  function  (d) {
                 if  (cbfunc)
                     cbfunc(d);
             }
         });
     //
};  //
 
//工具包
var  util = {
 
     getDeviceID :  function  () {
         return  "e"  + ( ""  + Math.random().toFixed(15)).substring(2, 17)
     },
     Now :  function  () {
         return   + (+ new  Date +  ""  + Math.round(Math.random() * 10000))
     },  //
     getCookie :  function  (e) {
         for  ( var  t = e +  "=" , o = document.cookie.split( ";" ), n = 0; n < o.length; n++) {
             for  ( var  r = o[n];  " "  == r.charAt(0); )
                 r = r.substring(1);
             if  (-1 != r.indexOf(t))
                 return  r.substring(t.length, r.length)
         }
         return  ""
     },  //
     getSkey :  function  () {
         var  src = $( ".main_inner .header .avatar img" ).attr( "src" );
         var  result = /\@crypt_.{0,41}/.exec(src);
         return  result !=  null  && result.length > 0 ? result[0] :  "" ;
     },
     getFromUserName :  function  () {
         var  src = $( ".main_inner .header .avatar img" ).attr( "src" );
         var  result = /\&username=(\@.+?)\&/.exec(src);
         return  result !=  null  && result.length > 0 ? result[1] :  "" ;
     },  //
     getActiveRoom :  function  () {
         var  cmStr = $( "[ng-repeat='chatContact in chatList track by chatContact.UserName'] .active" ).attr( "data-cm" );
         var  json = JSON.parse(cmStr);
         return  json !=  null  && json.username !=  null  ? json.username :  "" ;
     //
};  //

manifest.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
     "name"  "圖靈機器人微信自動回復" ,
     "description"  "圖靈機器人微信自動回復" ,
     "version"  "1.1" ,
     "background"  : {
         "scripts"  : [ "jquery.min.js" "md5.js" "background.js" ]
     },
     "content_scripts"  : [{
             "matches"  : [ "https://*.qq.com/*" , "http://*.qq.com/*" ],
             "js"  : [ "jquery.min.js" "md5.js" "content_script.js" ]
         }
     ],
     "permissions"  : [
         "tabs" "http://*/*" "https://*/*"
     ],
     "icons"  : {
         "16"  "icon.png" ,
         "48"  "icon.png" ,
         "128"  "icon.png"
     },
     "browser_action"  : {
         "default_icon"  "icon.png"
     },
     "homepage_url"  "http://www.cnblogs.com/mahatmasmile/" ,
     "manifest_version"  : 2
}

 

額外引用的插件:

jquery.min.js

md5.js

 

文件目錄結構如下:

temp.png

插件和源文件下載地址:

http://pan.baidu.com/s/1wPHy2


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM