图灵机器人对话

activity

public class CustomerServiceActivity extends AppCompatActivity {
private ActivityCustomerServiceBinding _binding;
private PromptDialog promptDialog;
private CustomerChatAdapter customerChatAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _binding= DataBindingUtil.setContentView(this,R.layout.activity_customer_service);
        StatusUtil.setSystemStatus(this,false,true);
        _binding.btnSend.setOnClickListener(v -> sendCustomer());
       promptDialog=new PromptDialog(CustomerServiceActivity.this);
       customerChatAdapter=new CustomerChatAdapter(R.layout.item_customer_view,null);
       initRecyclerView(_binding.rvList,customerChatAdapter, LinearLayoutManager.VERTICAL);

    }


    /**
     * 列表设置
     */
    private void initRecyclerView(RecyclerView recyclerView, BaseQuickAdapter baseQuickAdapter,
                                  int Orientation) {
        recyclerView.setHasFixedSize(false);
        GridLayoutManager layoutManager = new GridLayoutManager(CustomerServiceActivity.this, 1);
        layoutManager.setOrientation(Orientation);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(baseQuickAdapter);
    }

    /**
     * 获取下文
     */
    private TuningQueryBean callBean;
    private void sendCustomer() {
        if (!Tool.etIsOk(_binding.etCustomer)) {
            promptDialog.showInfoDelay("请输入您想要询问的问题", 3000);
            return;
        }
    TuningQueryBean.ResultsBean.ValuesBean valuesBean = new TuningQueryBean.ResultsBean.ValuesBean(_binding.etCustomer.getText().toString());
        customerChatAdapter.addData(new TuningQueryBean.ResultsBean(true, valuesBean));
        customerChatAdapter.notifyDataSetChanged();
        LoadingDlg.showLoading(CustomerServiceActivity.this);
        TuningUtils.TuningReq req = new TuningUtils.TuningReq();
        req.setReqType(0);
        TuningUtils.TuningReq.PerceptionBean.InputTextBean inputTextBean = new TuningUtils.TuningReq.PerceptionBean.InputTextBean();
        inputTextBean.setText(_binding.etCustomer.getText().toString());
        TuningUtils.TuningReq.PerceptionBean perceptionBean = new TuningUtils.TuningReq.PerceptionBean();
        perceptionBean.setInputText(inputTextBean);
        req.setPerception(perceptionBean);
        TuningUtils.TuningReq.UserInfoBean userInfoBean = new TuningUtils.TuningReq.UserInfoBean();
        userInfoBean.setUserId(MyApplication.getLocalUid());
        userInfoBean.setApiKey(API_KEY);
        req.setUserInfo(userInfoBean);
        new Thread(() -> {
            TuningUtils tuningUtils=new TuningUtils();
            String sRet = tuningUtils.create(req);
            runOnUiThread(() -> {
                LoadingDlg.hideLoading();
                callBean=new Gson().fromJson(sRet,TuningQueryBean.class);
                _binding.etCustomer.setText("");
                    if (callBean.getResults() != null && callBean.getResults().size() > 0) {
                        for (TuningQueryBean.ResultsBean resultsBean : callBean.getResults()) {
                            if (resultsBean.getResultType().equals("text")) {
                                resultsBean.setMine(false);
                                customerChatAdapter.addData(resultsBean);
                                customerChatAdapter.notifyDataSetChanged();
                                _binding.rvList.smoothScrollToPosition(customerChatAdapter.getData().size());
                            }
                        }
                }

            });

        }).start();



    }

}


聊天列表adapter

public class CustomerChatAdapter extends BaseQuickAdapter<TuningQueryBean.ResultsBean, BaseViewHolder> {
    public CustomerChatAdapter(int layoutResId, @Nullable List<TuningQueryBean.ResultsBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper,TuningQueryBean.ResultsBean item) {
        Tool.loadHead(mContext,(ImageView)helper.itemView.findViewById(R.id.headMine), MyApplication.getLocalUid());
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.circleCrop();
        Glide.with(mContext).load(R.drawable.customer_icon).apply(requestOptions).into((ImageView)helper.itemView.findViewById(R.id.headCustomer));
        if (item.isMine()){
         helper.setGone(R.id.mineView,true)
         .setGone(R.id.customerView,false)
         .setText(R.id.contentMine,item.getValues().getText());

     }else {
         helper.setGone(R.id.mineView,false)
                 .setGone(R.id.customerView,true)
                 .setText(R.id.content,item.getValues().getText());

     }
    }
}

