Chat Consultation
ChatConfig Object
For chat consultations, the consultation data will include the ChatConfig object. This config data is used to initiate and handle the chat between the user and the doctor.
struct ChatConfig {
var id: Int?
var consultationId: Int?
var groupId: String?
var chatUserId: String?
var appId: String?
var chatUserToken: String?
}
Chat Initialization
Use the chatConfig from the consultation data to initialize the chat.
import AltibbiTelehealth
if let info = consultationInfo, let config = info.chatConfig {
AltibbiChat.initialize(config: config)
}
Adding Event Listeners and Handlers
Handle chat connection and events by implementing the related protocols: GroupChannelDelegate and ConnectionDelegate.
// Connection Delegate
func didDisconnect(userId: String) {
print("ConnectionDelegate >>> didDisconnect user id: \(userId)")
}
// Group Channel Delegate
func channel(_ channel: BaseChannel, didReceive message: BaseMessage) {
// This is to handle the received messages
self.messages.append(message)
self.tableView.reloadData()
}
func channel(_ channel: GroupChannel, userDidLeave user: SendbirdChatSDK.User) {
// This is to handle if the doctor left the chat, indicating the consultation is closed
}
func channelDidUpdateTypingStatus(_ channel: GroupChannel) {
// This reflects the typing status of the other side
if channel.isTyping() {
self.typingLabel.text = "Typing..."
} else {
self.typingLabel.text = ""
}
}
Handle Chat Display
Display chat messages using UITableView and implementing its protocols.
var messages: [BaseMessage] = []
@IBOutlet weak var tableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messages.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let message = messages[indexPath.row]
if message.sender?.userId == (consultationInfo?.chatConfig?.chatUserId)! {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyMessageCell", for: indexPath) as! MyMessageCell
cell.configure(with: message)
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "MessageCell", for: indexPath) as! MessageCell
cell.configure(with: message)
return cell
}
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
// Registering chat cells
tableView.register(UINib(nibName: "MyMessageCell", bundle: nil), forCellReuseIdentifier: "MyMessageCell")
tableView.register(UINib(nibName: "MessageCell", bundle: nil), forCellReuseIdentifier: "MessageCell")
}
Loading Previous Messages
When entering the chat screen, load the previous messages from the channel. Ensure this happens after AltibbiChat.initialize().
var query: PreviousMessageListQuery?
func loadPreviousMessages() {
guard let channel = AltibbiChat.chatChannel else { return }
self.query = channel.createPreviousMessageListQuery { params in
params.limit = 200
params.reverse = false
}
self.query?.loadNextPage { messages, error in
if let error = error {
print("Error loading messages: \(error.localizedDescription)")
return
}
if let oldMessages = messages {
DispatchQueue.main.async {
self.messages.append(contentsOf: oldMessages)
self.tableView.reloadData()
self.scrollToBottom()
// Add delegates
AltibbiChat.addChannelDelegate(self, identifier: "Channel_Delegate")
// Note: AltibbiChat.addConnectionDelegate can also be used
}
}
}
}
Sending Messages
Use sendUserMessage from AltibbiChat.chatChannel to send text messages.
AltibbiChat.chatChannel?.sendUserMessage(text: msg) { userMsg, error in
if let error = error {
print("sendUserMessage Error: \(error.localizedDescription)")
}
if let sentMessage = userMsg {
DispatchQueue.main.async {
self.messages.append(sentMessage)
self.tableView.reloadData()
}
}
}
Sending Media Files
You can send pictures and PDF files by uploading them using ApiService.uploadMedia first. Once the file is uploaded, you get a Media object with a url, which you can then send as a message.
For displaying these media messages, check if the message content includes a URL to a supported file type (e.g., .jpg, .pdf).