Skip to content

Instantly share code, notes, and snippets.

@edwardinubuntu
Last active October 23, 2017 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edwardinubuntu/fe200c845a5417a573e5939d9f60819d to your computer and use it in GitHub Desktop.
Save edwardinubuntu/fe200c845a5417a573e5939d9f60819d to your computer and use it in GitHub Desktop.

一鍵開微客服

  • 檢查 1 對 1 聊天室是否存在。
  • 如果存在,直接開啟聊天室。
  • 如果不存在,先建立聊天室,將自己加入聊天室,開啟聊天室。
// Check if support contact in chat
if (!TextUtils.isEmpty(new TripTime(getContext()).getContactInChatUrl())) {
final UserPreferencesManager userPreferencesManager = new UserPreferencesManager(getContext());
final String roomId = userPreferencesManager.getMember().getUserId() + "-triptime-contact";
progressDialog.setTitle(getString(R.string.check_room_with_id));
progressDialog.setMessage(getString(R.string.loading));
progressDialog.show();
// Find room id
IMKit.instance().getRoom(roomId, new IMRestCallback<Room>() {
@Override
public void onFailure(Call<ApiResponse<Room>> call, Throwable t) {
super.onFailure(call, t);
Log.d(TripTime.TAG, "Room not found");
final Room room = new Room();
room.setName(userPreferencesManager.getMember().getName());
room.setId(roomId);
IMKit.instance().loadCurrentClient(new IMRestCallback<Client>() {
@Override
public void onResult(Client client) {
Log.d(TripTime.TAG, "createRoom");
IMKit.instance().createRoom(room, new IMRestCallback<Room>() {
@Override
public void onResult(final Room roomResult) {
if (progressDialog != null) {
progressDialog.dismiss();
}
Log.d(TripTime.TAG, "IMKIT IMRestCallback. Room id: " + roomResult.getId());
IMKit.instance().joinRoom(roomResult.getId(), new IMRestCallback<Room>() {
@Override
public void onResult(Room room) {
Log.d(TripTime.TAG, "Add my self into room.");
Log.d(TripTime.TAG, "Room member size: " + room.getMembers().size());
IMKit.instance().addMember(roomResult.getId(), new TripTime(getContext()).getContactId(), new IMRestCallback<Room>() {
@Override
public void onResult(Room room) {
openContactChatWeb(navigator, userPreferencesManager, roomResult.getId());
}
});
}
});
}
});
}
});
}
@Override
public void onResult(Room roomResult) {
if (progressDialog != null) {
progressDialog.dismiss();
}
if (roomResult != null && !TextUtils.isEmpty(roomResult.getId())) {
openContactChatWeb(navigator, userPreferencesManager, roomResult.getId());
}
}
});
} else {
navigator.showRoundedAlert(getActivity(), getString(R.string.contact_us_title), getString(R.string.contact_us_subtitle),
getString(R.string.send_us_email), true, true, new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto", TripTime.CONTACT_US_VIA_EMAIL, null));
startActivity(Intent.createChooser(emailIntent, "Contact us via email"));
}
}, null);
}
private void openContactChatWeb(Navigator navigator, UserPreferencesManager userPreferencesManager, String contactRoomId) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(new TripTime(getContext()).getContactInChatUrl());
stringBuilder.append("chat?roomId=");
stringBuilder.append(contactRoomId);
stringBuilder.append("&token=" + userPreferencesManager.getMember().getAccessToken());
navigator.loadUriToChrome(Uri.parse( stringBuilder.toString() ));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment