From a7cef3c6f87076977d1db2a0c482c436dde32945 Mon Sep 17 00:00:00 2001
From: 18401019693 <https://gitee.com/xxkp/NEWPDLIVE.git>
Date: Tue, 27 Dec 2022 14:41:17 +0800
Subject: [PATCH] =?UTF-8?q?=E6=9C=BA=E5=99=A8=E4=BA=BA=E8=87=AA=E5=8A=A8?=
 =?UTF-8?q?=E6=89=93=E6=8B=9B=E5=91=BC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../live/activity/LiveRyAnchorActivity.java   | 171 ++++++++++++++++++
 .../yunbao/live/event/LiveAudienceEvent.java  |   3 +-
 2 files changed, 173 insertions(+), 1 deletion(-)

diff --git a/live/src/main/java/com/yunbao/live/activity/LiveRyAnchorActivity.java b/live/src/main/java/com/yunbao/live/activity/LiveRyAnchorActivity.java
index a7e4a792c..6e02d0be9 100644
--- a/live/src/main/java/com/yunbao/live/activity/LiveRyAnchorActivity.java
+++ b/live/src/main/java/com/yunbao/live/activity/LiveRyAnchorActivity.java
@@ -17,6 +17,7 @@ import android.content.Intent;
 import android.graphics.Bitmap;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
+import android.os.Handler;
 import android.text.TextUtils;
 import android.util.Log;
 import android.view.KeyEvent;
@@ -31,17 +32,22 @@ import androidx.fragment.app.DialogFragment;
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
+import com.blankj.utilcode.util.GsonUtils;
 import com.bumptech.glide.Glide;
 import com.bumptech.glide.request.target.CustomTarget;
 import com.bumptech.glide.request.transition.Transition;
 import com.lxj.xpopup.XPopup;
+import com.lxj.xpopup.core.BasePopupView;
+import com.lxj.xpopup.interfaces.XPopupCallback;
 import com.opensource.svgaplayer.SVGAImageView;
 import com.tencent.trtc.TRTCCloudDef;
 import com.yunbao.common.CommonAppConfig;
 import com.yunbao.common.CommonAppContext;
 import com.yunbao.common.Constants;
+import com.yunbao.common.bean.AiAutomaticSpeechModel;
 import com.yunbao.common.bean.IMLoginModel;
 import com.yunbao.common.bean.LinkMicUserBean;
+import com.yunbao.common.bean.LiveAiRobotBean;
 import com.yunbao.common.bean.LiveBean;
 import com.yunbao.common.bean.LiveUserGiftBean;
 import com.yunbao.common.bean.MicUserBean;
@@ -52,6 +58,7 @@ import com.yunbao.common.http.CommonHttpConsts;
 import com.yunbao.common.http.CommonHttpUtil;
 import com.yunbao.common.http.HttpCallback;
 import com.yunbao.common.http.HttpClient;
+import com.yunbao.common.http.live.LiveNetManager;
 import com.yunbao.common.manager.IMLoginManager;
 import com.yunbao.common.manager.IMRTCManager;
 import com.yunbao.common.manager.RandomPkManager;
@@ -99,11 +106,13 @@ import com.yunbao.live.views.LivePushRyViewHolder;
 import com.yunbao.live.views.LiveRoomViewHolder;
 import com.yunbao.live.views.LiveRyAnchorViewHolder;
 
+import org.greenrobot.eventbus.EventBus;
 import org.greenrobot.eventbus.Subscribe;
 import org.greenrobot.eventbus.ThreadMode;
 
 import java.io.File;
 import java.util.List;
+import java.util.Random;
 
 import cn.rongcloud.rtc.api.RCRTCEngine;
 import cn.rongcloud.rtc.api.callback.IRCRTCResultCallback;
