小贱鸡自动回复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

文件目录结构如下:

插件和源文件下载地址:

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

转载于:https://www.cnblogs.com/mahatmasmile/p/4788654.html

【chrome插件】web版微信接入图灵机器人API实现自动回复相关推荐

  1. Python使用微信接入图灵机器人

    1.wxpy库介绍 wxpy 在 itchat 的基础上,通过大量接口优化提升了模块的易用性,并进行丰富的功能扩展. 文档地址:https://wxpy.readthedocs.io 从 PYPI 官 ...

  2. 机器人编程语言python-个人微信接入图灵机器人(基于Python)

    开发环境:Ubuntu14.04 编程语言:Python2.7 1.在图灵机器人官网(http://www.tuling123.com)注册账号,创建机器人,如果只是学习的话,可以使用免费版,一个账号 ...

  3. python图灵机器人微信号_个人微信接入图灵机器人(基于Python)

    开发环境:Ubuntu14.04 编程语言:Python2.7 1.在图灵机器人官网(http://www.tuling123.com)注册账号,创建机器人,如果只是学习的话,可以使用免费版,一个账号 ...

  4. itchat微信调用图灵机器人API

    #coding=utf8 import requests import itchatKEY5 = 'fe55deeb23447c3a42c6665633507e6'def get_response(m ...

  5. 微信公众号接入图灵机器人

    微信公众号接入图灵机器人 1. 申请一个微信公众号 这个步骤和申请一个小程序了类似,首先进入微信公众平台进行注册,关键是注册的时候是选择订阅号,还是服务号,我觉的如果你是自己做着玩的,就搞个订阅号,订 ...

  6. 【大学生软件测试基础】web版微信登录测试 - 正交表

    微信是一款手机App软件,但它也有web版微信可以登录,如果要测试微信web页面运行环境,需要考虑多种因素.在众多的因素中,我们可以选出几个影响比较大的因素,如服务器.操作系统,插件和浏览器.对于选取 ...

  7. 图灵机器人api接入c语言,apikey创建接入图灵机器人

    图灵机器人很多人都听说过,这个开放平台有很多功能,今天来介绍如何用apikey接入图灵机器人.以下是python代码 http://www.turingapi.com import requests ...

  8. 用wxBot和图灵机器人API实现微信群聊机器人

    1 实现方案 用 wxBot登录微信,接收.发送微信消息. 用 图灵机器人 API对消息作回复. 2 实现效果 机器人会回复来自联系人的消息,以及群里@此账号的消息. 注意要将对应的群保存到联系人. ...

  9. 微信公众号图灵机器人开发php,使用图灵机器人api搭建微信聊天机器人php实现,图灵微信聊天机器人...

    使用图灵机器人api搭建微信聊天机器人php实现,图灵微信聊天机器人之前通过hook技术实现了微信pc端发送消息功能,如果在结合图灵机器人就能实现微信聊天机器人. 代码下载:http://blog.y ...

最新文章

  1. Ubuntu 18.04+NVidia显卡+Anaconda3+Tensorflow-GPU安装、配置、测试(无需手动安装CUDA)
  2. SDP 协议分析 http://www.cnblogs.com/qingquan/archive/2011/08/02/2125585.html
  3. MIT重磅报告:一文看清AI商业化现状与未来
  4. Linux shell 正则表达式用法
  5. psm倾向得分匹配法举例_互助问答第110期:分组回归样本及倾向得分匹配相关问题...
  6. python运行py文件 sublime 快捷键_Sublime Text配置python以及快捷键总结
  7. 八十五、再探希尔排序,桶排序,计数排序和基数排序
  8. wpf 如何设置弹出窗口必须关闭才能打开其他软件_Mac忘记登录密码?以防万一,必须收藏。...
  9. Spring-Cloud中的统一配置中心
  10. ssl1007 and ssl -最大乘积 and 加法最大【区间dp练习】
  11. 计算机病毒怎么做图片解说,【虎子_游戏解说】计算机病毒防范的实施方法
  12. Linux snmp 时间戳,linux snmp计数器汇总
  13. swift扩展Extensions
  14. vue回调函数this指向问题
  15. 从零开始学数据分析,什么程度可以找工作?
  16. linkedin android,如何在android中登录linkedin?
  17. java求1到20的阶乘
  18. Tencent QQ/TIM 文件下载漏洞的研究
  19. java crm 进销存 websocket即时聊天发图片文字 好友群组 SSM源码
  20. 求两个整数中的较大者(用函数实现)

热门文章

  1. 整理的C++面试,这些是最为常见的
  2. hive-2.3.3安装指北
  3. Swift - 多线程实现方式(3) - Grand Central Dispatch(GCD)
  4. 快速排序—三路快排 vs 双基准
  5. nginx 设置端口访问网站
  6. libnids libnet
  7. 【DFS + backtracking】LeetCode 216. Combination Sum III
  8. Leetcode 538.二叉树转换为累加树
  9. 关于马克飞象中的Latex语法与CSDN中的区别
  10. ubuntu + pycharm + anaconda + pyqt5 + tools 配置