当前位置:   article > 正文

微信自动回复机器人weChaty+gpt(三分钟速成)_chagpt on wechat 如何自己问自己

chagpt on wechat 如何自己问自己

1、前言

最近工作比较闲,想玩点有意思的活,想到微信能不能搞个自动回复,加上ai现在这么火,微信结合gpt搞个机器人,这样朋友们就能和机器人聊天了哈哈哈哈哈哈,还能问问题想想就好玩

2、weChaty

  1. 新建一个文件夹
  2. 在这个文件夹的终端中(或者用vscode打开这个文件件), 执行 npm init -y 生成package-json文件
  3. 安装依赖
npm i wechaty
npm i qrcode-terminal     // 生成二维码要用的包
  • 1
  • 2
  1. 新建index,js文件
    注意:看第四点获取chatAPI key
const  { WechatyBuilder }  = require('wechaty');
const qrcode = require('qrcode-terminal');
const axios = require('axios');

 
class weChaty {
  bot = null              // 机器人实例
  constructor() {
    this.bot =  WechatyBuilder.build();
    this.bot.on('scan', code => {
      qrcode.generate(code, { small: true });
    })
    this.bot.on('message', this.onMessage.bind(this));
  }
  onMessage(message) {

    const talker = message.talker(); // 发送消息的联系人

    if(!talker.payload.friend || message.payload.roomId || talker.payload.type != 1) {   // 不是朋友发的消息,群消息
      return;
    }

    if(message.payload.type != 7) {   // 消息内容不是文字
      talker.say("我只能处理文字消息,请发送文字内容");
      return;
    }
    
    //限制你要发给谁,写你朋友的备注名
    if(talker.payload.alias == ""){
        const content = message.text(); // 消息内容
        chatgpt(content).then(response =>{
          talker.say(response);
        }).catch(error =>{
          console.log(error)
        });
    }
  }
  run() {
    this.bot.start();
  }
}

function chatgpt(message){
  // 定义 API 地址和 Token
  const apiUrl = 'https://api.chatanywhere.tech/v1/chat/completions';
  const token = 'ChatAnywhere API Key';
  // 定义要发送的数据
  let postData = {
      "model": "gpt-3.5-turbo",
      "messages": [{"role": "user", "content": ""}]
  };
  postData.messages[0].content = message
  // 设置请求头,包括 Token
  const headers = {
    'Authorization': `Bearer ${token}`,
    'Content-Type': 'application/json',
  };
  // 发起 POST 请求
  return new Promise((resolve,reject) =>{
    axios.post(apiUrl, postData, { headers })
    .then(response => {
      resolve(response.data.choices[0].message.content);
    })
    .catch(error => {
      reject('Error:', error);
    });
  }) 
}
new weChaty().run();
  • 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

3、数据格式

讲解上面代码的message、talker的数据格式
message数据格式
在这里插入图片描述

talker数据格式
在这里插入图片描述

4、调用ChatAnywhere API

https://chatanywhere.apifox.cn/

  1. 免费领取API key
    https://gitcode.com/chatanywhere/GPT_API_free?utm_source=gold_browser_extension&utm_source=csdn_github_accelerator&isLogin=1
    在这里插入图片描述

  2. 安装axios库调用接口(一般装了npm应该就有,无需安装)
    npm install axios

  3. 调用接口后的数据格式
    在这里插入图片描述

5、效果

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/936372
推荐阅读
相关标签
  

闽ICP备14008679号