Giter Club home page Giter Club logo

chatapi-wechat's Introduction

ChatApi-WeChat

Java版本微信聊天接口,使用网页微信API,让你能够开发自己的微信聊天机器人。

这是什么?

  • 这是一个模拟的微信聊天客户端
  • 该客户端使用的接口来自于网页版微信

有何优点?

  • 对接口、流程、实体类进行了封装,更加简单易用
  • 暴露了一个监听器,可以自己实现监听器以开发自己的业务功能
  • 全部功能的支持
    • 监听文字、图像、动图、语音、视频、文件、系统提示等消息
    • 发送文字、图像、动图、文件等消息
    • 撤回发送的消息
    • 同意好友申请(网页微信发送好友申请功能已被关闭)
    • 修改好友备注
    • 置顶/取消置顶联系人
    • 设置群名称
    • (网页微信创建群、添加群成员、移除群成员功能均已被关闭)

测试数据

  • 最后测试可用时间:2019-05-28
  • 最长在线时间:7天
  • 近期平均在线时间:2天

如何使用

  • maven依赖
<dependency>
    <groupId>me.xuxiaoxiao</groupId>
    <artifactId>chatapi-wechat</artifactId>
    <version>1.3.0</version>
</dependency>
  • gradle依赖
implementation 'me.xuxiaoxiao:chatapi-wechat:1.3.0'
  • jar包

点击进入下载页

  • 以下是一个学别人说话的小机器人,用到了该库提供的大部分功能
public class WeChatDemo {
    public static final Gson GSON = new GsonBuilder().disableHtmlEscaping().create();
    
    /**
     * 新建一个微信监听器
     */
    public static final WeChatClient.WeChatListener LISTENER = new WeChatClient.WeChatListener() {
        @Override
        public void onQRCode(@Nonnull WeChatClient client, @Nonnull String qrCode) {
            System.out.println("onQRCode:" + qrCode);
        }
        
        @Override
        public void onLogin(@Nonnull WeChatClient client) {
            System.out.println(String.format("onLogin:您有%d名好友、活跃微信群%d个", client.userFriends().size(), client.userGroups().size()));
        }
        
        @Override
        public void onMessage(@Nonnull WeChatClient client, @Nonnull WXMessage message) {
            System.out.println("获取到消息:" + GSON.toJson(message));
            
            if (message instanceof WXVerify) {
                //是好友请求消息,自动同意好友申请
                client.passVerify((WXVerify) message);
            } else if (message instanceof WXLocation && message.fromUser != null && !message.fromUser.id.equals(client.userMe().id)) {
                // 如果对方告诉我他的位置,发送消息的不是自己,则我也告诉他我的位置
                if (message.fromGroup != null) {
                    // 群消息
                    // client.sendLocation(message.fromGroup, "120.14556", "30.23856", "我在这里", "西湖");
                } else {
                    // 用户消息
                    client.sendLocation(message.fromUser, "120.14556", "30.23856", "我在这里", "西湖");
                }
            } else if (message instanceof WXText && message.fromUser != null && !message.fromUser.id.equals(client.userMe().id)) {
                //是文字消息,并且发送消息的人不是自己,发送相同内容的消息
                if (message.fromGroup != null) {
                    // 群消息
                    // client.sendText(message.fromGroup, message.content);
                } else {
                    // 用户消息
                    client.sendText(message.fromUser, message.content);
                }
            }
        }
        
        @Override
        public void onContact(@Nonnull WeChatClient client, @Nullable WXContact oldContact, @Nullable WXContact newContact) {
            System.out.println(String.format("检测到联系人变更:旧联系人名称:%s:新联系人名称:%s", (oldContact == null ? "null" : oldContact.name), (newContact == null ? "null" : newContact.name)));
        }
    };
    
