Browse Source

升级server sdk,去掉tuling自动回复,添加机器人回复功能(需要IM服务和客户端都升级)

heavyrian2012 1 month ago
parent
commit
8eea0bbe3b

+ 0 - 4
config/robot.properties

@@ -7,9 +7,5 @@ robot.im_url=http://localhost
 # 注意secret是机器人的secret,不是admin的secret
 robot.im_secret=123456
 
-# 图灵机器人api
-robot.use_tuling=true
-robot.tuling_key=a09a38b7da2f4f058b3b07972b505ec4
-
 # 当前服务的公网地址,用来生成webhook地址。
 robot.public_addr=http://192.168.3.101:8883

+ 1 - 1
pom.xml

@@ -22,7 +22,7 @@
 		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 		<java.version>1.8</java.version>
-		<wfc.sdk.version>1.2.1</wfc.sdk.version>
+		<wfc.sdk.version>1.3.9</wfc.sdk.version>
 		<netty.version>4.1.12.Final</netty.version>
 		<log4j2.version>2.17.1</log4j2.version>
 	</properties>

BIN
src/lib/common-1.2.1.jar


BIN
src/lib/common-1.3.9.jar


BIN
src/lib/sdk-1.2.1.jar


BIN
src/lib/sdk-1.3.9.jar


+ 0 - 20
src/main/java/cn/wildfirechat/app/RobotConfig.java

@@ -13,28 +13,8 @@ public class RobotConfig {
     public String im_id;
     public String im_name;
 
-    public boolean use_tuling;
-
-    public String tuling_key;
-
     public String public_addr;
 
-    public boolean isUse_tuling() {
-        return use_tuling;
-    }
-
-    public void setUse_tuling(boolean use_tuling) {
-        this.use_tuling = use_tuling;
-    }
-
-    public String getTuling_key() {
-        return tuling_key;
-    }
-
-    public void setTuling_key(String tuling_key) {
-        this.tuling_key = tuling_key;
-    }
-
     public String getIm_name() {
         return im_name;
     }

+ 41 - 66
src/main/java/cn/wildfirechat/app/ServiceImpl.java

@@ -1,22 +1,21 @@
 package cn.wildfirechat.app;
 
-import cn.wildfirechat.app.tuling.TulingService;
 import cn.wildfirechat.app.webhook.WebhookService;
 import cn.wildfirechat.common.ErrorCode;
-import cn.wildfirechat.messagecontentbuilder.StreamingTextMessageContentBuilder;
 import cn.wildfirechat.pojos.*;
 import cn.wildfirechat.proto.ProtoConstants;
 import cn.wildfirechat.sdk.MessageAdmin;
 import cn.wildfirechat.sdk.RobotService;
+import cn.wildfirechat.sdk.messagecontent.*;
 import cn.wildfirechat.sdk.model.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Async;
-import sun.misc.BASE64Encoder;
 
 import javax.annotation.PostConstruct;
 import java.util.Arrays;
+import java.util.Base64;
 import java.util.UUID;
 
 @org.springframework.stereotype.Service
@@ -29,9 +28,6 @@ public class ServiceImpl implements Service {
     @Autowired
     private WebhookService webhookService;
 
-    @Autowired
-    private TulingService tulingService;
-
     private RobotService robotService;
 
     @PostConstruct
@@ -64,42 +60,30 @@ public class ServiceImpl implements Service {
                 }
             }
         }
