Chat Consultations
Initiating the Chat Service
Initialize the chat service by calling AltibbiChat().init() with the Consultation object. This connects the user to the chat provider (Sendbird).
try {
User user = await AltibbiChat().init(consultation: consultation);
print(\"Connected as: ${user.userId}\");
} catch (e) {
print(\"Chat Initialization Error: $e\");
}
Adding Event Listeners
Use GroupChannelHandler to handle incoming messages, typing status, and other events.
class MyChannelHandler extends GroupChannelHandler {
@override
void onMessageReceived(BaseChannel channel, BaseMessage message) {
print(\"New Message: ${message.message}\");
}
@override
void onTypingStatusUpdated(BaseChannel channel) {
print(\"Typing status changed\");
}
}
AltibbiChat().addChannelHandler('myUniqueId', MyChannelHandler());
Loading Previous Messages
You can retrieve history using the PreviousMessageListQuery:
GroupChannel groupChannel = await AltibbiChat().getGroupChannel(consultation);
List<BaseMessage> messages = await groupChannel.getMessagesByTimestamp(
DateTime.now().millisecondsSinceEpoch,
MessageListParams(),
);
Sending Messages
GroupChannel groupChannel = await AltibbiChat().getGroupChannel(consultation);
groupChannel.sendUserMessage(UserMessageCreateParams(message: \"Hello Doctor\"));
Typing Indicators
groupChannel.startTyping();
groupChannel.endTyping();