    public static void main(String[] args) {
        //新建一个模拟微信客户端
        WeChatClient wechatClient = new WeChatClient();
        //为模拟微信客户端设置监听器
        wechatClient.setListener(LISTENER);
        //启动模拟微信客户端
        wechatClient.startup();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            try {
                System.out.println("请输入指令");
                switch (scanner.nextLine()) {
                    case "sendText": {
                        System.out.println("toContactId:");
                        String toContactId = scanner.nextLine();
                        System.out.println("textContent:");
                        String text = scanner.nextLine();
                        WXContact contact = wechatClient.userContact(toContactId);
                        if (contact != null) {
                            System.out.println("success:" + GSON.toJson(wechatClient.sendText(contact, text)));
                        } else {
                            System.out.println("联系人未找到");
                        }
                    }
                    break;
                    case "sendFile": {
                        System.out.println("toContactId:");
                        String toContactId = scanner.nextLine();
                        System.out.println("filePath:");
                        File file = new File(scanner.nextLine());
                        WXContact contact = wechatClient.userContact(toContactId);
                        if (contact != null) {
                            System.out.println("success:" + GSON.toJson(wechatClient.sendFile(contact, file)));
                        } else {
                            System.out.println("联系人未找到");
                        }
                    }
                    break;
                    case "sendLocation": {
                        System.out.println("toContactId:");
                        String toContactId = scanner.nextLine();
                        System.out.println("longitude:");
                        String longitude = scanner.nextLine();
                        System.out.println("latitude:");
                        String latitude = scanner.nextLine();
                        System.out.println("title:");
                        String title = scanner.nextLine();
                        System.out.println("lable:");
                        String lable = scanner.nextLine();
                        WXContact contact = wechatClient.userContact(toContactId);
                        if (contact != null) {
                            System.out.println("success:" + GSON.toJson(wechatClient.sendLocation(contact, longitude, latitude, title, lable)));
                        } else {
                            System.out.println("联系人未找到");
                        }
                    }
                    break;
                    case "revokeMsg": {
                        System.out.println("toContactId:");
                        String toContactId = scanner.nextLine();
                        System.out.println("clientMsgId:");
                        String clientMsgId = scanner.nextLine();
                        System.out.println("serverMsgId:");
                        String serverMsgId = scanner.nextLine();
                        WXUnknown wxUnknown = new WXUnknown();
                        wxUnknown.id = Long.valueOf(serverMsgId);
                        wxUnknown.idLocal = Long.valueOf(clientMsgId);
                        wxUnknown.toContact = wechatClient.userContact(toContactId);
                        wechatClient.revokeMsg(wxUnknown);
                    }
                    break;
                    case "passVerify": {
                        System.out.println("userId:");
                        String userId = scanner.nextLine();
                        System.out.println("verifyTicket:");
                        String verifyTicket = scanner.nextLine();
                        WXVerify wxVerify = new WXVerify();
                        wxVerify.userId = userId;
                        wxVerify.ticket = verifyTicket;
                        wechatClient.passVerify(wxVerify);
                    }
                    break;
                    case "editRemark": {
                        System.out.println("userId:");
                        String userId = scanner.nextLine();
                        System.out.println("remarkName:");
                        String remark = scanner.nextLine();
                        WXContact contact = wechatClient.userContact(userId);
                        if (contact instanceof WXUser) {
                            wechatClient.editRemark((WXUser) contact, remark);
                        } else {
                            System.out.println("好友未找到");
                        }
                    }
                    break;
                    case "topContact": {
                        System.out.println("contactId:");
                        String contactId = scanner.nextLine();
                        System.out.println("isTop:");
                        String isTop = scanner.nextLine();
                        WXContact contact = wechatClient.userContact(contactId);
                        if (contact != null) {
                            wechatClient.topContact(contact, Boolean.valueOf(isTop.toLowerCase()));
                        } else {
                            System.out.println("联系人未找到");
                        }
                    }
                    break;
                    case "setGroupName": {
                        System.out.println("groupId:");
                        String groupId = scanner.nextLine();
                        System.out.println("name:");
                        String name = scanner.nextLine();
                        WXGroup group = wechatClient.userGroup(groupId);
                        if (group != null) {
                            wechatClient.setGroupName(group, name);
                        } else {
                            System.out.println("群组未找到");
                        }
                    }
                    break;
                    case "quit": {
                        System.out.println("logging out");
                        wechatClient.shutdown();
                    }
                    return;
                    default: {
                        System.out.println("未知指令");
                    }
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.