微信個(gè)人號(hào)實(shí)現(xiàn)微信好友和微信群自動(dòng)發(fā)送消息

實(shí)現(xiàn)思路,利用微信網(wǎng)頁版API,登陸微信,獲取好友和群組信息,調(diào)用微信web端API發(fā)送消息

1、安裝lombok

在本地開發(fā)環(huán)境安裝 lombok 插件并確保你的 Java 環(huán)境是 1.7+

       <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
            <scope>provided</scope>
        </dependency>

2、添加依賴

<dependency>
    <groupId>io.github.biezhi</groupId>
    <artifactId>wechat-api</artifactId>
    <version>1.0.6</version>
</dependency>

該依賴中包含了日志組件,默認(rèn)是 logback,如果你的系統(tǒng)中需要其他的日志組件,請(qǐng)先排除 logback

<dependency>
    <groupId>io.github.biezhi</groupId>
    <artifactId>wechat-api</artifactId>
    <version>1.0.6</version>
    <exclusions>
        <exclusion>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </exclusion>
    </exclusions>
</dependency>

3 創(chuàng)建微信機(jī)器人

機(jī)器人 WeChatBot 對(duì)象可被理解為一個(gè) Web 微信客戶端。創(chuàng)建一個(gè) Java 類作為我們的機(jī)器人,比如 HelloBot

package com.topcom.cms.spider.weixin;

import io.github.biezhi.wechat.WeChatBot;
import io.github.biezhi.wechat.api.constant.Config;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class HelloBot extends WeChatBot {

//登陸二維保存路徑
    private static String assetsDir = "C:/QRCodePath/";

    private volatile static HelloBot helloBot;

    public static void setAssetsDir(String assetsDir) {
        HelloBot.assetsDir = assetsDir;
    }

    public static HelloBot getInstance(){
        if(helloBot == null){
            synchronized (HelloBot.class){
               if(helloBot ==null){
                   helloBot = new HelloBot(Config.me().autoLogin(true).assetsDir(assetsDir).showTerminal(true));
               }
            }
        }
        return helloBot;
    }
    private HelloBot(Config config) {
        super(config);
    }

    public static void main(String[] args) {
        getInstance().start();
    }
}

其他WeChatBot操作可以參考微信個(gè)人號(hào)API
https://biezhi.github.io/wechat-api/#/

package com.topcom.cms.spider.weixin;

import com.topcom.cms.spider.core.config.SpiderConfigAware;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * @Author shaol
 * @Date 2019年1月24日
 */
@Component
public class WeixinBoot implements CommandLineRunner, SpiderConfigAware {
    @Override
    public void run(String... args) throws Exception {
        HelloBot.setAssetsDir(spiderConfig.getWechatCode());
        HelloBot.getInstance().start();
    }

/**
     * 根據(jù)好友的昵稱
     * @param nickName 好友昵稱
     * @param msg 發(fā)送消息
     */
    public Boolean sendMsg(String nickName, String msg) {
        HelloBot helloBot = HelloBot.getInstance();
        if (null != helloBot) {
            String fromUserName = helloBot.api().getAccountByName(nickName).getUserName();
            return helloBot.sendMsg(fromUserName, msg);
        }
        return false;
    }
}

注:用戶的nickname可以重復(fù),UserName不會(huì)重復(fù),但是每次登陸后UserName會(huì)變化,可以用在每次登陸后保存UserName,調(diào)用helloBot.api().getAccountById來獲取用戶信息。

參考文檔:

https://biezhi.github.io/wechat-api/
https://github.com/biezhi/wechat-api
感謝好友LI木水推薦的微信個(gè)人API
http://www.itdecent.cn/u/14313cc03bd0

?著作權(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),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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