How can I send messages from my backend?
Most Crisp users interact with you from the chatbox, email or integrations. You may want to develop bots or automation systems, for which messages get sent from your own backend.
First of all, you need to connect your backend to the Crisp APIs. Read this introduction article on how to do it.
Once you are done, you will follow this integration process (explained in details below):
Your backend can reach the Crisp REST API for sending messages
Your backend is connected to Crisp RTM API for realtime events
Build a simple bot answering "Hello World" to user questions matching "Hi"
Good. Lets proceed now. We assume you are using the Crisp Go library, and that your backend is connected to both REST and RTM APIs.
Let's build that basic bot, as described in (3) above. This bot will be very simple, as to introduce how to automate messaging with Crisp.
The bot works the following way:
It gets notified of incoming visitor messages from the Crisp RTM API channel (the session identifier; session_id is included in those notifications, which are necessary to reply)
It extracts the message text from the event, and checks if it matches the word "Hi"
If it matches "Hi", it replies with "Hello World" by sending a text message to the Crisp REST API
The Go code implementing this behavior is the following:
First of all, you need to connect your backend to the Crisp APIs. Read this introduction article on how to do it.
Once you are done, you will follow this integration process (explained in details below):
Your backend can reach the Crisp REST API for sending messages
Your backend is connected to Crisp RTM API for realtime events
Build a simple bot answering "Hello World" to user questions matching "Hi"
Good. Lets proceed now. We assume you are using the Crisp Go library, and that your backend is connected to both REST and RTM APIs.
Basic messaging bot
Let's build that basic bot, as described in (3) above. This bot will be very simple, as to introduce how to automate messaging with Crisp.
The bot works the following way:
It gets notified of incoming visitor messages from the Crisp RTM API channel (the session identifier; session_id is included in those notifications, which are necessary to reply)
It extracts the message text from the event, and checks if it matches the word "Hi"
If it matches "Hi", it replies with "Hello World" by sending a text message to the Crisp REST API
The Go code implementing this behavior is the following:
// Listen for sent messages from visitors
client.Events.Listen([]string{"message:send"}, func(reg *crisp.EventsRegister) {
fmt.Print("Now listening for events\n")
reg.On("message:send/text", func(evt crisp.EventsReceiveTextMessage) {
fmt.Print("Got message content:", *evt.Content)
if *evt.Content == "Hi" {
// Build message
message := crisp.ConversationTextMessageNew{Type: "text", From: "operator", Origin: "chat", Content: "Hello World", Fingerprint: 0}
// Message matches "Hi", we can send the reply
_, _, err := client.Website.SendTextMessageInConversation(*evt.WebsiteID, *evt.SessionID, message)
if err != nil {
fmt.Printf("Error: %s", err)
} else {
fmt.Print("Reply message sent to:", *evt.SessionID)
}
}
})
})
Updated on: 13/03/2024
Thank you!