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;
}
}
}
}