Exposing the agent's information to the client

Now that we've implemented the chat agent, AgentCase, we need a way to expose Case's information to the client, notably, its name, title, and the path to its avatar image.

We create a new Rest API endpoint, GetAgentInfoEndpoint, to expose the chat agent's information to the client-side web application:

func GetAgentInfoEndpoint(env *common.Env, chatbot bot.Bot) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

m := make(map[string]string)
m["AgentName"] = chatbot.Name()
m["AgentTitle"] = chatbot.Title()
m["AgentThumbImagePath"] = chatbot.ThumbnailPath()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(m)
})

Note that in the signature of the GetAgentInfoEndpoint function, we accept the env object and the chatbot object. Note that the chatbot is of type bot.Bot, an interface type, rather than the AgentCase type. This provides us with the flexibility to easily swap in another bot, such as AgentMolly, instead of AgentCase, in the future.

We simply create a map, m, of type map[string]string, containing the bot's name, title, and avatar image path. We set a header to indicate that the server response will be in the JSON format. Finally, we write out the JSON encoded map using http.ResponseWriter, w.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.35.185