Skip to content

Instantly share code, notes, and snippets.

@circa10a
Created February 9, 2020 23:47
Show Gist options
  • Save circa10a/290e764762a3873077564ed03469aaa6 to your computer and use it in GitHub Desktop.
Save circa10a/290e764762a3873077564ed03469aaa6 to your computer and use it in GitHub Desktop.
package main
import (
"net/http"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
// Modeled after google docs
// https://developers.google.com/assistant/conversational/responses#browsing_carousel
// Response is the entire JSON payload response
type Response struct {
Payload Payload `json:"payload"`
}
// Payload is a google defined higher structure, see https://developers.google.com/assistant/conversational/responses#browsing_carousel
type Payload struct {
Google Google `json:"google"`
}
// Google is another google defined higher structure, see https://developers.google.com/assistant/conversational/responses#browsing_carousel
type Google struct {
ExpectUserResponse bool `json:"expectUserResponse"`
RichResponse RichResponse `json:"richResponse,omitempty"`
}
// RichResponse gives UI representation of the news data fetched
type RichResponse struct {
Items []Item `json:"items,omitempty"`
}
// Item provides different interactions
type Item struct {
SimpleResponse *SimpleResponse `json:"simpleResponse,omitempty"`
CarouselBrowse *CarouselBrowse `json:"carouselBrowse,omitempty"`
}
// Simple response provides audio only feedback
type SimpleResponse struct {
TextToSpeech string `json:"textToSpeech"`
}
// CarouselBrowse provides a UI list of items with hyperlinks
type CarouselBrowse struct {
Items []CarouselItem `json:"items"`
}
// CarouselItem is a UI entry for each news item
type CarouselItem struct {
Title string `json:"title"`
OpenURLAction OpenURLAction `json:"openUrlAction"`
Description string `json:"description,omitempty"`
}
// OpenURLAction provides a url to be opened in the client's browser when touched
type OpenURLAction struct {
URL string `json:"url"`
}
func buildFulfillment() *Response {
return &Response{
Payload{
Google{
ExpectUserResponse: false,
RichResponse: RichResponse{
Items: []Item{
{
SimpleResponse: &SimpleResponse{
TextToSpeech: "This is the default audio response",
},
},
{
CarouselBrowse: &CarouselBrowse{
Items: []CarouselItem{
CarouselItem{
Title: "Title1",
Description: "Description1",
OpenURLAction: OpenURLAction{
URL: "https://example.com/1",
},
},
CarouselItem{
Title: "Title2",
Description: "Description2",
OpenURLAction: OpenURLAction{
URL: "https://example.com/2",
},
},
},
},
},
},
},
},
},
}
}
func handleWebhook(c *gin.Context) {
c.JSON(http.StatusOK, buildFulfillment())
}
func main() {
var err error
r := gin.Default()
r.POST("/webhook", handleWebhook)
if err = r.Run(); err != nil {
log.WithError(err).Fatal("Couldn't start server")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment