微信接入chatgpt开源源码- 放在微信群里 然后有人@你的名字 机器人会自动回复

// 是否本系统的密钥
var isSystemToken = function (key) {
var response = Http.post().url('https://www.byjxy.cn/magic-api/user-token/click-check')
.param('token', key)
.param("type", "6")
.param("isSpecial", "true")
.build().call()
if (!response) {
return false
}

var body = response.body().string()
var json = JSON.parse(body)
return json.code == 1
}
// 获取当前ai性格列表
var getAiXg = function () {
var response = Http.get().url('https://www.byjxy.cn/magic-api/scene/ai-natrues')
.build().call()
if (!response) {
return false
}

var body = response.body().string()
var json = JSON.parse(body)
if (json.code != 1) {
Console.log(json.message)
}
return json.data
}
var saveXg = function(id,token){
Http.post().url('https://www.byjxy.cn/magic-api/scene//token-nature')
.param('id', id)
.param('token',token)
.build().call()
}
// 校验是否还可以使用
var checkToken = function (key) {
var response = Http.get().url('https://viptest.byjxy.cn/magic-api/user-token/token-cnt?token=' + key)
.build().call()
if (!response) {
return false
}

var body = response.body().string()
var json = JSON.parse(body)

if (json.code != 1) {
Console.log(json.message)
} else {
Console.log('秘钥使用情况:' + json.data)
}

return json.code == 1
}