-//        public interface ContentType {
-//            int Unknown = 0;
-//            int Text = 1;
-//            int Image = 2;
-//            int Voice = 3;
-//            int Location = 4;
-//            int Video = 5;
-//            int RichMedia = 6;
-//        }
+
         if (needResponse) {
+            boolean replyMessage = messageData.getPayload().getMentionedTarget() != null && messageData.getPayload().getMentionedTarget().contains(mRobotConfig.getIm_id());
+
             Conversation conversation = messageData.getConv();
             if (conversation.getType() == 0) {
                 conversation.setTarget(messageData.getSender());
             }
 
             String response = messageData.getPayload().getSearchableContent();
-            boolean localResponse = true;
-
             LOG.info("msg:{}, robotName:{}", response, mRobotConfig.im_name);
             response = response.replace("@" + mRobotConfig.im_name, "").trim();
             LOG.info("msg:{}, robotName:{}", response, mRobotConfig.im_name);
             if (messageData.getPayload().getType() == 1) {
+                TextMessageContent textMessageContent = (TextMessageContent)MessageContentFactory.decodeMessageContent(messageData.getPayload());
                 if(response.contains("地址") || response.contains("文档") || response.contains("论坛") || response.contains("官网")) {
-                    localResponse = false;
                     response = "项目地址在: https://github.com/wildfirechat。 文档地址在: https://docs.wildfirechat.cn。论坛地址在: https://bbs.wildfirechat.cn。官网地址在:https://wildfirechat.cn";
                 } else if(response.startsWith("公众号")) {
-                    localResponse = false;
                     response = "请关注微信公众号:野火IM";
                 } else if(response.startsWith("价格") || response.startsWith("收费") || response.startsWith("费用")) {
-                    localResponse = false;
                     response = "野火IM相关价格,请参考我们的文档: http://docs.wildfirechat.cn";
                 } else if(response.startsWith("商务") || response.startsWith("购买") || response.startsWith("联系")) {
-                    localResponse = false;
                     response = "请微信联系 wildfirechat 或 wfchat 进行商务交流";
                 } else if(response.startsWith("问题") || response.startsWith("崩溃")) {
-                    localResponse = false;
                     response = "请确保是按照文档进行对接使用。请检索github issue或者论坛bbs.wildfirechat.cn。如果还无法解决请提issue或者论坛发帖";
                 } else if(response.equalsIgnoreCase("/list") || response.equalsIgnoreCase("/")) {
                     if (conversation.getType() == 1 || conversation.getType() == 0) {
@@ -108,34 +92,37 @@ public class ServiceImpl implements Service {
                         response = "仅支持群组和私聊";
                     }
                 } else if(response.equals("流式文本")) {
-                    testStreamingText(conversation);
+                    String fullText = "北京野火无限网络科技有限公司是成立于2019年底的一家科技创新企业,公司的主要目标是为广大企业和单位提供优质可控、私有部署的即时通讯和实时音视频能力,为社会信息化水平提高作出自己的贡献。\n" +
+                            "\n" +
+                            "野火IM是公司研发一套自主可控的即时通讯组件,具有全部私有化、功能齐全、协议稳定可靠、全平台支持、安全性高和支持国产化等技术特点。客户端分层设计,既可开箱即用,也可与现有系统深度融合。具有完善的服务端API和自定义消息功能,可以任意扩展功能。代码开源率高,方便二次开发和使用。支持多人实时音视频和会议功能,线上沟通更通畅。\n" +
+                            "\n" +
+                            "公司致力于开源项目,在Github上开源项目广受好评,其中Server项目有超过7.1K个Star,组织合计Star超过1万个。有大量的技术公司受益于我们的开源,为自己的产品添加了即时通讯能力,这也算是我们公司为社会信息化建设做出的一点点贡献吧。\n" +
+                            "\n" +
+                            "公司以即时通讯技术为核心,持续努力优化和完善即时通讯和实时音视频产品,努力为客户提供最优质的即时通讯和实时音视频能力。";
+                    testStreamingText(replyMessage? messageData.getMessageId() : 0, conversation, fullText);
                     return;
                 } else if(webhookService.handleInvokeCommand(response, messageData.getSender(), messageData.getConv())) {
                     return;
                 } else {
-                    response = tulingService.handleWord(messageData.getSender(), response);
+                    String fullText = "我收到了你的消息内容:" + textMessageContent.getText() + " 我很想回答你,但我还没有接入AI功能,没有办法回答你,非常抱歉!";
+                    testStreamingText(replyMessage? messageData.getMessageId() : 0,conversation, fullText);
+                    return;
                 }
             } else if (messageData.getPayload().getType() == 3) {
-                localResponse = false;
                 response = "不好意思,我还不会看照片哟~";
             } else if (messageData.getPayload().getType() == 2) {
-                localResponse = false;
                 response = "不好意思,我还不会听声音哟~";
             } else if (messageData.getPayload().getType() == 4) {
-                localResponse = false;
                 response = "这是那里?我还没有学会看地图啊!";
             } else if (messageData.getPayload().getType() == 6) {
-                localResponse = false;
                 response = "我也想看视频,可惜我还没学会!";
             } else if (messageData.getPayload().getType() == 400) {
-                localResponse = false;
                 response = "别给我打电话了,我是个机器人,还不会接电话,还没有人教过我啊!";
-
                 MessagePayload payload = new MessagePayload();
                 payload.setType(402);
                 payload.setContent(messageData.getPayload().getContent());
                 String reason = "{\"r\":5}";
-                payload.setBase64edData(new BASE64Encoder().encode(reason.getBytes()));
+                payload.setBase64edData(Base64.getEncoder().encodeToString(reason.getBytes()));
                 try {
                     robotService.sendMessage(mRobotConfig.getIm_id(), conversation, payload);
                 } catch (Exception e) {
@@ -145,27 +132,7 @@ public class ServiceImpl implements Service {
                 //voip signal message, ignore it
                 return;
             }
-
-            if(localResponse) {
-                if (messageData.getPayload().getType() != 1) {
-                    response = "我不明白你在说什么";
-                } else {
-                    response = response.replace("@" + mRobotConfig.getIm_name() + " ", "");
-                    response = response.replace("@" + mRobotConfig.getIm_name(), "");
-                    response = response.replace("我", "");
-                    response = response.replace("吗", "");
-                    response = response.replace("你是谁", "我是" + mRobotConfig.getIm_name() + "呀");
-                    response = response.replace("你", "我");
-                    response = response.replace("?", "!");
-                    response = response.replace("? ", "!");
-                    response = response.replace("?", "!");
-                    response = response.replace("? ", "!");
-                    if (response.equals(messageData.getPayload().getSearchableContent())) {
-                        response = "我不明白你在说什么";
-                    }
-                }
-            }
-
+            
             MessagePayload payload = new MessagePayload();
             payload.setType(1);
             payload.setSearchableContent(response);
@@ -186,7 +153,12 @@ public class ServiceImpl implements Service {
                 }
             }
             try {
-                IMResult<SendMessageResult> result = robotService.sendMessage(mRobotConfig.getIm_id(), conversation, payload);
+                IMResult<SendMessageResult> result;
+                if(replyMessage) {
+                    result = robotService.replyMessage(messageData.getMessageId(), payload, false);
+                } else {
+                    result = robotService.sendMessage(mRobotConfig.getIm_id(), conversation, payload);
+                }
                 if (result != null) {
                     if (result.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
                         LOG.info("Send response success");
@@ -201,30 +173,33 @@ public class ServiceImpl implements Service {
                 LOG.error("Send response execption");
             }
         }
-        return;
     }
 
-
-    void testStreamingText(Conversation conversation) {
-        String fullText = "北京野火无限网络科技有限公司是成立于2019年底的一家科技创新企业,公司的主要目标是为广大企业和单位提供优质可控、私有部署的即时通讯和实时音视频能力,为社会信息化水平提高作出自己的贡献。\n" +
-                "\n" +
-                "野火IM是公司研发一套自主可控的即时通讯组件,具有全部私有化、功能齐全、协议稳定可靠、全平台支持、安全性高和支持国产化等技术特点。客户端分层设计,既可开箱即用,也可与现有系统深度融合。具有完善的服务端API和自定义消息功能,可以任意扩展功能。代码开源率高,方便二次开发和使用。支持多人实时音视频和会议功能,线上沟通更通畅。\n" +
-                "\n" +
-                "公司致力于开源项目,在Github上开源项目广受好评,其中Server项目有超过7.1K个Star,组织合计Star超过1万个。有大量的技术公司受益于我们的开源,为自己的产品添加了即时通讯能力,这也算是我们公司为社会信息化建设做出的一点点贡献吧。\n" +
-                "\n" +
-                "公司以即时通讯技术为核心,持续努力优化和完善即时通讯和实时音视频产品,努力为客户提供最优质的即时通讯和实时音视频能力。";
+    void testStreamingText(long replyMessageId, Conversation conversation, String fullText) {
         int i = 0;
         String streamId = UUID.randomUUID().toString();
         while (i < fullText.length()) {
-            i+= 15;
+            i+= 5;
 
             boolean finish = i >= fullText.length();
             String partText = finish?fullText:fullText.substring(0, i);
 
-            MessagePayload payload = StreamingTextMessageContentBuilder.newBuilder(streamId).text(partText).generating(!finish).build();
+            MessageContent messageContent;
+            if(finish) {
+                messageContent = new StreamTextGeneratedMessageContent(partText, streamId);
+            } else {
+                messageContent = new StreamTextGeneratingMessageContent(partText, streamId);
+            }
+            //消息转成Payload并发送
+            MessagePayload payload = messageContent.encode();
 
             try {
-                IMResult<SendMessageResult> resultSendMessage = robotService.sendMessage(mRobotConfig.getIm_id(), conversation, payload);
+                IMResult<SendMessageResult> resultSendMessage;
+                if(replyMessageId > 0) {
+                    resultSendMessage = robotService.replyMessage(replyMessageId, payload, false);
+                } else {
+                    resultSendMessage = robotService.sendMessage(mRobotConfig.getIm_id(), conversation, payload);
+                }
                 if (resultSendMessage != null && resultSendMessage.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
                     System.out.println("send message success");
                 } else {

+ 0 - 22
src/main/java/cn/wildfirechat/app/tuling/TulingResponse.java

@@ -1,22 +0,0 @@
-package cn.wildfirechat.app.tuling;
-
-import java.util.List;
-
-public class TulingResponse {
-    public static class Intent {
-        int code;
-        String intentName;
-        String actionName;
-    }
-    public static class Value {
-        String url;
-        String text;
-    }
-    public static class Result {
-        int groupType;
-        String resultType;
-        Value values;
-    }
-    public Intent intent;
-    public List<Result> results;
-}

+ 0 - 54
src/main/java/cn/wildfirechat/app/tuling/TulingService.java

@@ -1,54 +0,0 @@
-package cn.wildfirechat.app.tuling;
-
-import cn.wildfirechat.app.HttpUtils;
-import cn.wildfirechat.app.RobotConfig;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-import org.springframework.util.StringUtils;
-
-@Service
-public class TulingService {
-    @Autowired
-    private RobotConfig mRobotConfig;
-
-    public String handleWord(String sender, String text) {
-        String response = "";
-        if (mRobotConfig.use_tuling) {
-            String searchReq = "{\n" + "\t\"reqType\":0,\n" + "    \"perception\": {\n" + "        \"inputText\": {\n" + "            \"text\": \"${TEXT}\"\n" + "        }\n" + "    },\n" + "    \"userInfo\": {\n" + "        \"apiKey\": \"${APIKEY}\",\n" + "        \"userId\": \"${USERID}\"\n" + "    }\n" + "}";
-            searchReq = searchReq.replace("${APIKEY}", mRobotConfig.getTuling_key()).replace("${USERID}", Math.abs(sender.hashCode()) + "");
-            searchReq = searchReq.replace("${TEXT}", text);
-
-
-            try {
-                TulingResponse s = HttpUtils.post("http://openapi.tuling123.com/openapi/api/v2", searchReq, TulingResponse.class);
-                if (s != null) {
-                    if (s.results != null && s.results.size() > 0) {
-                        for (TulingResponse.Result result : s.results
-                        ) {
-                            if (result.values != null) {
-                                if (!StringUtils.isEmpty(result.values.text)) {
-                                    if(StringUtils.isEmpty(response)) {
-                                        response = result.values.text;
-                                    } else {
-                                        response = response + " \n" + result.values.text;
-                                    }
-                                }
-
-                                if (!StringUtils.isEmpty(result.values.url)) {
-                                    if(StringUtils.isEmpty(response)) {
-                                        response = result.values.url;
-                                    } else {
-                                        response = response + " \n" + result.values.url;
-                                    }
-                                }
-                            }
-                        }
-                    }
-                }
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-        }
-        return response;
-    }
-}

File diff suppressed because it is too large
+ 0 - 2
src/main/java/cn/wildfirechat/app/webhook/github/pojo/PushEvent.java


Some files were not shown because too many files changed in this diff