wechaty消息防撤回功能

 

let historyMsgList = []
const handleAddHistoryMsg = ({ msg }) => {
  if (historyMsgList.length < 10) {
    historyMsgList.push(msg)
  } else {
    historyMsgList.push(msg)
    historyMsgList.shift()
  }
}

const handleGetDeleteMsg = ({ msg, alias }) => {
  historyMsgList.forEach(async (item) => {
    if (msg.payload.text.includes(item.id)) {
      console.log(item.text())
      const room = msg.room()
      if (room) {
        if (global.isLocal) {
          const roomName = await room?.topic()
          if (roomWhiteList.includes(roomName)) {
            room.say(`我是机器人,我知道你撤回了什么消息:
${item.text()}`)
          }
        } else {
          if (alias.includes('徐同保') === false) {
            room.say(`我是机器人,我知道你撤回了什么消息:
${item.text()}`)
          }
        }
      }
    }
  })
}

const handleMessage = async ({ msg, bot }) => {
  const contact = msg.talker() // 发消息人
  //const receiver = msg.to() // 消息接收人
  let content = msg.text() // 消息内容
  const room = msg.room() // 是否是群消息
  const roomName = (await room?.topic()) || null // 群名称
  let alias = (await contact.alias()) || (await contact.name()) // 发消息人昵称
  // const remarkName = await contact.alias() // 备注名称
  // const name = await contact.name() // 微信名称
  const msgType = msg.type()
  let isRoom = true // roomWhiteList.includes(roomName) || roomName?.includes('gpt')
  // const isAlias =
  //   aliasWhiteList.includes(remarkName) ||
  //   aliasWhiteList.includes(name) ||
  //   remarkName.includes('gpt')
  const isAlias = true

  if (global.isLocal) {
    //alias = '徐同保88'
    isRoom = roomWhiteList.includes(roomName)
  }


  if (msg.mentionSelf && (await msg.mentionSelf())) {
    console.log(
      'this message were mentioned me! [You were mentioned] tip ([有人@我]的提示)'
    )
    await room.say(`不要@微信机器人
免费向微信机器人提问的格式:
gpt,xxx
晓丽,xxx
speech,xxx(文本转语音)
付费后向微信机器人提问的格式:
gpt4,xxx
claude3,xxx
大明,xxx
mistral,xxx
mj,xxx(AI绘画)
gemini,xxx
文心一言v4,xxx
upload
uploadForMJ
whisper,xxx(xxx为音频或视频链接,语音转文本)
可以加微信机器人私聊
网站:
https://chat.xutongbao.top`)
  } else if (msgType === 13) {
    console.log(msg.payload.text)
    handleGetDeleteMsg({ msg, alias })
  } else if (msgType === bot.Message.Type.Text) {
    handleAddHistoryMsg({ msg })

    let message = ''
    let gptVersion = ''
    content = content.trim()
    let wxMsgType = ''
    let contentObj = {}

    //#region mj帮助
    if (content.length >= 3) {
      let startContent = content.slice(0, 3)
      startContent = startContent.toLowerCase()
      if (startContent === 'mj?' || startContent === 'mj?') {
        let mjHelp = `### midjourney提问格式文档
#### 一次画四个图
mj,xxx
#### 备注
- 提示词支持中文
- 如果需要使用【--v】之类的指令请使用英文提示词
`

        /*
let mjHelp = `### midjourney提问格式文档
#### 一次画四个图
mj,xxx

#### 一次画一个图
mj1,xxx

#### 一次画四个图
{
"type": "mj",
"prompt": "xxx",
"drawType": "grid"
}

#### 选择四个图中的某一个放大
U1代表第一个,还有U2、U3、U4
parent_id的值为一次画四个图时返回的ID
{
"type": "mj",
"pick": "U1",
"parent_id": 123
}

#### 选择四个图中的某一个再次生成四个相似的图
V1代表第一个,还有V2、V3、V4
parent_id的值为一次画四个图时返回的ID
{
"type": "mj",
"pick": "V1",
"parent_id": 123
}
`
*/

        if (room) {
          room.say(mjHelp)
        } else if (contact) {
          contact.say(mjHelp)
        }
        return
      }
    }
    //#endregion

    let assistantName = ''
    //#region json格式的提示词
    try {
      contentObj = JSON.parse(content) ? JSON.parse(content) : {}
      if (contentObj.type === 'sd') {
        console.log('sd绘画', contentObj)
        wxMsgType = 'sd'
      } else if (contentObj.type === 'textToAudioByMicrosoft') {
        console.log('文本转语音', contentObj)
        wxMsgType = 'textToAudioByMicrosoft'
      } else if (contentObj.type === 'mj') {
        console.log('mj绘画', contentObj)
        contentObj.message = contentObj.prompt
          ? contentObj.prompt
          : contentObj.message
        wxMsgType = 'mj'
      } else {
        console.log('contentObj', contentObj)
      }
    } catch (error) {
      if (content.includes('"type":')) {
        if (room) {
          room.say(`JSON格式错误,${error}`)
        } else if (contact) {
          contact.say(`JSON格式错误,${error}`)
        }
      }
      //todo
    }
    //#endregion

    //#region 智能问答
    if (content.length > 4) {
      let startContent = content.slice(0, 4)
      startContent = startContent.toLowerCase()

      if (
        startContent === 'gpt,' ||
        startContent === 'gpt,' ||
        startContent === 'gpt '
      ) {
        message = content.slice(4)
        gptVersion = '3.5'
      }
    }
    if (content.length > 5) {
      let startContent = content.slice(0, 5)
      startContent = startContent.toLowerCase()
      if (
        startContent === 'gpt4,' ||
        startContent === 'gpt4,' ||
        startContent === 'gpt4 '
      ) {
        message = content.slice(5)
        gptVersion = '4'
      }
    }
    if (content.length > 3) {
      let startContent = content.slice(0, 3)
      if (
        startContent === '晓丽,' ||
        startContent === '晓丽,' ||
        startContent === '晓丽 '
      ) {
        message = content.slice(3)
        gptVersion = '3.5'
        assistantName = 'xiaoli'
      }
    }
    if (content.length > 3) {
      let startContent = content.slice(0, 3)
      if (
        startContent === '大明,' ||
        startContent === '大明,' ||
        startContent === '大明 '
      ) {
        message = content.slice(3)
        gptVersion = '4'
        assistantName = 'daming'
      }
    }
    if (content.length > 7) {
      let startContent3 = content.slice(0, 7)
      startContent3 = startContent3.toLowerCase()
      if (
        startContent3 === 'gemini,' ||
        startContent3 === 'gemini,' ||
        startContent3 === 'gemini '
      ) {
        message = content.slice(7)
        gptVersion = 'gemini'
      }
    }
    if (content.length > 7) {
      let startContent = content.slice(0, 7)
      startContent = startContent.toLowerCase()
      if (
        startContent === '文心一言v4,' ||
        startContent === '文心一言v4,' ||
        startContent === '文心一言v4 '
      ) {
        message = content.slice(7)
        gptVersion = 'ERNIE-Bot-4'
      }
    }
    if (content.length > 8) {
      let startContent = content.slice(0, 8)
      startContent = startContent.toLowerCase()
      if (
        startContent === 'claude3,' ||
        startContent === 'claude3,' ||
        startContent === 'claude3 '
      ) {
        message = content.slice(8)
        gptVersion = 'claude3'
      }
    }
    if (content.length > 8) {
      let startContent = content.slice(0, 8)
      startContent = startContent.toLowerCase()
      if (
        startContent === 'mistral,' ||
        startContent === 'mistral,' ||
        startContent === 'mistral '
      ) {
        message = content.slice(8)
        gptVersion = 'mistral-large-latest'
      }
    }
    if (gptVersion) {
      wxMsgType = 'gpt'
    }
    //#endregion

    //#region midjourney
    if (content.length > 3) {
      let startContent = content.slice(0, 3)
      startContent = startContent.toLowerCase()
      if (
        startContent === 'mj,' ||
        startContent === 'mj,' ||
        startContent === 'mj '
      ) {
        message = content.slice(3)
        contentObj = {
          type: 'mj',
          message,
          prompt: message,
          drawType: 'grid',
          msgFrom: 'wx_mj',
        }
        wxMsgType = 'mj'
      }
    }
    if (content.length > 4) {
      let startContent = content.slice(0, 4)
      startContent = startContent.toLowerCase()
      if (
        startContent === 'mj1,' ||
        startContent === 'mj1,' ||
        startContent === 'mj1 '
      ) {
        message = content.slice(4)
        contentObj = {
          type: 'mj',
          message,
          prompt: message,
        }
        wxMsgType = 'mj'
      }
    }
    if (content.length > 6) {
      if (
        (content.charAt(0) === 'U' || content.charAt(0) === 'V') &&
        content.includes(':')
      ) {
        contentObj = {
          type: 'mj',
          pick: content.slice(0, 2),
          parent_id: content.split(':')[1] - 0,
        }
        wxMsgType = 'mj'
      }
    }
    if (content.length > 6) {
      let startContent = content.slice(0, 4)
      startContent = startContent.toLowerCase()
      if (startContent === 'mjs,') {
        message = content.slice(4)
        contentObj = {
          type: 'mj',
          message,
          prompt: message,
          msgFrom: 'wx_mjs',
        }
        wxMsgType = 'mj'
      }
    }

    //#endregion

    //#region 文本转语音
    if (content.length > 7) {
      let startContent = content.slice(0, 7)
      startContent = startContent.toLowerCase()
      if (
        startContent === 'speech,' ||
        startContent === 'speech,' ||
        startContent === 'speech '
      ) {
        message = content.slice(7)
        let voiceName = ['zh-CN', 'zh-CN-YunxiNeural']
        let style = 'chat'
        let role = 'YoungAdultMale'
        if (message.includes('--type=')) {
          const options = {
            string: ['name', 'age', 'type'], // 字符串类型的选项
            boolean: ['verbose'], // 布尔类型的选项
          }
          const cmdArr = parseCommandLine(message)
          let cmdResult = parseArgs(cmdArr, options)
          let resultStr = cmdResult['_'].join(' ')
          if (resultStr) {
            message = resultStr
          }
          if (cmdResult.type) {
            let resultIndexVoiceList = voiceList.findIndex(
              (item) => item.id === cmdResult.type
            )
            if (resultIndexVoiceList >= 0) {
              voiceName = voiceList[resultIndexVoiceList].voiceName
              style = voiceList[resultIndexVoiceList].style
              role = voiceList[resultIndexVoiceList].role
            }
          }
        }

        contentObj = {
          type: 'textToAudioByMicrosoft',
          dataList: [
            {
              componentType: 'msttsExpressAs',
              message: message,
              voiceName,
              style,
              role,
              styledegree: 1,
              delayTime: 500,
            },
          ],
        }
        wxMsgType = 'textToAudioByMicrosoft'
      }
    }

    //#endregion

    //#region 上传
    if (content.length === 6) {
      let startContent = content.slice(0, 6)
      startContent = startContent.toLowerCase()
      if (startContent === 'upload') {
        waitingUploadType = 'upload'
        waitingUploadAlias = alias
        return await handleUploadFileBefore({
          isAlias,
          alias,
          isRoom,
          room,
          contact,
        })
      }
    }
    if (content.length === 11) {
      let startContent = content.slice(0, 11)
      startContent = startContent.toLowerCase()
      if (startContent === 'uploadformj') {
        waitingUploadType = 'uploadformj'
        waitingUploadAlias = alias
        return await handleUploadFileBefore({
          isAlias,
          alias,
          isRoom,
          room,
          contact,
        })
      }
    }
    //#endregion

    //#region 语音转文本
    if (content.length > 8) {
      let startContent = content.slice(0, 8)

      startContent = startContent.toLowerCase()
      if (
        startContent === 'whisper,' ||
        startContent === 'whisper,' ||
        startContent === 'whisper '
      ) {
        message = content.slice(8)

        return await handleWhisper({
          isAlias,
          alias,
          isRoom,
          room,
          contact,
          message,
        })
      }
    }
    //#endregion

    //#region google搜索
    if (content.length > 7) {
      let startContent = content.slice(0, 7)

      startContent = startContent.toLowerCase()
      if (
        startContent === 'google,' ||
        startContent === 'google,' ||
        startContent === 'google '
      ) {
        message = content.slice(7)

        return await handleGoogleSearch({
          isAlias,
          alias,
          isRoom,
          room,
          contact,
          message,
        })
      }
    }
    //#endregion

    //#region 图片转文字
    if (content.length > 10) {
      let startContent = content.slice(0, 10)

      startContent = startContent.toLowerCase()
      if (
        startContent === 'imgtotext,' ||
        startContent === 'imgtotext,' ||
        startContent === 'imgtotext '
      ) {
        message = content.slice(10)

        return await handleImgToText({
          isAlias,
          alias,
          isRoom,
          room,
          contact,
          message,
        })
      }
    }
    //#endregion

    if (wxMsgType === 'gpt') {
      console.log('gpt,alias', alias)
      console.log(JSON.stringify(msg))
      return await handleGPT({
        isAlias,
        alias,
        isRoom,
        room,
        message,
        gptVersion,
        contact,
        assistantName,
      })
    } else if (wxMsgType === 'sd') {
      console.log('sd,alias', alias)
      console.log(JSON.stringify(msg))
      await handleSD({ isAlias, alias, isRoom, room, contentObj, contact })
    } else if (wxMsgType === 'textToAudioByMicrosoft') {
      console.log(JSON.stringify(msg))
      await handleTextToAudio({
        isAlias,
        alias,
        isRoom,
        room,
        contentObj,
        contact,
      })
    } else if (wxMsgType === 'mj') {
      if (global.isLocal) {
        if (alias === '徐同保') {
          console.log(JSON.stringify(msg))
          await handleMj({
            isAlias,
            alias,
            isRoom,
            room,
            message,
            contentObj,
            contact,
          })
        } else {
          console.log('本地调试mj,其他人不能用')
        }
      } else {
        console.log(JSON.stringify(msg))
        await handleMj({
          isAlias,
          alias,
          isRoom,
          room,
          message,
          contentObj,
          contact,
        })
      }
    }
  } else if (waitingUploadType && waitingUploadAlias === alias) {
    const fileTypeList = [
      bot.Message.Type.Attachment,
      bot.Message.Type.Audio,
      bot.Message.Type.Image,
      bot.Message.Type.Video,
    ]
    if (fileTypeList.includes(msg.type())) {
      const fileBox = await msg.toFileBox()
      console.info(`Saving file ${fileBox.name}...`)
      let filePath = `/temp/ai/wx/${fileBox.name}`
      await fileBox.toFile(filePath, true)
      console.log('保存成功')
      await handleUploadFile({
        isAlias,
        alias,
        isRoom,
        room,
        message: '',
        contentObj: {},
        contact,
        fileBox,
        filePath,
        waitingUploadType,
      })
    } else {
      console.log('文件类型错误')
    }
  } else {
    //console.log('不是文本也不是上传文件')
  }
}

人工智能学习网站

https://chat.xutongbao.top

相关推荐

  1. kafka 消息丢失/消息重复设计分析

    2024-04-24 16:50:03       20 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-24 16:50:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-24 16:50:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-24 16:50:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-24 16:50:03       20 阅读

热门阅读

  1. 基于Hadoop的石油大数据平台设计

    2024-04-24 16:50:03       15 阅读
  2. css中backface-visibility使用

    2024-04-24 16:50:03       13 阅读
  3. docker 故障解决

    2024-04-24 16:50:03       15 阅读
  4. bash test.sh 2>&1 &是什么意思?

    2024-04-24 16:50:03       38 阅读
  5. 国产系统注意事项

    2024-04-24 16:50:03       12 阅读
  6. centos常见命令及进阶命令

    2024-04-24 16:50:03       18 阅读