@@ -284,6 +293,52 @@ public class LiveRyAnchorActivity extends LiveActivity implements LiveFunctionCl
         switch (functionID) {
             case Constants.LIVE_ROBOT:
                 new XPopup.Builder(mContext)
+                        .setPopupCallback(new XPopupCallback() {
+                            @Override
+                            public void onCreated(BasePopupView popupView) {
+
+                            }
+
+                            @Override
+                            public void beforeShow(BasePopupView popupView) {
+
+                            }
+
+                            @Override
+                            public void onShow(BasePopupView popupView) {
+
+                            }
+
+                            @Override
+                            public void onDismiss(BasePopupView popupView) {
+                                EventBus.getDefault().post(new LiveAudienceEvent().setType(LiveAudienceEvent.LiveAudienceType.UPDATA_ROBOT));
+                            }
+
+                            @Override
+                            public void beforeDismiss(BasePopupView popupView) {
+
+                            }
+
+                            @Override
+                            public boolean onBackPressed(BasePopupView popupView) {
+                                return false;
+                            }
+
+                            @Override
+                            public void onKeyBoardStateChanged(BasePopupView popupView, int height) {
+
+                            }
+
+                            @Override
+                            public void onDrag(BasePopupView popupView, int value, float percent, boolean upOrLeft) {
+
+                            }
+
+                            @Override
+                            public void onClickOutside(BasePopupView popupView) {
+
+                            }
+                        })
                         .asCustom(new LiveRobotSettingCustomPopup(mContext))
                         .show();
                 break;
@@ -674,6 +729,7 @@ public class LiveRyAnchorActivity extends LiveActivity implements LiveFunctionCl
      * @param data createRoom返回的数据
      */
     public void startLiveSuccess(String data, int liveType, int liveTypeVal) {
+        Log.e("随机打招呼", "(((((((((((((((((((((((((((((((((((((((((((((((" + data);
         mLiveType = liveType;
         mLiveTypeVal = liveTypeVal;
         //处理createRoom返回的数据
@@ -774,6 +830,7 @@ public class LiveRyAnchorActivity extends LiveActivity implements LiveFunctionCl
         if (CommonAppConfig.GAME_ENABLE) {
             mGameList = JSON.parseArray(obj.getString("game_switch"), Integer.class);
         }
+        getAiRobotStatus();
     }
 
     /**
@@ -1506,6 +1563,9 @@ public class LiveRyAnchorActivity extends LiveActivity implements LiveFunctionCl
                     mLiveRoomViewHolder.aiAutomaticSpeech((event.getAiAutomaticSpeechModel()));
                 }
                 break;
+            case UPDATA_ROBOT:
+                getAiRobotStatus();
+                break;
 
         }
     }
@@ -1532,4 +1592,115 @@ public class LiveRyAnchorActivity extends LiveActivity implements LiveFunctionCl
             }
         });
     }
+
+    /**
+     * 获取机器人配置;取得求关注时间间隔
+     */
+    private long robotTime = 0;
+
+    private void getAiRobotStatus() {
+        Log.e("随机打招呼", "(((((((((((((((((((((((((((((((((((((((((((((((");
+        LiveNetManager.get(mContext)
+                .getAiRobotStatus(new com.yunbao.common.http.base.HttpCallback<LiveAiRobotBean>() {
+                    @Override
+                    public void onSuccess(LiveAiRobotBean data) {
+                        Log.e("随机打招呼", "(((((((((((((((((((((((((((((((((((((((((((((((" + data.toString());
+                        if (data.getStatus() == 1) {
+                            if (robotRunnable != null) {
+                                robotHandler.removeCallbacks(robotRunnable);
+                                robotRunnable = null;
+                            }
+                            robotRunnable = new Runnable() {
+                                @Override
+                                public void run() {
+                                    Log.e("随机打招呼", "(((((((((((((((((((((((((((((((((((((((((((((((data.getName())" + data.getName());
+                                    String robotName = data.getName();
+                                    //发送消息
+                                    LiveNetManager.get(mContext)
+                                            .getAiRobotBody(LiveAiRobotBean.Message.TYPE_FOLLOW,
+                                                    new com.yunbao.common.http.base.HttpCallback<List<LiveAiRobotBean.Message>>() {
+                                                        @Override
+                                                        public void onSuccess(List<LiveAiRobotBean.Message> messageList) {
+                                                            Random random = new Random();
+                                                            int randNumber = random.nextInt(messageList.size() - 1) - 1;
+                                                            Log.e("随机打招呼", "(((((((((((((((((((((((((((((((((((((((((((((((data.randNumber())" + randNumber);
+                                                            Log.e("随机打招呼", "(((((((((((((((((((((((((((((((((((((((((((((((data)" + messageList.toString());
+                                                            sendRobotMessage(robotName, messageList.get(randNumber).getContent());
+                                                        }
+
+                                                        @Override
+                                                        public void onError(String error) {
+                                                            Log.e("随机打招呼", "(((((((((((((((((((((((((((((((((((((((((((((((data.error())" + error);
+                                                        }
+                                                    });
+
+                                }
+                            };
+                            robotTime = data.getTime() * 60 * 1000;
+                            Log.e("随机打招呼", "(((((((((((((((((((((((((((((((((((((((((((((((robotTime)" + robotTime);
+                            robotHandler.postDelayed(robotRunnable, robotTime);
+                        }
+                    }
+
+                    @Override
+                    public void onError(String error) {
+                        ToastUtil.show(error);
+                    }
+                });
+    }
+
+    //机器人求关注轮询器
+    private Handler robotHandler = new Handler();
+    private Runnable robotRunnable = null;
+
+    /**
+     * '{"msg":
+     * [{"_method_":"aiAutomaticSpeech",
+     * "ct":{"name_color":"#f19ec2","ai_name":"'+_this.robotInfo.ai_name+'",
+     * "content":"'+content+'","user_name":"",
+     * "icon":"https://downs.yaoulive.com/xzs_tab.png",
+     * "system_bubble":"https://downs.yaoulive.com/xzs_qipao.9.png"},
+     * "msgtype":"10"}],"retcode":"000000","retmsg":"OK"}'
+     */
+    private void sendRobotMessage(String name, String content) {
+        Log.e("随机打招呼", name + "==========00000000000000++++" + content);
+        AiAutomaticSpeechModel aiAutomaticSpeechModel = new AiAutomaticSpeechModel();
+        aiAutomaticSpeechModel.setAiName(name)
+                .setIcon("https://downs.yaoulive.com/xzs_tab.png")
+                .setNameColor("#f19ec2")
+                .setSystemBubble("https://downs.yaoulive.com/xzs_qipao.9.png")
+                .setUserName("")
+                .setContent(content);
+        SocketSendBean msg =
+                new SocketSendBean()
+                        .param("_method_", "aiAutomaticSpeech")
+                        .param("msgtype", "10")
+                        .param("ct", GsonUtils.toJson(aiAutomaticSpeechModel, AiAutomaticSpeechModel.class));
+        msg.create();
+        String targetId = "g" + mLiveUid;
+        Conversation.ConversationType conversationType = Conversation.ConversationType.CHATROOM;
+        TextMessage messageContent = TextMessage.obtain(msg.mResult.toString());
+        Message message = Message.obtain(targetId, conversationType, messageContent);
+
+        RongIMClient.getInstance().sendMessage(message, null, null, new IRongCallback.ISendMessageCallback() {
+            @Override
+            public void onAttached(Message message) {
+
+            }
+
+            @Override
+            public void onSuccess(Message message) {
+                if (SocketRyClient.mSocketHandler != null) {
+                    SocketRyClient.mSocketHandler.processBroadcast(msg.mResult.toString() + "");
+                }
+            }
+
+            @Override
+            public void onError(Message message, RongIMClient.ErrorCode errorCode) {
+                Log.e("tx", "aaa" + errorCode.toString());
+                ToastUtil.show(errorCode.code + ":" + errorCode.msg);
+            }
+        });
+        robotHandler.postDelayed(robotRunnable, robotTime);
+    }
 }
diff --git a/live/src/main/java/com/yunbao/live/event/LiveAudienceEvent.java b/live/src/main/java/com/yunbao/live/event/LiveAudienceEvent.java
index c690ed6f2..8e5daccd3 100644
--- a/live/src/main/java/com/yunbao/live/event/LiveAudienceEvent.java
+++ b/live/src/main/java/com/yunbao/live/event/LiveAudienceEvent.java
@@ -255,7 +255,8 @@ public class LiveAudienceEvent extends BaseModel {
         DISCONNEXT_PK_TIME(50, "单人Pk结束"),
         UP_PK_TWO(51, "PK头像信息"),
         CUSTOM_FULL_SERVICE_NOTIFY(51, "全服通知"),
-        CHANGE_VIEW(52, "更改连胜位置");
+        CHANGE_VIEW(52, "更改连胜位置"),
+        UPDATA_ROBOT(53, "更新机器人消息");
 
         private int type;
         private String name;