Skip to content
This repository has been archived by the owner on Feb 7, 2023. It is now read-only.
/ go-chatbase Public archive

Golang client for interacting with the chatbase.com API

License

Notifications You must be signed in to change notification settings

m90/go-chatbase

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-chatbase

Build Status godoc

Golang client for interacting with the chatbase.com bot analytics API

Installation

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"

Example

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)
}

Supported APIs

Generic message API

The generic message API allows handling of Message, Messages and Update types.

Message

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

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

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)
}

Facebook Message API

The Facebook Message API allows handling of FacebookMessage, FacebookMessages, FacebookRequestResponse and FacebookRequestResponses types.

FacebookMessage

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)
}

FacebookMessages

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)
}

FacebookRequestResponse

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)
}

FacebookRequestResponses

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)
}

Events API

The Events API allows handling of Event and Events types.

Event

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

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
}

Link tracking API

The link tracking allows handling of Link types.

Link

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)
}

License

MIT © Frederik Ring