Connecting NLP API backend (postman link in Swift), storing messages on Firebase

Hello All,

I am new to the forum and super new to the Xcode & swift. I am trying to connect my backend with an examplar chat frontend. However, I am totally lost on how to connect my NLP API with postman link. Messages will be stored on firebase for the interaction analysis purposes for a test flight. It is my first project and I absolutely enjoy it but I need support! :slight_smile: Any help is appreciated.

Best!
Kyoto


import Foundation
import FirebaseFirestore
import FirebaseFirestoreSwift

class MessagesManager: ObservableObject {
@Published private(set) var messages: [Message] = []
@Published private(set) var lastMessageId: String = “”

// Create an instance of our Firestore database
let db = Firestore.firestore()

// On initialize of the MessagesManager class, get the messages from Firestore
init() {
    getMessages()
}

// Read message from Firestore in real-time with the addSnapShotListener
func getMessages() {
    db.collection("messages").addSnapshotListener { querySnapshot, error in
        
        // If we don't have documents, exit the function
        guard let documents = querySnapshot?.documents else {
            print("Error fetching documents: \(String(describing: error))")
            return
        }
        
        // Mapping through the documents
        self.messages = documents.compactMap { document -> Message? in
            do {
                // Converting each document into the Message model
                // Note that data(as:) is a function available only in FirebaseFirestoreSwift package - remember to import it at the top
                return try document.data(as: Message.self)
            } catch {
                // If we run into an error, print the error in the console
                print("Error decoding document into Message: \(error)")

                // Return nil if we run into an error - but the compactMap will not include it in the final array
                return nil
            }
        }
        
        // Sorting the messages by sent date
        self.messages.sort { $0.timestamp < $1.timestamp }
        
        // Getting the ID of the last message so we automatically scroll to it in ContentView
        if let id = self.messages.last?.id {
            self.lastMessageId = id
        }
    }
}

// Add a message in Firestore
func sendMessage(text: String) {
    do {
        // Create a new Message instance, with a unique ID, the text we passed, a received value set to false (since the user will always be the sender), and a timestamp
        let newMessage = Message(id: "\(UUID())", text: text, received: false, timestamp: Date())
        
        // Create a new document in Firestore with the newMessage variable above, and use setData(from:) to convert the Message into Firestore data
        // Note that setData(from:) is a function available only in FirebaseFirestoreSwift package - remember to import it at the top
        try db.collection("messages").document().setData(from: newMessage)
        
    } catch {
        // If we run into an error, print the error in the console
        print("Error adding message to Firestore: \(error)")
    }
}

}

Welcome! Sounds like you may have posted in the wrong section, this is for Kitura. I would recommend you instead post in Firebase and Swift related areas.

Thank you @robertomachorro !! I changed now :slight_smile: Hope to have an answer.
Best!

1 Like