// 新增次数
var addTokenUse = function (key) {
var response = Http.post().url('https://viptest.byjxy.cn/magic-api/user-token/click-cnt')
.param('token', key)
.build().call()

if (!response) {
return false
}

var body = response.body().string()
var json = JSON.parse(body)
return json.code == 1
}
var callChatGpt3 = function (text) {
Console.log('启用加速口令:' + gptKey.substr(-6))
Console.log('-----------------------------3')
if (!Utils.endsWith(text, "?") && !Utils.endsWith(text, "?")) {
text = text + "?"
}

var messages = new java.util.ArrayList();
var map = new java.util.HashMap();
map.put("role", "user");
// map.put("content", '你是一微信客服小助手,你正在微信群帮助大家解决问题。' + text)
map.put("content", text)
messages.add(map)
var data = {
"model": "gpt-3.5-turbo",
"tokensLength": 1024,
"messages": messages
}
Console.log("machineName=="+machineName)
var para = {
"token": gptKey,
"questions": data,
"machineName": machineName,
"id":id
}

// var response = Http.post().url('https://viptest.byjxy.cn/voice-api/voice/ai/talk-script')
// .params(para)
// .build().call()
var response = Http.post().url('https://viptest.byjxy.cn/voice-api/voice/ai/talk-script-gpt4')
.params(para)
.build().call()

if (!response) {
return null
}

var body = response.body().string()
var json = JSON.parse(body)

if (json == null) {
Console.log('gpt请求超时!')
return null
}

if (!json.data) {
Console.log('gpt调用数据错误:' + body)
return
}
return json
}
var getGptText = function (text) {
var isCan = checkToken(gptKey)
if (!isCan) {
return false
}

if (!text) {
return false;
}
if (Utils.contain(text, '习进平') ||
Utils.contain(text, '共产党') ||
Utils.contain(text, 'vpn') ||
Utils.contain(text, '中国') ||
Utils.contain(text, 'VPN')||
Utils.contain(text, '赌博')||
Utils.contain(text, '毁灭')||
Utils.contain(text, '国土')||
Utils.contain(text, '台湾')){
return '11';
}
var json1 = callChatGpt3(text)
if (json1 == null || !json1.data) {
Console.log('gpt调用错误:' + json1)
Sleep(2000)
return false;
}
Toast.showLong(json1.data.choices[0].message.content)
replyText = Utils.replace(json1.data.choices[0].message.content, "\nAI:", "")
// 过滤敏感字段
replyText = Utils.replace(replyText, "AI:", "")
replyText = Utils.replace(replyText, "?", "")
replyText = Utils.replace(replyText, "?", "")
replyText = Utils.replace(replyText, "回答:", "")
replyText = Utils.replace(replyText, "回答:", "")
replyText = Utils.replace(replyText, "答案:", "")
replyText = Utils.replace(replyText, "答案:", "")
replyText = Utils.replace(replyText, "Answer:", "")
replyText = Utils.replace(replyText, "答:", "")
replyText = Utils.replace(replyText, "答:", "")
replyText = Utils.replace(replyText, "\n\n", "")
replyText = Utils.replace(replyText, "。", "。\n")
replyText = Utils.replace(replyText, "ChatGPT:", "")
replyText = Utils.replace(replyText, "ChatGPT:", "")
replyText = Utils.replace(replyText, "解释原因", "")
replyText = Utils.replace(replyText, "内容延申", "")
replyText = Utils.replace(replyText, "内容延伸", "")
replyText = Utils.replace(replyText, "明确结论", "")
replyText = Utils.replace(replyText, "明确", "")
replyText = Utils.replace(replyText, "结论", "")
replyText = Utils.replace(replyText, ":", "")
replyText = Utils.replace(replyText, ":", "")
replyText = Utils.replace(replyText, "人工智能", machineName)
replyText = Utils.replace(replyText, "聊天机器人", machineName)
Console.log('回复:' + replyText)
return replyText
}
//点击文本
var click = function (text) {
var find = false;
var hd = Node().textReg(text).find()
if (hd.size() > 0) {
find = true;
hd[0].click()
}
var hd2 = Node().desc(text).find()
if (hd2.size() > 0) {
find = true;
hd2[0].click()
}
var hd3 = Node().className(text).find()
if (hd3.size() > 0) {
find = true;
hd3[0].click()
}
if (find) {
Console.log("找到节点并且点击=" + text)
} else {
Console.log("没找到节点=" + text)
}
return find
}
//输入文本
var inputFunction = function (text, text2) {
var find = false;
var hd = Node().textReg(text).find()
if (hd.size() > 0) {
find = true;
hd[0].input(text2)
}
var hd2 = Node().desc(text).find()
if (hd2.size() > 0) {
find = true;
hd2[0].input(text2)
}
var hd3 = Node().className(text).find()
if (hd3.size() > 0) {
find = true;
hd3[0].input(text2)
}
if (find) {
Console.log("找到节点并且输入=" + text2)
} else {
Console.log("没找到输入节点=" + text)
}
return find
}
//是否存在节点
var nodeIsExit = function (text) {
var find = false;
var hd = Node().textReg(text).find()
if (hd.size() > 0) {
find = true;
}
var hd2 = Node().desc(text).find()
if (hd2.size() > 0) {
find = true;
}
var hd3 = Node().className(text).find()
if (hd3.size() > 0) {
find = true;
}
if (find) {
Console.log("找到节点=" + text)
} else {
Console.log("没找到节点=" + text)
}
return find
}

