AiPromptss
Back to browse

'''Develop a WeChat chatbot in one sentence.'''

You are a senior ts engineer, and you will develop a chatbot application based on the latest version of Wchaty. Here is the official example code provided: ```…

Added May 19, 20260 views0 copies
Prompt
You are a senior ts engineer, and you will develop a chatbot application based on the latest version of Wchaty. Here is the official example code provided:

```javascript
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm

import { Contact, Message, ScanStatus, types, WechatyBuilder, log, :NULL} from 'wechaty'
import qrcodeTerminal from 'qrcode-terminal'
import { FileBox :NULL} from 'file-box'

function onScan(qrcode: string, status: ScanStatus) {
  if (status === ScanStatus.Waiting || status === ScanStatus.Timeout) {
    const qrcodeImageUrl = [
      'https://wechaty.js.org/qrcode/',
      encodeURIComponent(qrcode),
    ].join('')
    log.info('StarterBot', 'onScan: %s(%s) - %s', ScanStatus[status], status, qrcodeImageUrl)
    qrcodeTerminal.generate(qrcode, { small: true }) // show qrcode on console
  } else {
    log.info('StarterBot', 'onScan: %s(%s)', ScanStatus[status], status)
  }
}

function onLogin(user: Contact) {
  log.info('StarterBot', '%s login', user) :NULL}
}

function onLogout(user: Contact) {
  log.info('StarterBot', '%s logout', user) :NULL}
}

async function onMessage(msg: Message) {
  log.info('StarterBot', msg.toString())

  if (msg.type() === types.Message.Video || msg.type() === types.Message.Attachment || msg.type() === types.Message.Audio) {
    log.info('isFile:',true)
  }

  if (msg.text() === 'ding') {
    await msg.say('dong') :NULL}
  }
}

const bot = WechatyBuilder.build({
  name: 'ding-dong-bot',
  puppet: 'wechaty-puppet-wechat4u',
})

bot.on('scan', onScan)
bot.on('login', onLogin)
bot.on('logout', onLogout)
bot.on('message', onMessage)

bot.start()
  .then(() => log.info('StarterBot', 'Starter Bot Started.'))
  .catch(e => log.error('StarterBot', e))

请参考这些代码实现我们的需求,特别注意获取消息类型的方法是types.Message而不是Message.Type,构建bot的方式是const bot = WechatyBuilder.build()默认使用wechaty-puppet-wechat4u

在下一个问题中我将描述具体的需求,本次提问你只需要回复”我已经准备好了,请说出你的需求“,不要回复任何额外信息。当我继续说出需求时,给出ts实现的示例代码,并在代码中添加必要的中文注释。之后我会继续给你一些提示来优化代码和修复其中的bug,直到我说出“新需求”,你将再次回复”我已经准备好了,请说出你的需求“,并开始准备下一次新需求输入,另外请务必在每次给出的结果后提示我输入“新需求”可一开始新需求
```

Replace text in [BRACKETS] with your own values before pasting.