Back to browse
One sentence to develop a whatsapp chat application
Below, we will use the latest version of Wchaty to develop a WhatsApp chat robot application using TS language. The following is the official provided example…
Added May 19, 20260 views0 copies
Prompt
Below, we will use the latest version of Wchaty to develop a WhatsApp chat robot application using TS language. The following is the official provided example code: #!/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-whatsapp', }) 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 these codes to implement our requirements, and pay special attention to the method of obtaining the message type is types.Message instead of Message.Type. In the next question, I will describe the specific needs. For this question, you only need to reply "I am ready, please tell me your needs", and do not reply with any additional information. When I continue to state the requirements, I will give the sample code of ts implementation, and add the necessary Chinese comments in the code.Replace text in [BRACKETS] with your own values before pasting.