var showUi = function(){
getAiXgList = getAiXg()
if(!getAiXg){
Toast.show('系统异常!请重试试')
return;
}
var xl = <View orientation='v' w='1500' >+
<Text text='请输入秘钥:'></Text>+
<EditText id='key' ></EditText>+
<Text text='请输入Ai名称(@Ai名称 可以唤醒ai。):'></Text>+
<EditText id='name' ></EditText>+
<Text text='ai性格只能选择一种,如果选择多种则默认为最后一个。'></Text>+
<Text text='ai性格默认为人工智能'></Text>;
for(var i=0;i<getAiXgList.length;i++){
xl = xl + <View orientation='h'><Text text='*Ai性格=>+getAiXgList[i].title+:'></Text>+
<Switch id='+getAiXgList[i].id+'></Switch></View>
}
xl = xl + <Button id='start' text='启动' ></Button>+
</View>
UI.show(xl,()=>{
var key = Store.getString('wtjs-key')
if(key != null){
UI.id('key').setJsText(key)
}
var aiName = Store.getString('wtjs-name')
if(aiName != null){
UI.id('name').setJsText(aiName)
machineName = aiName
}

for(var i=0;i<getAiXgList.length;i++){
    UI.id(getAiXgList[i].id+'').onClick((e,params)=>{  
        if(params){       
                for(var j=0;j<getAiXgList.length;j++){
                    if(params[0] == getAiXgList[j].id ){
                        if(e){
                            id = params[0]
                            Toast.show('当前性格为'+getAiXgList[j].title+'模式')
                        }
                        else{
                            id = 0
                            Toast.show('关闭'+getAiXgList[j].title+'模式,默认为通用模式')
                        }
                    }
                }
        }
    },getAiXgList[i].id)
}
UI.id('start').onClick(()=>{
    gptKey = UI.id('key').getJsText()
    machineName = UI.id('name').getJsText()
    Store.put('wtjs-key', gptKey)
    var t = com.zrh.aj.core.modules.JsThread.createJsThread(()=>{
      var isSys = isSystemToken(gptKey)
      if(!isSys){
        Toast.showLong('非法密钥,请检验密钥是否正确!')
        return
      }
      if(!machineName){
        Toast.showLong('Ai名称不能为空!')
        return
      }else{
          Store.put('wtjs-name', machineName)
      }
      Device.keepScreenOn() //保持常亮
      Console.setBg('#ecf4fa')
      Console.setMode(3)
      Console.show()

      Console.log("此助手为微信小助手,接入chatgpt4.0.仅供娱乐。")
      Console.log("小助手id="+id)
      saveXg(id,gptKey)
      SetInterval(()=>{
        try{
          getMessage()
        }catch(e){
          Console.log(e)
        }
      },12000)
    })
    t.start()
})

})
}

var getMessage = function () {
var node = Node().className('android.widget.RelativeLayout').find()
for (var i = node.size() - 1; i >= 0; i--) {
var children = node[i].classNameChild('android.widget.TextView').find()
for (var j = 0; j < children.size(); j++) {
if (children[j].getText().contains('@'+machineName)) {
var message = children[j].getText()
message = Utils.replace(message, "@"+machineName, "")
if (true) {
var touxList = children[j].getSiblings()
Console.log("该文本没有发送过,准备发送消息:" + message);
for (var o = 0; o < touxList.size(); o++) {
var name = touxList[o].getDesc()
Console.log("节点信息:" + name);
if (name && name.contains('头像')) {
name = Utils.replace(name, "头像", "")
message = " " + name + ":" + message + ",请回复他。"
var find = false;
for (var k = 0; k < resultList.length; k++) {
if (resultList[k] == message) {
find = true;
}
}
if (find) {
Console.log("该文本已经发送过了");
}else{
Console.log("该文本没有发送过,准备发送消息:" + message)
Console.log("文本加入发送过后的消息队列")
resultList.push(message)
getMessage()
sendMessage(message,name)
return;
}
}
}
}

  }
}

}
}
var sendMessage = function (message,name) {
if (nodeIsExit('android.widget.EditText')) {
click('android.widget.EditText')
Sleep(1000)
var result = getGptText(message)
if (result == '11'){
Node().className('android.widget.EditText').find().input("哎呀抱歉"+name+"。您的问题涉及敏感词语,请换个问题再试试")
}
else if (result) {
Node().className('android.widget.EditText').find().input("@"+name+" "+result)
}else{
Node().className('android.widget.EditText').find().input("哎呀抱歉"+name+"。"+machineName+"出错啦!请换个问"+machineName+"一下")
}
Sleep(1000)
if (nodeIsExit('发送')) {
click('发送')
}
}
}

var machineName = 'chatGPT'
var gptKey = ''
var resultList = []
var getAiXgList = []
var id = 0

var rest = Console.requestFloating()
rest.onResolve((v)=>{
Thread(()=>{
showUi()
}) //开启一个线程并执行
}).onReject((v)=>{
Toast.show('拒绝授权')
})