###图灵接口调用

public class TuningUtils {
    protected static String urlQuery = "http://openapi.turingapi.com/openapi/api/v2";//智能回复
    public static String API_KEY = "****************";//智能回复

   //https://www.kancloud.cn/turing/www-tuling123-com/718227
    public String create(TuningReq req)
    {
        String ret = Http.post(urlQuery+"?",new Gson().toJson(req));
        return ret;
    }
    public static class TuningReq{


        /**
         * reqType : 0
         * perception : {"inputText":{"text":"附近的酒店"},"inputImage":{"url":"imageUrl"},"selfInfo":{"location":{"city":"北京","province":"北京","street":"信息路"}}}
         * userInfo : {"apiKey":"","userId":""}
         */

        private int reqType;
        private PerceptionBean perception;
        private UserInfoBean userInfo;

        public int getReqType() {
            return reqType;
        }

        public void setReqType(int reqType) {
            this.reqType = reqType;
        }

        public PerceptionBean getPerception() {
            return perception;
        }

        public void setPerception(PerceptionBean perception) {
            this.perception = perception;
        }

        public UserInfoBean getUserInfo() {
            return userInfo;
        }

        public void setUserInfo(UserInfoBean userInfo) {
            this.userInfo = userInfo;
        }

        public static class PerceptionBean {
            /**
             * inputText : {"text":"附近的酒店"}
             * inputImage : {"url":"imageUrl"}
             * selfInfo : {"location":{"city":"北京","province":"北京","street":"信息路"}}
             */

            private InputTextBean inputText;
            private InputImageBean inputImage;
            private SelfInfoBean selfInfo;

            public InputTextBean getInputText() {
                return inputText;
            }

            public void setInputText(InputTextBean inputText) {
                this.inputText = inputText;
            }

            public InputImageBean getInputImage() {
                return inputImage;
            }

            public void setInputImage(InputImageBean inputImage) {
                this.inputImage = inputImage;
            }

            public SelfInfoBean getSelfInfo() {
                return selfInfo;
            }

            public void setSelfInfo(SelfInfoBean selfInfo) {
                this.selfInfo = selfInfo;
            }

            public static class InputTextBean {
                /**
                 * text : 附近的酒店
                 */

                private String text;

                public String getText() {
                    return text;
                }

                public void setText(String text) {
                    this.text = text;
                }
            }

            public class InputImageBean {
                /**
                 * url : imageUrl
                 */

                private String url;

                public String getUrl() {
                    return url;
                }

                public void setUrl(String url) {
                    this.url = url;
                }
            }

            public static class SelfInfoBean {
                /**
                 * location : {"city":"北京","province":"北京","street":"信息路"}
                 */

                private LocationBean location;

                public LocationBean getLocation() {
                    return location;
                }

                public void setLocation(LocationBean location) {
                    this.location = location;
                }

                public static class LocationBean {
                    /**
                     * city : 北京
                     * province : 北京
                     * street : 信息路
                     */

                    private String city;
                    private String province;
                    private String street;

                    public String getCity() {
                        return city;
                    }

                    public void setCity(String city) {
                        this.city = city;
                    }

                    public String getProvince() {
                        return province;
                    }

                    public void setProvince(String province) {
                        this.province = province;
                    }

                    public String getStreet() {
                        return street;
                    }

                    public void setStreet(String street) {
                        this.street = street;
                    }
                }
            }
        }

        public static class UserInfoBean {
            /**
             * apiKey :
             * userId :
             */

            private String apiKey;
            private String userId;

            public String getApiKey() {
                return apiKey;
            }

            public void setApiKey(String apiKey) {
                this.apiKey = apiKey;
            }

            public String getUserId() {
                return userId;
            }

            public void setUserId(String userId) {
                this.userId = userId;
            }
        }
    }


}

效果图

完结,撒花

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值