wechaty消息防撤回(node)

const handleAddHistoryMsg = async ({ msg, room }) => {
  if (historyMsgList.length < 10) {
    if (room) {
      const contact = msg.talker()
      let roomAlias = await room.alias(contact)
      historyMsgList.push({ msg, roomAlias })
    }
  } else {
    if (room) {
      const contact = msg.talker()
      let roomAlias = await room.alias(contact)
      historyMsgList.push({ msg, roomAlias })
      historyMsgList.shift()
    }
  }
}

const handleGetDeleteMsg = ({ msg, alias, bot }) => {
  historyMsgList.forEach(async (item) => {
    if (msg.payload.text.includes(item.msg.id)) {
      let deleteMsgAlias = item.roomAlias ? item.roomAlias : alias
      const msgType = item.msg.type()
      const room = msg.room()
      if (room) {
        if (global.isLocal) {
          const roomName = await room?.topic()

          if (roomWhiteList.includes(roomName)) {
            if (msgType === bot.Message.Type.Text) {
              await room.say(`${deleteMsgAlias},我是机器人,我知道你撤回了什么消息:
${item.msg.text()}`)
            } else if (msgType === bot.Message.Type.Emoticon) {
              // const fileBox = await item.msg.toFileBox()
              // let filePath = `/temp/ai/wx/${fileBox.name}`
              // await fileBox.toFile(filePath, true)
              // const fileBox1 = FileBox.fromFile(filePath)
              // room.say(item.msg)

              // room.say(`${item.msg.text()}`)
              // room.say(fileBox1)

              try {
                await item.msg.forward(room)
                await room.say(
                  `${deleteMsgAlias},我是机器人,上面是你撤回的表情包`
                )
              } catch (error) {
                await room.say(
                  `${deleteMsgAlias},我是机器人,你撤回了一个表情包,但是你的表情包比较特殊,我无法转发,这不是bug,详情信息:${error}`
                )
              }
            } else if (fileTypeList.includes(msgType)) {
              const fileBox = await item.msg.toFileBox()
              await room.say(
                `${deleteMsgAlias},我是机器人,我知道你撤回了什么消息:`
              )
              await room.say(fileBox)
            }
          }
        } else {
          if (alias.includes('徐同保') === false) {
            if (msgType === bot.Message.Type.Text) {
              await room.say(`${deleteMsgAlias},我是机器人,我知道你撤回了什么消息:
${item.msg.text()}`)
            } else if (msgType === bot.Message.Type.Emoticon) {
              try {
                await item.msg.forward(room)
                await room.say(
                  `${deleteMsgAlias},我是机器人,上面是你撤回的表情包`
                )
              } catch (error) {
                await room.say(
                  `${deleteMsgAlias},我是机器人,你撤回了一个表情包,但是你的表情包比较特殊,我无法转发,这不是bug,详情信息:${error}`
                )
              }
            } else if (fileTypeList.includes(msgType)) {
              const fileBox = await item.msg.toFileBox()
              await room.say(
                `${deleteMsgAlias},我是机器人,我知道你撤回了什么消息:`
              )
              await room.say(fileBox)
            }
          }
        }
      }
    }
  })
}

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)
  }
  fileTypeList = [
    bot.Message.Type.Attachment,
    bot.Message.Type.Audio,
    bot.Message.Type.Image,
    bot.Message.Type.Video,
  ]
  if (msg.mentionSelf && (await msg.mentionSelf())) {
    //机器人被@
    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) {
    //消息防撤回
    handleGetDeleteMsg({ msg, alias, bot })
  } else if (msgType === bot.Message.Type.Text) {
    handleAddHistoryMsg({ msg, room, alias })

    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

    //#region 性别统计
    if (content.length === 4) {
      let startContent = content
      if (startContent === '性别统计') {
        return await handleGenderStatistics({
          bot,
          isAlias,
          alias,
          isRoom,
          room,
          contact,
        })
      }
    }

    //#endregion

    //#region 翻牌子
    if (content.length === 3) {
      let startContent = content
      if (startContent === '翻牌子') {
        return await handleSelectOneFriend({
          bot,
          isAlias,
          alias,
          isRoom,
          room,
          contact,
        })
      }
    }

    //#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 (msgType === bot.Message.Type.Emoticon) {
    handleAddHistoryMsg({ msg, room, alias })
  } else if (fileTypeList.includes(msgType)) {
    handleAddHistoryMsg({ msg, room, alias })
    if (waitingUploadType && waitingUploadAlias === alias) {
      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('不是文本也不是上传文件')
  }
}

人工智能学习网站

https://chat.xutongbao.top

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐同保

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值