基于simple-robot框架写的qq自动摸鱼功能:java

然后的话,里面需要注册一些api,截止到发帖前这些接口都是免费的,一天可以获取个一百次,个人用还是够够的。

注意:因为我太马虎,不小心把源码丢在了上家公司,所以导致现在有一个GroupConfig文件找不到了。groupconfig只有一些群号和链接的配置,可能要猜着写一下。猜不到来问我吧

大概效果就是这样

9点到18点每四小时发送一次新闻

 然后每天发送一次今年最近节假日的信息,和一条语录

首先你要申请一个聚合的万年历 万年历 数据接口_免费API接口调用-聚合数据聚合数据,专业的API数据平台,为您提供万年历数据接口以及调用信息,无期限免费试用再付费升级。https://www.juhe.cn/docs/api/id/177

 还有天行的朋友圈api

朋友圈文案API接口 - 天行数据TianAPI朋友圈文案,简单走心的文案句子。接口稳定高效、简单易用,支持免费试用再付费,欢迎使用。https://www.tianapi.com/apiview/194和新闻的api

今日头条新闻API接口 - 天行数据TianAPI今日头条新闻,今日头条热门新闻列表接口。接口稳定高效、简单易用,支持免费试用再付费,欢迎使用。https://www.tianapi.com/apiview/99

然后把代码中提到的key替换为你的key

 然后我的架构是这样的

GroupConfig:配置了发送的群聊号码

GetMoyuMess:发送节假日信息

GetNewsMess:4小时发送新闻

HttpUtil:发送网络请求以及转化请求为map

MySimotApp:启动类

simbot:配置类

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.qqmy</groupId>
    <artifactId>qqmy</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <simbot.version>2.0.3</simbot.version>
        <java.version>1.8</java.version>
        <encoding>UTF-8</encoding>

    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- 通过parent进行版本控制。当然,也可以直接放到 parent 标签中。 -->
            <dependency>
                <groupId>love.forte.simple-robot</groupId>
                <artifactId>parent</artifactId>
                <version>${simbot.version}</version>
                <type>pom</type>
                <scope>compile</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- mirai组件依赖。如果使用版本控制,则不需要填写version。 -->
        <dependency>
            <groupId>love.forte.simple-robot</groupId>
            <artifactId>component-mirai</artifactId>
            <version>2.0.3</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.1.RELEASE</version>
                <configuration>
                    <!-- main方法所在类。 -->
                    <mainClass>com.qqmy.MySimbotApp</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>



</project>

simbot.yml

simbot:
  core:
    # 账号:密码,多个用逗号(,)分隔或者用yaml的多项配置
    # bots: 账号:密码
    bots: 你的qq:密码
  component:
    mirai:
     protocol: ANDROID_PAD

MySimotApp


/**
 * @author Created by ywz on 2021/9/8 11:15
 * @description
 */
@SimbotApplication(@SimbotResource("simbot.yml"))
public class MySimbotApp implements SimbotProcess {

    public static void main(String[] args) {
        SimbotApp.run(MySimbotApp.class, args);
    }

    @Override
    public void post(@NotNull SimbotContext context) {
        Bot defaultBot = context.getBotManager().getDefaultBot();
        BotSender sender = defaultBot.getSender();
        setScheduled(sender);

    }

    @Override
    public void pre(@NotNull Configuration config) {

    }

    private void setScheduled(BotSender sender) {
        ScheduledExecutorService service = Executors.newScheduledThreadPool(2);
        //每天9点自动发送距离周末的日期
        service.scheduleAtFixedRate(() -> {
            System.out.println("开始自动发送消息");
            GetMoyuMess.sendmoyu(sender);
            System.out.println("发送消息结束");

        }, (24 + 9 - new Date().getHours()) % 24, 24 , TimeUnit.HOURS);
        //在9点到晚上6点,每四个小时发送一次新闻
        service.scheduleAtFixedRate(() -> {
            System.out.println("开始推送新闻");
            Date date = new Date();
            if (date.getHours() < 9 || date.getHours() > 18 ) {
                return;
            }
            GetNewsMess.sendnew(sender);
            System.out.println("推送新闻完成");
        }, 0, 4, TimeUnit.HOURS);

    }

}

