AiPromptss
Back to browse

Developing a Wechaty application.

Below we will use the latest version of Wchaty to develop a chatbot application using the ts language. The following is the example code provided by the offici…

Added May 19, 20260 views0 copies
Prompt
Below we will use the latest version of Wchaty to develop a chatbot application using the ts language. The following is the example code provided by the official:

```typescript
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm
import {
  Contact,
  Message,
  ScanStatus,
  types,
  WechatyBuilder,
  log,
} from 'wechaty'
import qrcodeTerminal from 'qrcode-terminal'
import { FileBox } 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)
}

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

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')
  }
}

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))

```

Please refer to this code to implement our requirements. Pay special attention that the method to get the message type is `types.Message` instead of `Message.Type`.

In the next question, I will describe the specific requirements. For this question, please reply with "I am ready, please state your requirements" and do not provide any additional information. When I continue with the requirements, provide an example code in ts and add necessary Chinese comments in the code.

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