接上一篇 通义千问AI模型对接飞书机器人-模型配置(2-1)
1、通过飞书机器人对接ai的在线接口
参考文档:发送 HTTP 请求
1.1 创建飞书应用
创建流程
配置http请求
http请求地址上一篇百炼平台配置的应用地址
1.2 企业自建应用对接AI
添加应用能力
添加权限
添加机器人权限:
im:message
im:message.group_at_msg
im:message.group_at_msg:readonly
im:message.p2p_msg
im:message.p2p_msg:readonly
im:message:send_as_bot
事件订阅
消息监听事件
代码参考
/*** 用户请求入口* @param request* @param response* @throws Throwable*/
@RequestMapping(value = "/ask/fileDownload")public void ask(HttpServletRequest request, HttpServletResponse response) throws Throwable {//3.1 回调扩展包提供的事件回调处理器
//监听消息 ->请求ai->回复消息servletAdapter.handleEvent(request, response, EVENT_DISPATCHER);}//1. 注册消息处理器
private final EventDispatcher EVENT_DISPATCHER = EventDispatcher.newBuilder("","").onP2MessageReceiveV1(new ImService.P2MessageReceiveV1Handler() {@Overridepublic void handle(P2MessageReceiveV1 event) throws Exception {log.info("P2MessageReceiveV1={}",Jsons.DEFAULT.toJson(event));String content = event.getEvent().getMessage().getContent();String text = JSONUtil.parseObj(content).getStr("text");log.info("text={}",text);// 获取租户 keyString tenantKey = event.getTenantKey();String openId = event.getEvent().getSender().getSenderId().getOpenId();aiAssistantService.answer(text,tenantKey,openId);log.info("requestId={}",event.getRequestId());}}).onP2UserCreatedV3(new ContactService.P2UserCreatedV3Handler() {@Overridepublic void handle(P2UserCreatedV3 event) {log.info("P2UserCreatedV3={}",Jsons.DEFAULT.toJson(event));log.info(event.getRequestId());}}).onP2MessageReadV1(new ImService.P2MessageReadV1Handler() {@Overridepublic void handle(P2MessageReadV1 event) {log.info("P2MessageReadV1={}",Jsons.DEFAULT.toJson(event));System.out.println(event.getRequestId());}}).onP1MessageReadV1(new ImService.P1MessageReadV1Handler() {@Overridepublic void handle(P1MessageReadV1 event) {log.info("P1MessageReadV1={}",Jsons.DEFAULT.toJson(event));System.out.println(event.getRequestId());}}).build();//2. 注入 ServletAdapter 实例
@Autowired
private ServletAdapter servletAdapter;//service类的方法@Asyncpublic void answer(String askStr,String tenantKey,String openId) throws Exception {//通过openId 获取 sessionIdString sessionId = map.get(openId);log.info("askStr start={}",askStr);ApplicationParam param = ApplicationParam.builder().apiKey("sk-*************").appId("*******************").prompt(askStr).sessionId(sessionId).build();log.info("param={}",JSONUtil.toJsonStr(param));Application application = new Application();ApplicationResult result = application.call(param);if(result.getOutput().getSessionId()!=null){map.put(openId,result.getOutput().getSessionId());}String text = result.getOutput().getText();
//飞书应用的秘钥,用户回复信息Client client = Client.newBuilder("cli_*******","j***********").build();Map<String, String> map = new HashMap<>();map.put("text",text);log.info("resp={}",JSONUtil.toJsonStr(map));// 发送请求,回复提问client.im().message().create(CreateMessageReq.newBuilder().receiveIdType(ReceiveIdTypeEnum.OPEN_ID).createMessageReqBody(CreateMessageReqBody.newBuilder().content(JSONUtil.toJsonStr(map)).msgType(MsgTypeEnum.TEXT.getValue()).receiveId(openId).build()).build(), RequestOptions.newBuilder().tenantKey(tenantKey).build());log.info("askStr end ={}",askStr);}