Skip to content

Commit

Permalink
DialogFlow article
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Lhussiez committed Mar 20, 2018
1 parent dd5a1b5 commit 57b6b8c
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 6 deletions.
Expand Up @@ -46,7 +46,7 @@ type FullDrink struct {

// FullDrinkList contains a slice of FullDrink
type FullDrinkList struct {
Drinks []FullDrink `json:"drinks"`
Drinks []*FullDrink `json:"drinks"`
}

// Drink is a minimal representation of a FullDrink
Expand All @@ -58,5 +58,5 @@ type Drink struct {

// DrinkList contains a slice of Drink
type DrinkList struct {
Drinks []Drink `json:"drinks"`
Drinks []*Drink `json:"drinks"`
}
84 changes: 84 additions & 0 deletions code/dialogflow/cocktail/query.go
@@ -0,0 +1,84 @@
package cocktail

import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/url"
"time"
)

// Client is the Cocktail API client structure
type Client struct {
BaseURL *url.URL
HTTPClient *http.Client
}

// C is the exported default client
var C = Client{
BaseURL: &url.URL{
Host: "www.thecocktaildb.com",
Path: "/api/json/v1/1/",
Scheme: "https",
},
HTTPClient: &http.Client{
Timeout: time.Second * 10,
},
}

func (c *Client) newRequest(method, path string, body interface{}) (*http.Request, error) {
rel := &url.URL{Path: path}
u := c.BaseURL.ResolveReference(rel)

var buf io.ReadWriter
if body != nil {
buf = new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(body)
if err != nil {
return nil, err
}
}

req, err := http.NewRequest(method, u.String(), buf)
if err != nil {
return nil, err
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
req.Header.Set("Accept", "application/json")

return req, nil
}

func (c *Client) do(req *http.Request, v interface{}) (*http.Response, error) {
var err error
var resp *http.Response

if resp, err = c.HTTPClient.Do(req); err != nil {
return resp, err
}
defer resp.Body.Close()

err = json.NewDecoder(resp.Body).Decode(v)
return resp, err
}

// GetRandomDrink returns a single random FullDrink object
func (c *Client) GetRandomDrink() (*FullDrink, error) {
var err error
var req *http.Request
var d *FullDrink
var ds *FullDrinkList

if req, err = c.newRequest("GET", "random.php", nil); err != nil {
return d, err
}

_, err = c.do(req, &ds)
if len(ds.Drinks) > 0 {
d = ds.Drinks[0]
}
return d, err
}
22 changes: 21 additions & 1 deletion code/dialogflow/main.go
@@ -1,9 +1,13 @@
package main

import (
"fmt"
"log"
"net/http"

"github.com/sirupsen/logrus"

"github.com/Depado/articles/code/dialogflow/cocktail"
"github.com/gin-gonic/gin"
df "github.com/leboncoin/dialogflow-go-webhook"
)
Expand All @@ -13,7 +17,23 @@ func search(c *gin.Context, dfr *df.Request) {
}

func random(c *gin.Context, dfr *df.Request) {
c.JSON(http.StatusOK, gin.H{})
var err error
var d *cocktail.FullDrink

if d, err = cocktail.C.GetRandomDrink(); err != nil {
logrus.WithError(err).Error("Coudln't get random drink")
c.AbortWithStatus(http.StatusInternalServerError)
return
}

out := fmt.Sprintf("I found that cocktail : %s", d.StrDrink)
dff := &df.Fulfillment{
FulfillmentMessages: df.Messages{
{RichMessage: df.Text{Text: []string{out}}},
df.ForGoogle(df.SingleSimpleResponse(out, out)),
},
}
c.JSON(http.StatusOK, dff)
}

func webhook(c *gin.Context) {
Expand Down
Binary file added pages/assets/dialogflow/dialogflow-logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 0 additions & 2 deletions pages/assets/dialogflow/webhook-flow.svg

This file was deleted.

0 comments on commit 57b6b8c

Please sign in to comment.