腾讯云+wechaty实现微信群文本消息自动转发(跨群聊天)


服务器配置:轻量应用服务器,地域:硅谷,镜像:Node.js (CentOS 7.6 64位操作系统),最低的24元/月套餐

1. 在控制台上重置root密码,用root密码登录,创建普通用户,安装git,rz/sz,chrome。
https://intoli.com/blog/installing-google-chrome-on-centos/ 提供了一行命令安装chrome的方法

curl https://intoli.com/install-google-chrome.sh | bash

2. 按照https://github.com/wechaty/wechaty-getting-started介绍的步骤安装依赖

git clone https://github.com/wechaty/wechaty-getting-started.git
cd wechaty-getting-started
npm install

3. 将examples/ding-dong-bot.ts改为以下代码

/**
 * Wechaty - WeChat Bot SDK for Personal Account, Powered by TypeScript, Docker, and 💖
 *  - https://github.com/chatie/wechaty
 */
import {
  Contact,
  ScanStatus,
  Wechaty,
  log,
}               from 'wechaty'

import { generate } from 'qrcode-terminal'

require('dotenv').config()

// You can safely ignore / comment out the next two lines because it is using for CodeSandbox
require('./.code-sandbox.js')

const welcome = `[Powered by Wechaty]
发送2020,提示机器人指令:
++2020 本群消息推给对方
==2020 本群消息不推给对方
2020++ 对方消息推到本群
2020== 对方消息不推到本群
FFFF开头 消息强制推给对方 ` const help_cmd
= '2020' const push_msg_cmd = '++2020' const push_nil_cmd = '==2020' const pull_msg_cmd = '2020++' const pull_nil_cmd = '2020==' // [0] is not used const topic_ls = ['', 'XXXX一群', 'XXXX二群'] const abbr_ls = ['', '一群.', '二群.'] const push_to = [false, false, false] function onScan (qrcode: string, status: ScanStatus) { if (status === ScanStatus.Waiting || status === ScanStatus.Timeout) { generate(qrcode, { small: true }) // show qrcode on console const qrcodeImageUrl = [ 'https://wechaty.js.org/qrcode/', encodeURIComponent(qrcode), ].join('') log.info('StarterBot', 'onScan: %s(%s) - %s', ScanStatus[status], status, qrcodeImageUrl) } 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) } const bot = new Wechaty({ name: 'ding-dong-bot', }) bot.on('scan', onScan) bot.on('login', onLogin) bot.on('logout', onLogout) bot.on('message', async msg => {
  log.info('StarterBot', msg.toString())
  if (msg.self() ||
    (msg.type() !== bot.Message.Type.Text &&
     msg.type() !== bot.Message.Type.Url)) {
    return
  }

  const room = msg.room()
  if (!room) {
    return;
  }

  const topic = await room.topic()
  const from_idx = topic_ls.indexOf(topic)
  if (from_idx > -1) {
    const to_idx = from_idx == 1 ? 2 : 1
    const orig_push_to = push_to[to_idx]

    const txt = msg.text()
    if (txt == push_msg_cmd || txt == push_nil_cmd) {
      push_to[to_idx] = (txt == push_msg_cmd)
    } else if (txt == pull_msg_cmd || txt == pull_nil_cmd) {
      push_to[from_idx] = (txt == pull_msg_cmd)
    } else if (txt == help_cmd) {
      await room.say(welcome)
      return
    }

    if (push_to[to_idx] || orig_push_to || txt.substr(0,4) == 'FFFF') {
      const contact = msg.talker()
      const alias = await room.alias(contact) // TODO: cache
      const s = '[' + abbr_ls[from_idx] + (alias ? alias : contact.name()) + ']\n' + txt;
const fwd_room = await bot.Room.find({topic: topic_ls[to_idx]}) // TODO: cache
    if (!fwd_room) {
      return
    }
      await fwd_room.say(s)
    }
  }
})
bot.start() .then(()
=> log.info('StarterBot', 'Starter Bot Started.')) .catch(e => log.error('StarterBot', e))

 

4. 执行以下命令,扫码登录,退出shell,done。

注意:2017年之前注册的微信账号才能登录网页版;有的ssh客户端不能正常显示二维码,putty可以。

nohup npm start >stdout.log 2>stderr.log &
tailf stdout.log

 

官方社区群

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM