Wechaty bot in Java to sync group messages

本文介绍如何使用Java部署微信机器人,并实现不同群聊间的消息同步功能。文章涵盖设置TokenGate服务器、启动Java应用及常见问题解决方法等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In the previous blog, we have deployed wechaty bot with node.js to aws. This blog is about to develop wechaty bot with Java and implement the function of sync messages in between different groups.

Prerequisites

Setup Token Gate Server

  1. Follow the official wechaty instruction here
  2. Make sure your localhost and port 8788 is accessible from public. If you are connecting internet with home network router, you will need to set up port forwarding for port 8788 to your machine. And if you are using AWS EC2 as server, you need to config security group to allow inbound traffic to port 8788
  3. To test the Wechaty Token Gate Server is working, Token Test Site shall return your public IP and port 8788
{
"host":"xxx.xxx.xxx.xxx",
"port":8788
}

Start Java Application

Java-wechaty is a good place to start the hello world application. The ding-dong bot example is in the example folder.

  1. Checkout the code.

  2. In the example folder, build the executable by mvn clean install

  3. Run the executable jar by java -jar target/wechaty-example-1.0.0-SNAPSHOT-jar-with-dependencies.jar

Common issues

Account was disabled / locked by Wechat

Solutions:

  • It is a high risk to use a newly registered wechat account. Advice to use an account registerred more than half a year.
  • Login account but DO NOTHING to a few days. Then try again.
  • Related Issues in github issue 1issue 2
The bot application server is not stable. Restart or logout by itself, usually in the late night

Solutions:

  • Monitor the offline pattern(i.e. always offline around 4am). Use cron job to stop and start application. As long as token hostie gateway docker server does not restart, scan is not required when restarting the application.
  • Periodical send message from and to the bot in a random internal.
Cannot get the list of rooms after the token hostie gateway docker server restart

Solutions:

  • Not a good solution yet. Sending a message in each group will register the group to wechaty bot again after server restarted.

Group message sync feature in Java project: Java wechaty bot

Check message if not from bot itself

if (message.self()) { // skip message from self, also to avoid infinite loop
                    logger.info("message from self");
                    return;
                }

Define the rooms to sync messages

MessageRoute route1 = new MessageRoute("测试区危险", "测试区不危险");
List<MessageRoute> routes = new ArrayList<>();

Check if message if from the room defined in the routes, send message to the other room in the route

routes.stream()
                .filter(messageRoute -> messageRoute.getSourceName().equals(getTopicByRoom(room)))
                .forEach(messageRoute -> {
                    Room destRoom = getRoomByTopic(wechaty, messageRoute.getDestinationName());
                    destRoom.say(String.format("[%s in %s]:%n%s", from.name(), getTopicByRoom(room), text));
                });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值