2023-11-26 如何使用 wechaty 創(chuàng)建屬于自己的微信機(jī)器人

參考鏈接:

wechaty:https://github.com/wechaty/wechaty

文檔:https://wechaty.js.org/docs/

快速開(kāi)始:https://github.com/wechaty/wechaty-getting-started

廢話少說(shuō),直接上安裝流程!

運(yùn)行環(huán)境

首先,wechaty 是基于 TypeScript 開(kāi)發(fā)的,所以需要 nodejs 環(huán)境。

node -v

其次,需要通過(guò) npm 來(lái)安裝

npm -v

安裝

只需要?jiǎng)?chuàng)建一個(gè)新的 nodejs 項(xiàng)目后安裝 wechaty 即可。

cd <your wechaty dir>
npm install wechaty

然后就成功安裝了 wechaty !

使用

單純的安裝是沒(méi)有用的,能運(yùn)行才是真的行!

創(chuàng)建一個(gè)新的 index.ts 文件(或者其他任何你喜歡的文件名)

#!/usr/bin/env -S node --no-warnings --loader ts-node/esm
/**
 * Wechaty - Conversational RPA SDK for Chatbot Makers.
 *  - https://github.com/wechaty/wechaty
 */
// https://stackoverflow.com/a/42817956/1123955
// https://github.com/motdotla/dotenv/issues/89#issuecomment-587753552
import 'dotenv/config.js'

import {
  Contact,
  Message,
  ScanStatus,
  WechatyBuilder,
  log,
}                  from 'wechaty'

import qrcodeTerminal from 'qrcode-terminal'

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.text() === 'ding') {
    await msg.say('dong')
  }
}

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

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

以上代碼實(shí)現(xiàn)了一個(gè)最簡(jiǎn)單的微信機(jī)器人,只要你向機(jī)器人發(fā)送ding,它就會(huì)回復(fù)dong。

在運(yùn)行代碼之后,會(huì)看到一個(gè)二維碼,使用微信機(jī)器人的賬號(hào)掃碼登錄即可。(機(jī)器人推薦使用小號(hào),畢竟容易被封。)

image-20231126121629101

然后登錄成功的話就會(huì)看到 session,檢查下賬號(hào)即可

image-20231126121745054

之后,就可以盡情的使用 wechaty 提供的 API 進(jìn)行開(kāi)發(fā)了!

總結(jié)

這篇博客介紹了如何使用 Wechaty 創(chuàng)建自己的微信機(jī)器人。首先,需要安裝 Node.js 和 npm 環(huán)境。然后,通過(guò) npm 安裝 Wechaty。接下來(lái),創(chuàng)建一個(gè)新的 index.ts 文件,并編寫(xiě)代碼實(shí)現(xiàn)最簡(jiǎn)單的微信機(jī)器人功能。運(yùn)行代碼后,使用微信掃描生成的二維碼進(jìn)行登錄。登錄成功后,就可以使用 Wechaty 提供的 API 進(jìn)行開(kāi)發(fā)了。這篇博客提供了詳細(xì)的安裝和使用步驟,適合想要學(xué)習(xí)微信機(jī)器人開(kāi)發(fā)的開(kāi)發(fā)者閱讀。

【總結(jié)由 Chat LangChain 生成】

本文作者:草梅友仁
本文地址:https://blog.cmyr.ltd/archives/eb20e5d9.html
版權(quán)聲明:轉(zhuǎn)載請(qǐng)注明出處!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容