Golang client for interacting with the chatbase.com bot analytics API
Use go get
:
$ go get github.com/m90/go-chatbase
or if you're using go modules, just specify:
import "github.com/m90/go-chatbase/v2"
Send a single message to Chatbase:
// a client is a wrapper around an chatbase API key
client := chatbase.New("MY-API-KEY")
// calling Message requires passing of all required values
message := client.Message(chatbase.MessageTypeAgent, "USER-ID", chatbase.PlatformTelegram)
// optional values are added after creation
message.SetMessage("I didn't understand that, sorry")
// this can also be chained
message.SetIntent("fallback").SetNotHandled(true)
// calling Submit will send the data to chatbase
response, err := message.Submit()
if err != nil {
// an error submitting the data occurred
fmt.Println(err)
} else if !response.Status.OK() {
// the data was submitted to ChatBase, but
// the response contained an error code
fmt.Println(response.Reason)
}
The generic message API allows handling of Message
, Messages
and Update
types.
message := client.Message(chatbase.UserType, "USER-ID", "messenger")
message.SetMessage("How are you today?")
response, err := message.Submit()
if err != nil {
// an error submitting the data occurred
fmt.Println(err)
} else if !response.Status.OK() {
// the data was submitted to ChatBase, but
// the response contained an error code
fmt.Println(response.Reason)
}
messages := chatbase.Messages{}
for i := 1; i < 4; i++ {
message := client.AgentMessage("USER-ID", "messenger")
message.SetMessage(fmt.Sprintf("Counting: %d", i))
messages.Append(message)
}
response, err := messages.Submit()
if err != nil {
// an error submitting the data occurred
fmt.Println(err)
} else if !response.Status.OK() {
// the data was submitted to ChatBase, but
// the response contained an error code
fmt.Println(response.Reason)
}
update := client.Update("ID-OF-MESSAGE-TO-UPDATE")
update.SetIntent("this-changed")
response, err := update.Submit()
if err != nil {
// an error submitting the data occurred
fmt.Println(err)
} else if !response.Status.OK() {
// the data was submitted to ChatBase, but
// the response contained an error code
fmt.Println(response.Reason)
}
The Facebook Message API allows handling of FacebookMessage
, FacebookMessages
, FacebookRequestResponse
and FacebookRequestResponses
types.
message := client.FacebookMessage(facebookPayload)
message.SetIntent("test-messenger")
response, err := message.Submit()
if err != nil {
// an error submitting the data occurred
fmt.Println(err)
} else if !response.Status.OK() {
// the data was submitted to ChatBase, but
// the response contained an error code
fmt.Println(response.Reason)
}
messages := chatbase.FacebookMessages{}
for _, msg := range listOfFacebookMessages {
message := client.FacebookMessage(msg).SetVersion("0.0.1-beta")
messages.Append(message)
}
response, err := messages.Submit()
if err != nil {
// an error submitting the data occurred
fmt.Println(err)
} else if !response.Status.OK() {
// the data was submitted to ChatBase, but
// the response contained an error code
fmt.Println(response.Reason)
}
pair := client.FacebookRequestResponse(incomingMessage, respondingMessage)
pair.SetIntent("test-messenger")
response, err := pair.Submit()
if err != nil {
// an error submitting the data occurred
fmt.Println(err)
} else if !response.Status.OK() {
// the data was submitted to ChatBase, but
// the response contained an error code
fmt.Println(response.Reason)
}
pairs := chatbase.FacebookRequestResponses{}
for _, msg := range listOfFacebookMessages {
pair := client.FacebookRequestResponse(msg.request, msg.response).SetVersion("0.0.1-beta")
pairs.Append(pair)
}
response, err := pairs.Submit()
if err != nil {
// an error submitting the data occurred
fmt.Println(err)
} else if !response.Status.OK() {
// the data was submitted to ChatBase, but
// the response contained an error code
fmt.Println(response.Reason)
}
The Events API allows handling of Event
and Events
types.
event := client.Event("USER-ID", "intent-name")
event.SetPlatform("line").SetVersion("2.2.1")
event.AddProperty("success", true)
if err != nil {
// an error submitting the data occurred
fmt.Println(err)
} else if !response.Status.OK() {
// the data was submitted to ChatBase, but
// the response contained an error code
fmt.Println(response.Reason)
}
events := chatbase.Events{}
for i := 0; i < 99; i ++ {
event := client.Event("USER-ID", "counting-intent")
event.AddProperty("is-even", i%2 == 0)
events.Append(event)
}
if err := events.Submit(); err != nil {
// handle error
}
The link tracking allows handling of Link
types.
link := client.Link("https://golang.org/", chatbase.PlatformLine)
trackableHREF, err := link.Encode()
response, err := link.Submit()
if err != nil {
// an error submitting the data occurred
fmt.Println(err)
} else if !response.Status.OK() {
// the data was submitted to ChatBase, but
// the response contained an error code
fmt.Println(response.Reason)
}
MIT © Frederik Ring