Java代码实现向微信发送消息(通过智能微秘书-需付费)

        不是广告,如果只是想往微信发个消息个人不推荐购买

智能微秘书-智能机器人管理平台智能机器人配置管理平台,一键接入ChatGPT对话,无缝适配Dify和FastGPT知识库!支持群组,个人定义不同的角色,灵活配置各种对话模式,技能丰富多样,拥有各种定时任务。一键接入公众号,企业微信,Gitter,Lark,Whatsapp,5G消息等Wechaty支持的协议icon-default.png?t=N7T8https://wechat.aibotk.com/?r=Bap705

1.注册智能微秘书账号

        注册之后需要配置一下基础配置,天行api直接点申请地址注册获取

Java代码实现向微信发送消息(通过智能微秘书-需付费)_第1张图片

Java代码实现向微信发送消息(通过智能微秘书-需付费)_第2张图片 2.后端代码(jdk1.8)

        导入依赖


        com.squareup.okhttp3
        okhttp
        4.9.0

        创建调用api发送消息的方法类

package com.wxsecretary;

import okhttp3.*;
import org.springframework.stereotype.Service;

/**
 * @author : luobei
 * @date : 2024/2/26 14:16
 */
@Service
public class WxService {
    public void sendMessage(){
        String apiKey = "密钥";
        String roomName = "群名字";
        String content = "文字内容";

        OkHttpClient client = new OkHttpClient();

        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "{\"apiKey\":\"" + apiKey + "\",\"roomName\":\"" + roomName + "\",\"message\":{\"type\":1,\"content\":\"" + content + "\"}}");
        Request request = new Request.Builder()
                .url("https://api-bot.aibotk.com/openapi/v1/chat/room")
                .post(body)
                .addHeader("Content-Type", "application/json")
                .build();

        try {
            Response response = client.newCall(request).execute();
            System.out.println(response.body().string());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sendToFriend(){
        String apiKey = "密钥";
        String name = "群名字";
        String content = "文字内容";

        OkHttpClient client = new OkHttpClient();

        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "{\"apiKey\":\"" + apiKey + "\",\"name\":\"" + name + "\",\"message\":{\"type\":1,\"content\":\"" + content + "\"}}");
        Request request = new Request.Builder()
                .url("https://api-bot.aibotk.com/openapi/v1/chat/contact")
                .post(body)
                .addHeader("Content-Type", "application/json")
                .build();

        try {
            Response response = client.newCall(request).execute();
            System.out.println(response.body().string());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 密钥在智能微秘书的个人中心查看

你可能感兴趣的:(skill,微信)