圖靈第三方機器人自動回覆

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">在個別APP中需要中,使用者遇到問題但是找不到解決的問題,這時候要是有個人可以出現幫助客戶解決問題,是不是太炫酷了,而且大幅度增加使用者</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">的體驗度。今天給大家介紹一個第三方的自動回覆機器人,可以解決使用者不會或者解決不了的問題。正式開始吧!!</span>

我用的自動回覆是圖靈機器人,官方網址是:http://www.tuling123.com/

登入註冊等基本簡單的內容我就不說了,

登入成功之後找到建立機器人選擇需要的,我們既不是做QQ也不是微信,所以選擇自定義,如下圖

當我們選擇自定義以後就可以編輯我們自己的機器人了,如下

這只是簡單的基本資訊,如果想高大上一點,還可以選擇知識庫,如下

點進去之後就可以控制機器遇到問題要怎麼回答了,如下

完成我們需要配置的機器人在在最下面會有一個
“接入”我們只要記住API地址和APIKey,如下圖

這樣就是機器人就配置完成了,當然了,這是其中的冰山一角,我們可以編輯其他的,比如能力拓展,聰慧度,學習能力等等…因為每個人的需求不一樣我就不給大家一一配置了,大家可以根據自己的需求 進行設定,下面是程式碼部分

———————————————————————————————————————————————————————————

先給大家看一下我專案枝幹,並且說明每個類具體的作用

用到的類和佈局已經在圖中用紅色框標註

ChatMessage:此類是bean有msg,type(列舉類需要自己定義),Date

result:此類是機器人返回內容的實體類,有返回碼和返回內容

HttpUtils:此類是根據我們的API地址和APIKey拼接字串然後傳送,並根據返回的josn進行解析

ChatMessageAdapter:此類是自定義介面卡,具體內容一會看程式碼

ManinActivity:是主Activty負責顯示,更新UI,傳送內容、、、

三個佈局檔案很好理解就不一一解釋了,好了,具體我會用註釋方法進行解釋,請看程式碼

ChatMessage程式碼

public class ChatMessage {
private String msg;
private Type type;
private Date date;
public enum Type
{
INCOMING, OUTCOMING
}
public ChatMessage()
{
}
public ChatMessage(String msg, Type type, Date date)
{
super();
this.msg = msg;
this.type = type;
this.date = date;
}
public String getMsg()
{
return msg;
}
public void setMsg(String msg)
{
this.msg = msg;
}
public Type getType()
{
return type;
}
public void setType(Type type)
{
this.type = type;
}
public Date getDate()
{
return date;
}
public void setDate(Date date)
{
this.date = date;
}
}

result程式碼

public class result {
//返回碼
private int code;
//機器人回覆的內容
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}

HttpUtils程式碼

