出售本站【域名】【外链】

首页 AI人工智能软件 qqAI人工智能 微信AI人工智能 抖音AI人工智能 快手AI人工智能 云控系统 手机AI人工智能

java springBoot实现QQ机器人,定时发送信息,自动回复功能

2022-09-26

1. 筹备一个空皂springBoot名目

(自止百度创立)

2. 引入simple-robot依赖 <properties> <java.version>1.8</java.version> <simbot.version>2.0.3</simbot.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>love.forte.simple-robot</groupId> <artifactId>parent</artifactId> <version>${simbot.version}</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <!--java呆板人框架--> <dependency> <groupId>love.forte.simple-robot</groupId> <artifactId>component-mirai-spring-boot-starter</artifactId> </dependency> 3. 配置application.yml文件,运用ANDROID_PAD和谈可以保持手机QQ和呆板人同时正在线 simbot: core: # 呆板人的QQ账号取暗码(帐号:暗码) bots: 123456:123456 component: mirai: # mirai心跳周期. 过长会招致被效逸器断开连贯. 单位毫秒。 heartbeat-period-millis: 30000 # 每次心跳时等候结因的光阳.一旦心跳超时, 整个网络效逸将会重启 (将泯灭约 1s). 除正正在停行的任务 (如图片上传) 会被中断外, 变乱和插件均不受影响. heartbeat-timeout-millis: 5000 # 心跳失败后的第一次重连前的等候光阳. first-reconnect-delay-millis: 5000 # 重连失败后, 继续检验测验的每次等候光阳 reconnect-period-millis: 5000 # 最多检验测验几多屡次重连 reconnection-retry-times: 2147483647 # 运用和谈类型。注,此值为枚举类 net.mamoe.mirai.utils.BotConfiguration.MiraiProtocol 的元素值, # 可选值为:ANDROID_PHONE、ANDROID_PAD、ANDROID_WATCH protocol: ANDROID_PAD # 能否封锁mirai的bot logger no-bot-log: true # 封锁mirai网络日志 no-network-log: true # mirai bot log切换运用simbot的log use-simbot-bot-log: true # mirai 网络log 切换运用simbot的log use-simbot-network-log: true # mirai配置自界说deviceInfoSeed的时候运用的随机种子。默许为1. device-info-seed: 1 # mirai图片缓存战略,为枚举 love.forte.simbot.component.mirai.configuration.MiraiCacheType 的元素值, # 可选为 FILE、 MEMORY cache-type: MEMORY # 假如配置项 simbot.mirai.cacheType 的值为 FILE,此处为缓存文件的保存目录。为空时默许为系统久时文件夹。 cache-directory: # 登录验证码办理器,当登录须要输入验证码的时候可能会用到。 login-solver-type: DEFAULT # 假如不为空,此处代表指定一个 deviceInfo 的 json文件途径。 dispatcher: # mirai组件中,对变乱停行调治的线程池参数:最大线程数。 core-pool-size: 8 # mirai组件中,对变乱停行调治的线程池参数:最大线程数。 maximum-pool-size: 8 # mirai组件中,对变乱停行调治的线程池参数:线程存活光阳(毫秒) keep-alive-time: 1000 4. 正在springboot启动类加上@EnableSimbot表明 @EnableSimbot @EnableScheduling @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) @Slf4j public class RobotApplication { public static void main(String[] args) { SpringApplication.run(RobotApplication.class, args); log.info("呆板人启动乐成~~~~"); } } 5. 创立一个按时类AutoQQMessage.java类 package com.xiaoxiaoguai.robot.scheduling; import lombok.extern.slf4j.Slf4j; import love.forte.simbot.bot.BotManager; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import java.util.Random; import java.util.Set; /** * @author 小小怪 * @description 按时发送音讯 * @since 2022/09/15 12:11 */ @Component @Slf4j public class AutoQQMessage { @Resource private BotManager botManager; @Value("${bello.qq}") private Set<String> qqSet; /** * 界说语录 */ static List<String> content; /** * 界说图片 */ static List<String> images; static { content = new ArrayList<>(); images = new ArrayList<>(); log.info("初步加载界说语录~~~"); // 界说语录 content.add("逢见你之前,我没想过成亲,逢见你之后,成亲我没想过别人。"); content.add("你走向我,我感觉一日不见如隔三秋,你朝我笑,我又感觉三秋未见不过一日。"); content.add("假如可以和你正在一起,我甘愿让天空所有的星光全副损落,果为你的眼睛,是我生命里最亮的光芒。"); content.add("我接续喜爱暖和的东西,而世界上最暖和的,无非阴光和你。"); content.add("我不要短久的温存,只有你一世的陪同。"); content.add("我没有出格喜爱的零食,没有出格喜爱的影戏,没有出格喜爱听的歌,但我便是出格喜爱你。"); content.add("一年四季想陪你渡过,世间琐事都想取你作,今生也只想同你尝尽炊火。"); content.add("我还是很喜爱你,像七月的风和八月的雨,毫无交加。"); content.add("你正在我身边也好,正在天边也罢,想到世界的角落有一个你,感觉整个世界也变得和顺安定了。"); content.add("我的人生理念是活十成,一成不变的是爱你,剩下九成是加倍爱你。"); log.info("初步加载表情图片~~~"); // 表情图片 images.add("https://gitee.com/xxg-git/file-services/raw/master/QQ-robot/112ab4dd77ba5919bd830a75cf8973c.png"); images.add("https://gitee.com/xxg-git/file-services/raw/master/QQ-robot/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20200623142757.jpg"); images.add("https://gitee.com/xxg-git/file-services/raw/master/QQ-robot/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20201002220953.jpg"); images.add("https://gitee.com/xxg-git/file-services/raw/master/QQ-robot/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20201002221329.jpg"); images.add("https://gitee.com/xxg-git/file-services/raw/master/QQ-robot/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20201002221441.jpg"); images.add("https://gitee.com/xxg-git/file-services/raw/master/QQ-robot/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20201002221545.jpg"); images.add("https://gitee.com/xxg-git/file-services/raw/master/QQ-robot/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20201004212459.jpg"); images.add("https://gitee.com/xxg-git/file-services/raw/master/QQ-robot/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20201004212532.jpg"); } /** * 每一小时发送一次: 0 0 0/1 * * ? * 每五分钟发送一次: 0 0/5 * * * ? */ @Scheduled(cron = "0 0/5 * * * ?") public void handler() { Calendar calendar = Calendar.getInstance(); // 获与当前小时 int hour = calendar.get(Calendar.HOUR_OF_DAY); // 只正在早上9点到早晨8点发送音讯 if (hour < 9 || hour > 20) { return; } if (null != qqSet && qqSet.size() > 0){ qqSet.forEach(qq -> { try { final String msg = content.get(new Random().nextInt(content.size())); final String img = String.format("[CAT:image,url=%s,flash=false]", images.get(new Random().nextInt(content.size()))); // 发送随机界说语录 botManager.getDefaultBot().getSender().SENDER.sendPrivateMsg(qq, msg); // 发送随机界说图片 botManager.getDefaultBot().getSender().SENDER.sendPrivateMsg(qq, img); log.info("正正在发送界说语录,当前qq={}, 语录={}, img={}", qq, msg, img); } catch (Exception e) { log.error("发送界说语录异样, qq={}", qq, e); } }); } } } 6. 正在yml文件中配置承受音讯的QQ号 #配置女神们的QQ号,多个QQ用逗号收解 bello: qq: 11223344,55667788 7. 参预智能聊天罪能-运用青云客的api停行聊天,官网 8. 封拆http工具类  import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.util.EntityUtils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * @author xiaoxiaoguai * @description http工具类 * @since 2022/09/15 12:11 */ public class HttpUtil { /** * 向指定URL发送GET办法的乞求 * * @param url 发送乞求的URL * @param param 乞求参数,乞求参数应当是 name1=value1&name2=value2 的模式。 * @return URL 所代表远程资源的响应结因 */ public static String sendGet(String url, String param) { StringBuilder result = new StringBuilder(); BufferedReader in = null; try { String urlNameString = url; if (!StringUtils.isEmpty(param)) { urlNameString += "?" + param; } URL realUrl = new URL(urlNameString); // 打开和URL之间的连贯 URLConnection connection = realUrl.openConnection(); // 设置通用的乞求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"); // 建设真际的连贯 connection.connect(); // 界说 BufferedReader输入流来读与URL的响应 in = new BufferedReader(new InputStreamReader( connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result.append(line); } } catch (Exception e) { System.out.println("发送GET乞求显现异样!" + e); e.printStackTrace(); } // 运用finally块来封锁输入流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result.toString(); } } 9. 创立音讯监听类,撑持私聊音讯和群音讯的智能聊天 import com.alibaba.fastjson.JSONObject; import com.xiaoxiaoguai.robot.entity.LoveChatDTO; import com.xiaoxiaoguai.robot.util.HttpUtil; import lombok.extern.slf4j.Slf4j; import love.forte.simbot.annotation.OnGroup; import love.forte.simbot.annotation.OnPrivate; import love.forte.simbot.api.message.Reply; import love.forte.simbot.api.message.ReplyAble; import love.forte.simbot.api.message.events.GroupMsg; import love.forte.simbot.api.message.events.MessageGet; import love.forte.simbot.api.message.events.PrivateMsg; import love.forte.simbot.api.sender.MsgSender; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import java.util.List; import java.util.Objects; import java.util.Random; /** * @author 小小怪 * @description 呆板人监听 * @since 2022/09/15 10:15 */ @Component @Slf4j public class MessageListener { static final String URL = ""; /** * 监听私聊音讯 */ @OnPrivate public void privateMsg(PrivateMsg privateMsg, MsgSender sender) { // 智能聊天 sendMsg(privateMsg, sender, false); // 恋情话术获与 // loveChat(privateMsg, sender); } /** * 监听群音讯 */ @OnGroup public ReplyAble groupMsg(GroupMsg groupMsg, MsgSender sender) { // 默许封锁群聊形式,须要的话把注释去掉 return sendMsg(groupMsg, sender, true); // return null; } /** * 恋情话术罪能 * * @param commonMsg commonMsg * @param sender sender */ private void loveChat(MessageGet commonMsg, MsgSender sender) { log.info("恋情话术~~~,接管音讯:qq={}, msg={}", commonMsg.getAccountInfo().getAccountCode(), commonMsg.getMsgContent().getMsg()); // 获与话术 final JSONObject jsonObject = new JSONObject(); jsonObject.put("page", 1); jsonObject.put("count", 5); jsonObject.put("key", commonMsg.getMsgContent().getMsg()); final String resultStr = HttpUtil.post("https://yin-feng.top/open/getLoveChat", jsonObject.toJSONString()); if (StringUtils.isEmpty(resultStr)) { return; } final JSONObject resultObj = JSONObject.parseObject(resultStr); if (!Objects.equals("0", resultObj.getString("code"))) { return; } final List<LoveChatDTO> loveChatList = JSONObject.parseArray(resultObj.getString("data"), LoveChatDTO.class); //发送私聊话术音讯 if (null != loveChatList && loveChatList.size() > 0){ Random random = new Random(); int size = loveChatList.size(); int i = random.nextInt(size); LoveChatDTO loveChatDTO = loveChatList.get(i); String female = loveChatDTO.getFemale(); sender.SENDER.sendPrivateMsg(commonMsg, female); } } /** * 通过青客云封拆智能聊天 * * @param commonMsg commonMsg * @param sender sender */ private ReplyAble sendMsg(MessageGet commonMsg, MsgSender sender, boolean group) { log.info("智能聊天中~~~,接管音讯:qq={}, msg={}", commonMsg.getAccountInfo().getAccountCode(), commonMsg.getMsgContent().getMsg()); // MsgSender中存正在三大送信器,以及很是多的重载办法。 // 通过get乞求挪用聊天接口 final String result = HttpUtil.get(URL.concat("?key=free&appid=0&msg=").concat(commonMsg.getMsgContent().getMsg())); if (!StringUtils.isEmpty(result)) { final JSONObject json = JSONObject.parseObject(result); if (json.getInteger("result") == 0 && !StringUtils.isEmpty(json.getString("content"))) { final String msg = json.getString("content").replace("{br}", "\n"); log.info("智能聊天中~~~,发送音讯:qq={}, msg={}", commonMsg.getAccountInfo().getAccountCode(), msg); //发送群音讯 if (group) { // 参数1:回复的音讯 参数2:能否at当事人 return Reply.reply(msg, true); } //发送私聊音讯 sender.SENDER.sendPrivateMsg(commonMsg, msg); } } return null; } }  10. 成效如图

11.  源码链接 https://gitee.com/xxg-git/xiaoxiaoguai-robot.git

热门文章

随机推荐

推荐文章

友情链接: 永康物流网 本站外链出售 义乌物流网 本网站域名出售 手机靓号-号码网