Documentation
¶
Overview ¶
Package chatbase provides a client and types for interacting with the Chatbase bot analytics platform - https://chatbase.com
The primary entrypoint for interacting with Chatbase is a `Client` instance:
// a client is a wrapper around an chatbase API key
client := chatbase.New("MY_CHATBASE_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)
}
For further usage instructions refer to the README.
Index ¶
- Constants
- Variables
- func SetAPITimeout(t time.Duration)
- func SetAPITransport(t http.RoundTripper)
- type Client
- func (c *Client) AgentMessage(userID, platform string) *Message
- func (c *Client) Event(userID, intent string) *Event
- func (c *Client) FacebookMessage(payload interface{}) *FacebookMessage
- func (c *Client) FacebookRequestResponse(request, response interface{}) *FacebookRequestResponse
- func (c *Client) Link(url, platform string) *Link
- func (c *Client) Message(typ MessageType, userID, platform string) *Message
- func (c *Client) String() string
- func (c *Client) Update(messageID string) *Update
- func (c *Client) UserMessage(userID, platform string) *Message
- type Event
- type EventProperty
- type Events
- type FacebookFields
- type FacebookMessage
- func (f FacebookMessage) MarshalJSON() ([]byte, error)
- func (f *FacebookMessage) SetFeedback(n bool) *FacebookMessage
- func (f *FacebookMessage) SetIntent(i string) *FacebookMessage
- func (f *FacebookMessage) SetNotHandled(n bool) *FacebookMessage
- func (f *FacebookMessage) SetVersion(v string) *FacebookMessage
- func (f *FacebookMessage) Submit() (*MessageResponse, error)
- func (f *FacebookMessage) SubmitWithContext(ctx context.Context) (*MessageResponse, error)
- type FacebookMessages
- type FacebookRequestResponse
- func (f *FacebookRequestResponse) SetFeedback(n bool) *FacebookRequestResponse
- func (f *FacebookRequestResponse) SetIntent(i string) *FacebookRequestResponse
- func (f *FacebookRequestResponse) SetNotHandled(n bool) *FacebookRequestResponse
- func (f *FacebookRequestResponse) SetVersion(v string) *FacebookRequestResponse
- func (f *FacebookRequestResponse) Submit() (*MessageResponse, error)
- func (f *FacebookRequestResponse) SubmitWithContext(ctx context.Context) (*MessageResponse, error)
- type FacebookRequestResponses
- func (f *FacebookRequestResponses) Append(addition ...*FacebookRequestResponse) *FacebookRequestResponses
- func (f FacebookRequestResponses) MarshalJSON() ([]byte, error)
- func (f *FacebookRequestResponses) Submit() (*MessagesResponse, error)
- func (f *FacebookRequestResponses) SubmitWithContext(ctx context.Context) (*MessagesResponse, error)
- type Link
- type LinkResponse
- type Message
- func (m *Message) SetFeedback(f bool) *Message
- func (m *Message) SetIntent(i string) *Message
- func (m *Message) SetMessage(msg string) *Message
- func (m *Message) SetNotHandled(n bool) *Message
- func (m *Message) SetTimeStamp(t int) *Message
- func (m *Message) SetVersion(v string) *Message
- func (m *Message) Submit() (*MessageResponse, error)
- func (m *Message) SubmitWithContext(ctx context.Context) (*MessageResponse, error)
- type MessageID
- type MessageResponse
- type MessageType
- type Messages
- type MessagesResponse
- type Status
- type Update
- func (u *Update) SetFeedback(f string) *Update
- func (u *Update) SetIntent(i string) *Update
- func (u *Update) SetNotHandled(n bool) *Update
- func (u *Update) SetVersion(v string) *Update
- func (u *Update) Submit() (*UpdateResponse, error)
- func (u *Update) SubmitWithContext(ctx context.Context) (*UpdateResponse, error)
- type UpdateResponse
Constants ¶
const ( PlatformFacebook = "Facebook" PlatformSMS = "SMS" PlatformWeb = "Web" PlatformAndroid = "Android" PlatformIOS = "iOS" PlatformActions = "Actions" PlatformAlexa = "Alexa" PlatformCortana = "Cortana" PlatformKik = "Kik" PlatformSkype = "Skype" PlatformTwitter = "Twitter" PlatformViber = "Viber" PlatformTelegram = "Telegram" PlatformSlack = "Slack" PlatformWhatsApp = "WhatsApp" PlatformWeChat = "WeChat" PlatformLine = "Line" PlatformKakao = "Kakao" )
A set of strings that can be used for specifying a message's platform. These platforms will be recognized by Chatbase, but any other non-zero custom string value like "Workplace" can be used as well
Variables ¶
var TimeStamp = func() int { return int(time.Now().Unix()) }
TimeStamp returns the current time in UNIX milliseconds
Functions ¶
func SetAPITimeout ¶
SetAPITimeout allows setting a timeout value for the http.Client that is being used for handling Chatbase API calls
func SetAPITransport ¶
func SetAPITransport(t http.RoundTripper)
SetAPITransport allows setting a transport for the http.Client that is being used for handling Chatbase API calls
Types ¶
type Client ¶
type Client string
Client wraps a Chatbase API Key and can be used to generate messages, events and link
func (*Client) AgentMessage ¶
AgentMessage is a convenience method for creating an agent created message
func (*Client) FacebookMessage ¶
func (c *Client) FacebookMessage(payload interface{}) *FacebookMessage
FacebookMessage creates a new native Facebook message
func (*Client) FacebookRequestResponse ¶
func (c *Client) FacebookRequestResponse(request, response interface{}) *FacebookRequestResponse
FacebookRequestResponse creates a new wrapper around a request and response
func (*Client) Message ¶
func (c *Client) Message(typ MessageType, userID, platform string) *Message
Message returns a new Message using the client's key and the current time as its "TimeStamp" value
func (*Client) UserMessage ¶
UserMessage is a convenience method for creating a user created message
type Event ¶
type Event struct {
APIKey string `json:"api_key"`
UserID string `json:"user_id"`
Intent string `json:"intent"`
TimeStamp int `json:"timestamp_millis,omitempty"`
Platform string `json:"platform,omitempty"`
Version string `json:"version,omitempty"`
Properties []EventProperty `json:"properties"`
}
Event contains data about an event
func (*Event) AddProperty ¶
AddProperty adds a new property to the event using the given name and value. The passed value needs to be one of `int`, `string`, `bool` or `float64`
func (*Event) SetPlatform ¶
SetPlatform adds an optional "platform" value to the event
func (*Event) SetTimeStamp ¶
SetTimeStamp adds an optional "timestamp" value to the event
func (*Event) SetVersion ¶
SetVersion adds an optional "version" value to the event
type EventProperty ¶
type EventProperty struct {
Name string `json:"property_name"`
StringValue string `json:"string_value,omitempty"`
IntegerValue int `json:"integer_value,omitempty"`
FloatValue float64 `json:"float_value,omitempty"`
BoolValue bool `json:"bool_value,omitempty"`
}
EventProperty is a property that is attached to an event
func NewEventProperty ¶
func NewEventProperty(name string, value interface{}) (EventProperty, error)
NewEventProperty generates an EventProperty containing the correctly typed field for the passed value. The passed value needs to be one of `int`, `string`, `bool` or `float64`
type Events ¶
type Events []Event
Events is a collection of Event
func (*Events) Append ¶
Append adds events to the the collection. The collection should not contain events using different API keys
func (Events) MarshalJSON ¶
MarshalJSON ensure the collection is correctly wrapped into an object and added the api_key value
type FacebookFields ¶
type FacebookFields struct {
Intent string `json:"intent"`
NotHandled bool `json:"not_handled"`
Feedback bool `json:"feedback,omitempty"`
Version string `json:"version,omitempty"`
}
FacebookFields contains metadata about a native Facebook message
type FacebookMessage ¶
type FacebookMessage struct {
Fields *FacebookFields
Payload interface{}
APIKey string
}
FacebookMessage is a single native Facebook message
func (FacebookMessage) MarshalJSON ¶
func (f FacebookMessage) MarshalJSON() ([]byte, error)
MarshalJSON ensures the message is merged with the metadata in the way that Chatbase expects it to be
func (*FacebookMessage) SetFeedback ¶
func (f *FacebookMessage) SetFeedback(n bool) *FacebookMessage
SetFeedback adds an optional "feedback" value to the message
func (*FacebookMessage) SetIntent ¶
func (f *FacebookMessage) SetIntent(i string) *FacebookMessage
SetIntent adds an optional "intent" value to the message
func (*FacebookMessage) SetNotHandled ¶
func (f *FacebookMessage) SetNotHandled(n bool) *FacebookMessage
SetNotHandled adds an optional "not handled" value to the message
func (*FacebookMessage) SetVersion ¶
func (f *FacebookMessage) SetVersion(v string) *FacebookMessage
SetVersion adds an optional "version" value to the message
func (*FacebookMessage) Submit ¶
func (f *FacebookMessage) Submit() (*MessageResponse, error)
Submit tries to deliver a single Facebook message to chatbase
func (*FacebookMessage) SubmitWithContext ¶
func (f *FacebookMessage) SubmitWithContext(ctx context.Context) (*MessageResponse, error)
SubmitWithContext tries to deliver a single Facebook message to chatbase considering the given context's deadline
type FacebookMessages ¶
type FacebookMessages []FacebookMessage
FacebookMessages is a collection of FacecbookMessage
func (*FacebookMessages) Append ¶
func (f *FacebookMessages) Append(addition ...*FacebookMessage) *FacebookMessages
Append adds additional messages to the collection. The collection should not contain messages using different API keys
func (FacebookMessages) MarshalJSON ¶
func (f FacebookMessages) MarshalJSON() ([]byte, error)
MarshalJSON ensures the messages are wrapped in a top-level object before being serialized into the payload
func (*FacebookMessages) Submit ¶
func (f *FacebookMessages) Submit() (*MessagesResponse, error)
Submit tries to deliver the set of messages to Chatbase. The collection cannot contain messages using different API keys
func (*FacebookMessages) SubmitWithContext ¶
func (f *FacebookMessages) SubmitWithContext(ctx context.Context) (*MessagesResponse, error)
SubmitWithContext tries to deliver a single Facebook message to chatbase considering the given context's deadline
type FacebookRequestResponse ¶
type FacebookRequestResponse struct {
APIKey string `json:"-"`
Request interface{} `json:"request_body"`
Response interface{} `json:"response_body"`
Fields *FacebookFields `json:"chatbase_fields"`
}
FacebookRequestResponse is a payload that contains both request and response data
func (*FacebookRequestResponse) SetFeedback ¶
func (f *FacebookRequestResponse) SetFeedback(n bool) *FacebookRequestResponse
SetFeedback adds an optional "feedback" value to the pair
func (*FacebookRequestResponse) SetIntent ¶
func (f *FacebookRequestResponse) SetIntent(i string) *FacebookRequestResponse
SetIntent adds an optional "intent" value to the pair
func (*FacebookRequestResponse) SetNotHandled ¶
func (f *FacebookRequestResponse) SetNotHandled(n bool) *FacebookRequestResponse
SetNotHandled adds an optional "not handled" value to the pair
func (*FacebookRequestResponse) SetVersion ¶
func (f *FacebookRequestResponse) SetVersion(v string) *FacebookRequestResponse
SetVersion adds an optional "version" value to the pair
func (*FacebookRequestResponse) Submit ¶
func (f *FacebookRequestResponse) Submit() (*MessageResponse, error)
Submit tries to deliver the pair to Chatbase
func (*FacebookRequestResponse) SubmitWithContext ¶
func (f *FacebookRequestResponse) SubmitWithContext(ctx context.Context) (*MessageResponse, error)
SubmitWithContext tries to deliver the pair to Chatbase considering the given context's deadline
type FacebookRequestResponses ¶
type FacebookRequestResponses []FacebookRequestResponse
FacebookRequestResponses is a collection of FacebookRequestResponse
func (*FacebookRequestResponses) Append ¶
func (f *FacebookRequestResponses) Append(addition ...*FacebookRequestResponse) *FacebookRequestResponses
Append adds additional messages to the collection. The collection should not contain messages using different API keys
func (FacebookRequestResponses) MarshalJSON ¶
func (f FacebookRequestResponses) MarshalJSON() ([]byte, error)
MarshalJSON ensures the messages are wrapped in a top-level object before being serialized into the payload
func (*FacebookRequestResponses) Submit ¶
func (f *FacebookRequestResponses) Submit() (*MessagesResponse, error)
Submit tries to send the collection of request/response pairs to Chatbase. The collection should not contain messages using different API keys
func (*FacebookRequestResponses) SubmitWithContext ¶
func (f *FacebookRequestResponses) SubmitWithContext(ctx context.Context) (*MessagesResponse, error)
SubmitWithContext tries to send the collection of request/response pairs to Chatbase considering the given context's deadline
type Link ¶
type Link struct {
APIKey string `json:"api_key"`
URL string `json:"url"`
Platform string `json:"platform"`
Version string `json:"version,omitempty"`
}
Link describes a hyperlink to be tracked using Chatbase
func (*Link) SetVersion ¶
SetVersion adds an optional "version" parameter to a link
func (*Link) Submit ¶
func (l *Link) Submit() (*LinkResponse, error)
Submit tries to send the link to Chatbase
func (*Link) SubmitWithContext ¶
func (l *Link) SubmitWithContext(ctx context.Context) (*LinkResponse, error)
SubmitWithContext tries to send the link to Chatbase while considering the given context's deadline
type LinkResponse ¶
LinkResponse contains the response to submitting a link
type Message ¶
type Message struct {
APIKey string `json:"api_key"`
Type MessageType `json:"type"`
UserID string `json:"user_id"`
TimeStamp int `json:"time_stamp"`
Platform string `json:"platform"`
Message string `json:"message,omitempty"`
Intent string `json:"intent,omitempty"`
NotHandled bool `json:"not_handled,omitempty"`
Feedback bool `json:"feedback,omitempty"`
Version string `json:"version,omitempty"`
}
Message contains data about a generic chat message
func (*Message) SetFeedback ¶
SetFeedback adds an optional "feedback" value to a message
func (*Message) SetMessage ¶
SetMessage adds an optional "message" value to a message
func (*Message) SetNotHandled ¶
SetNotHandled adds an optional "not handled" value to a message
func (*Message) SetTimeStamp ¶
SetTimeStamp overrides the message's "timestamp" value
func (*Message) SetVersion ¶
SetVersion adds an optional "version" value to a message
func (*Message) Submit ¶
func (m *Message) Submit() (*MessageResponse, error)
Submit tries to deliver the message to Chatbase
func (*Message) SubmitWithContext ¶
func (m *Message) SubmitWithContext(ctx context.Context) (*MessageResponse, error)
SubmitWithContext tries to deliver the message to Chatbase while considering the given context's deadline
type MessageID ¶
type MessageID string
MessageID is a named string type that normalizes the IDs sent by Chatbase
func (*MessageID) UnmarshalJSON ¶
UnmarshalJSON distinguishes ints and strings and normalizes both values into a string representation
type MessageResponse ¶
type MessageResponse struct {
MessageID MessageID `json:"message_id"`
Status Status `json:"status"`
Reason string `json:"reason,omitempty"`
}
MessageResponse describes a Chatbase response to the submission of a single message. It is also used to represent the result of an item of a collection of messages that have been submitted.
type MessageType ¶
type MessageType string
MessageType describes the source of a message
const ( UserType MessageType = "user" AgentType MessageType = "agent" )
Types of messages used to identify the origin of a message in Chatbase
type Messages ¶
type Messages []Message
Messages is a collection of Message
func (Messages) MarshalJSON ¶
MarshalJSON ensures the list of messages is wrapped in a top-level object before being serialized into JSON
func (*Messages) Submit ¶
func (m *Messages) Submit() (*MessagesResponse, error)
Submit tries to deliver the set of messages to Chatbase
func (*Messages) SubmitWithContext ¶
func (m *Messages) SubmitWithContext(ctx context.Context) (*MessagesResponse, error)
SubmitWithContext tries to deliver the set of messages to Chatbase while considering the given context's deadline
type MessagesResponse ¶
type MessagesResponse struct {
AllSucceeded bool `json:"all_succeeded"`
Status Status `json:"status"`
Responses []MessageResponse `json:"responses"`
Reason string `json:"reason,omitempty"`
}
MessagesResponse describes a Chatbase response to submitting a set of messages
type Status ¶
type Status bool
Status describes the success of an operation
func (*Status) UnmarshalJSON ¶
UnmarshalJSON normalizes the int and string values that are being used by Chatbase to represent success or failure. Int values will map to HTTP status codes (200 or 400), otherwise "success" is considered true where "failure" is considered false
type Update ¶
type Update struct {
APIKey string `json:"-"`
MessageID MessageID `json:"-"`
Intent string `json:"intent,omitempty"`
NotHandled string `json:"not_handled,omitempty"`
Feedback string `json:"feedback,omitempty"`
Version string `json:"version,omitempty"`
}
Update contains data about an existing message that should be updated
func (*Update) SetFeedback ¶
SetFeedback adds an optional "feedback" value to an update
func (*Update) SetNotHandled ¶
SetNotHandled adds an optional "not handled" value to an update
func (*Update) SetVersion ¶
SetVersion adds an optional "version" value to an update
func (*Update) Submit ¶
func (u *Update) Submit() (*UpdateResponse, error)
Submit tries to deliver the update to Chatbase
func (*Update) SubmitWithContext ¶
func (u *Update) SubmitWithContext(ctx context.Context) (*UpdateResponse, error)
SubmitWithContext tries to deliver the update to Chatbase while considering the given context's deadline