public class HttpUtil {
//我們剛才的獲取的API地址:
private static final String URL="http://www.tuling123.com/openapi/api";
//我們剛才的獲取的APIKey:
private static final String APP_KAY="a97341b1eff7404f898f8a4f75d62c5e";
public static ChatMessage sendMessage(String msg){
ChatMessage chatmessage=new ChatMessage();
String jsonretult = getJosn(msg);
Gson gson=new Gson();
result resultjosn =null;
try {
resultjosn=gson.fromJson(jsonretult, result.class);
chatmessage.setMsg(resultjosn.getText());
} catch (JsonSyntaxException e) {
chatmessage.setMsg("伺服器繁忙,請稍後再試試");
e.printStackTrace();
}
chatmessage.setDate(new Date());
chatmessage.setType(ChatMessage.Type.INCOMING);
return chatmessage;
}
//傳送請求並且獲取返回JOSN
public static String getJosn(String msg)
{
String result = "";
//獲取傳送的請求字串
String url = stitchingString(msg);
ByteArrayOutputStream baos = null;
InputStream is = null;
try
{
java.net.URL urlNet = new java.net.URL(url);
HttpURLConnection conn = (HttpURLConnection) urlNet
.openConnection();
conn.setReadTimeout(5 * 1000);
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("GET");
is = conn.getInputStream();
int len = -1;
byte[] buf = new byte[128];
baos = new ByteArrayOutputStream();
while ((len = is.read(buf)) != -1)
{
baos.write(buf, 0, len);
}
baos.flush();
result = new String(baos.toByteArray());
} catch (MalformedURLException e)
{
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
} finally
{
try
{
if (baos != null)
baos.close();
} catch (IOException e)
{
e.printStackTrace();
}
try
{
if (is != null)
{
is.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
}
return result;
}
//拼接請求字串並設定請求編碼
private static String stitchingString(String msg)
{
String url = "";
try
{
url = URL   "?key="   APP_KAY   "&info="
URLEncoder.encode(msg, "UTF-8");
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
Log.i("url      ", "url: " url);
return url;
}
}

ChatMessageAdapter程式碼

public class ChatMessageAdapter extends BaseAdapter
{
private LayoutInflater mInflater;
private List<ChatMessage> mDatas;
public ChatMessageAdapter(Context context, List<ChatMessage> mDatas)
{
mInflater = LayoutInflater.from(context);
this.mDatas = mDatas;
}
@Override
public int getCount()
{
return mDatas.size();
}
@Override
public Object getItem(int position)
{
return mDatas.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public int getItemViewType(int position)
{
//判斷Item類別
ChatMessage chatMessage = mDatas.get(position);
if (chatMessage.getType() == ChatMessage.Type.INCOMING)
{
return 0;
}
return 1;
}
@Override
public int getViewTypeCount()
{
return 2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ChatMessage chatMessage = mDatas.get(position);
ViewHolder viewHolder = null;
if (convertView == null)
{
if (getItemViewType(position) == 0)
{
convertView = mInflater.inflate(R.layout.item_from_msg, parent,
false);
viewHolder = new ViewHolder();
viewHolder.mDate = (TextView) convertView
.findViewById(R.id.id_form_msg_date);
viewHolder.mMsg = (TextView) convertView
.findViewById(R.id.id_from_msg_info);
} else
{
convertView = mInflater.inflate(R.layout.item_to_msg, parent,
false);
viewHolder = new ViewHolder();
viewHolder.mDate = (TextView) convertView
.findViewById(R.id.id_to_msg_date);
viewHolder.mMsg = (TextView) convertView
.findViewById(R.id.id_to_msg_info);
}
convertView.setTag(viewHolder);
} else
{
viewHolder = (ViewHolder) convertView.getTag();
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
viewHolder.mDate.setText(df.format(chatMessage.getDate()));
viewHolder.mMsg.setText(chatMessage.getMsg());
return convertView;
}
private final class ViewHolder
{
TextView mDate;
TextView mMsg;
}
}

MainActivity程式碼

public class MainActivity extends AppCompatActivity {
private ListView mMsgs;
private ChatMessageAdapter mAdapter;
private List<ChatMessage> mDatas;
private EditText mInputMsg;
private Button mSendMsg;
private Handler mHandler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
// 等待接收,子執行緒完成資料的返回
ChatMessage fromMessge = (ChatMessage) msg.obj;
mDatas.add(fromMessge);
mAdapter.notifyDataSetChanged();
mMsgs.setSelection(mDatas.size()-1);
};
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.rengong_man);
initView();
initDatas();
// 初始化事件
initListener();
}
//當我們點選傳送時候觸發的事件
private void initListener()
{
mSendMsg.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//將EditText中的內容轉化為文字
final String toMsg = mInputMsg.getText().toString();
//判斷EditText的內容是否為空
if (TextUtils.isEmpty(toMsg))
{
Toast.makeText(MainActivity.this, "傳送訊息不能為空!",
Toast.LENGTH_SHORT).show();
return;
}
ChatMessage toMessage = new ChatMessage();
toMessage.setDate(new Date());
toMessage.setMsg(toMsg);
toMessage.setType(ChatMessage.Type.OUTCOMING);
mDatas.add(toMessage);
mAdapter.notifyDataSetChanged();
mMsgs.setSelection(mDatas.size()-1);
//情況EditText
mInputMsg.setText("");
new Thread()
{
public void run()
{
ChatMessage fromMessage = HttpUtil.sendMessage(toMsg);
Message m = Message.obtain();
m.obj = fromMessage;
mHandler.sendMessage(m);
};
}.start();
}
});
}
//設定機器人初始資訊
private void initDatas()
{
mDatas = new ArrayList<ChatMessage>();
mDatas.add(new ChatMessage("你好,我是柳巖", ChatMessage.Type.INCOMING, new Date()));
mAdapter = new ChatMessageAdapter(this, mDatas);
mMsgs.setAdapter(mAdapter);
}
//初始化控制元件
private void initView()
{
mMsgs = (ListView) findViewById(R.id.id_listview_msgs);
mInputMsg = (EditText) findViewById(R.id.id_input_msg);
mSendMsg = (Button) findViewById(R.id.id_send_msg);
}
}

item_from_msg.xml程式碼

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@ id/id_form_msg_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#bebebe"
android:text="2012-12-12 12:12:12"
android:textColor="#f5f5f5"
android:textSize="12sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:layout_width="49dp"
android:layout_height="49dp"
android:src="@drawable/icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="柳巖"
android:textSize="18sp" >
</TextView>
</LinearLayout>
<TextView
android:id="@ id/id_from_msg_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/chatfrom_bg_normal"
android:gravity="center_vertical"
android:textSize="16sp"
android:text="你好" />
</LinearLayout>
</LinearLayout>

item_to_msg.xml程式碼

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="vertical" >
<TextView
android:id="@ id/id_to_msg_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#bebebe"
android:text="2012-12-12 12:12:12"
android:textColor="#f5f5f5"
android:textSize="12sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<TextView
android:id="@ id/id_to_msg_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/chatto_bg_normal"
android:gravity="center_vertical"
android:textSize="16sp"
android:text="你好" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:layout_width="49dp"
android:layout_height="49dp"
android:src="@drawable/me" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="hyman"
android:textSize="18sp" >
</TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>

rengong_man.xml程式碼

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@ id/id_ly_top"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:background="@drawable/title_bar9" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="嘿嘿嘿"
android:textColor="#ffffff"
android:textSize="22sp" />
</RelativeLayout>
<RelativeLayout
android:id="@ id/id_ly_bottom"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
android:background="@drawable/bottom_bar9" >
<Button
android:id="@ id/id_send_msg"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/send_btn_bg"
android:text="傳送" />
<EditText
android:id="@ id/id_input_msg"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@id/id_send_msg"
android:background="@drawable/login_edit_normal"
android:textSize="18sp" />
</RelativeLayout>
<ListView
android:id="@ id/id_listview_msgs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/id_ly_bottom"
android:layout_below="@id/id_ly_top"
android:divider="@null"
android:dividerHeight="5dp" >
</ListView>
</RelativeLayout>

到這裡所有程式碼已經粘完了,如果大家有什麼問題或者發現我有什麼問題可以回覆我。。謝謝