GetMoyuMess

    /**
     * 发送每天摸鱼消息的方法
     *
     * */
    public static void sendmoyu(BotSender sender) {
        DateFormat yy = new SimpleDateFormat("yyyy");
        String format = yy.format(new Date());
        String result = getjsonByyear((Integer.parseInt(format)));
//        String resultn = getjsonByyear((Integer.parseInt(format) + 1));
        List theyearday = jsontoList(result);
//        List nextyearday = jsontoList(resultn);
        StringBuffer talk = new StringBuffer();
        addtalk(talk);
        appendday(theyearday, talk, "今年");
        isweekkend(talk);
//        appendday(nextyearday,talk,"明年");
        talk.append("工作再忙,也要抽时间摸鱼哦");
        sender.SENDER.sendGroupMsg(GroupConfig.DATE_GROUP, talk.toString());

    }

    private static void addtalk(StringBuffer talk) {
        String result = HttpUtil.getjsonByurl("http://api.tianapi.com/txapi/pyqwenan/index" + "?key=你申请的key");
        jsonaddappend(talk, result);
    }

    private static void isweekkend(StringBuffer talk) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if(week_index<0){
            week_index = 0;
        }
        if (week_index > 0 && week_index < 6) {
            talk.append("今天距离周末还有" + (6 - week_index) + "天\n");
        } else {
            talk.append("今天就是周末哦\n");

        }
    }

    private static String getjsonByyear(int year) {
        return HttpUtil.getjsonByurl("http://v.juhe.cn/calendar/year" + "?year=" + year + "&key=你申请的key");
    }

    private static List jsontoList(String result) {
        Gson gson = new Gson();
        Map map = gson.fromJson(result.toString(), Map.class);
        List o = (List) ((Map) ((Map) map.get("result"))
                .get("data"))
                .get("holiday_list");
        return o;
    }

    private static void jsonaddappend(StringBuffer sb, String json) {
        Gson gson = new Gson();
        Map map = gson.fromJson(json.toString(), Map.class);
        ((List) map.get("newslist")).forEach((m) -> {
            Map saymap = (Map) m;
            sb.append(saymap.get("content") + "\n来自:" + saymap.get("source") + "\n");
        });
    }

    private static void appendday(List o, StringBuffer talk, String s) {
        Date now = new Date();
        long nowtime = now.getTime();
        o.forEach(m -> {
            Map m1 = (Map) m;
            try {
                DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
                Date startday = format1.parse(m1.get("startday").toString());
                long happyday = startday.getTime();
                if (nowtime > happyday) {
                    return;
                } else {
                    int day = Integer.parseInt((happyday - nowtime) / (1000 * 60 * 60 * 24) + "");
                    talk.append("今天距离" + s + m1.get("name") + "还有" + (day + 1) + "天\n");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }


GetNewsMess


/**
 * @author Created by ywz on 2021/9/13 11:16
 * @description
 */
public class GetNewsMess {

    public static void sendnew(BotSender sender) {
//        getnewjson();/**/
        Map newmap = getnewmap();
        String msg = buildMsg(newmap);
        System.out.println("消息为" + msg);
        sender.SENDER.sendGroupMsg(GroupConfig.NEW_GROUP,msg);
    }

    private static String buildMsg(Map newMap) {
        StringBuffer sb = new StringBuffer();
        sb.append(newMap.get("title") + "\n");
        sb.append(newMap.get("description") + "\n");
      /*  CatCodeUtil instance = CatCodeUtil.getInstance();
        String image = instance.toCat("image", true, "url=" + newMap.get("picUrl"));
        sb.append(image + "\n");
      */
        sb.append("消息原文:" + newMap.get("url"));
        return sb.toString();
    }

    private static Map getnewmap() {
        String result = HttpUtil.getjsonByurl("http://api.tianapi.com/topnews/index?key=你申请的key&num=1");
        Map map = HttpUtil.jsontoMap(result);
        Map newmap = (Map) (HttpUtil.objtoList(map.get("newslist")).get(0));
        return newmap;
    }
}

HttpUtil


/**
 * @author Created by ywz on 2021/9/13 11:02
 * @description
 */
public class HttpUtil {
    public static String getjsonByurl(String url) {
        try {
            URL urld = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) urld.openConnection();

            connection.setDoOutput(true); // 设置该连接是可以输出的
            connection.setRequestMethod("GET"); // 设置请求方式
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
            String line = null;
            StringBuilder result = new StringBuilder();
            while ((line = br.readLine()) != null) { // 读取数据
                result.append(line + "\n");
            }
            connection.disconnect();
            return result.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static Map jsontoMap(String json) {
        Gson gson = new Gson();
        Map map = gson.fromJson(json, Map.class);
        return map;
    }
    public static List objtoList(Object obj) {
        return (List) obj;
    }
    public static Map objtoMap(Object obj) {
        return (Map) obj;
    }

}

 最后这是simple-robot的官方文档

simpler-robot BOT开发手册 · 语雀simple-robot 2.x版本的bot开发手册,...https://www.yuque.com/simpler-robot/simpler-robot-doc

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没有名字。。。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值