Skip to content
Permalink
Browse files

WIP on examples and documentation

  • Loading branch information...
Depado committed Mar 14, 2018
1 parent 42eb0cc commit d3c24c02f1a1081dbcdb7285272b337625a67a8f
Showing with 134 additions and 5 deletions.
  1. +50 −0 README.md
  2. +5 −5 dialogflow.go
  3. +30 −0 examples/gin/main.go
  4. +49 −0 examples/http/main.go
@@ -14,5 +14,55 @@ Simple library to create compatible DialogFlow v2 webhooks using Go.
This package is only intended to create webhooks, it doesn't implement the whole
DialogFlow API.

## Introduction

### Goal of this package

This package aims to implement a complete way to receive a DialogFlow payload,
parse it, and retrieve data stored in the parameters and contexts by providing
your own data structures.

It also allows you to format your response properly, the way DialogFlow expects
it, including all the message types and platforms. (Such as cards, carousels,
quick replies, etc…)

### Disclaimer

As DialogFlow's v2 API is still in Beta, there may be breaking changes. If
something breaks, please file an issue.

## Installation

### Using dep

If you're using [dep](https://github.com/golang/dep) which is the recommended
way to vendor your dependencies in your project, simply run this command :

`dep ensure -add github.com/leboncoin/dialogflow-go-webhook`

### Using go get

If your project isn't using `dep` yet, you can use `go get` to install this
package :

`go get github.com/leboncoin/dialogflow-go-webhook`

## Usage

Import the package `github.com/leboncoin/dialogflow-go-webhook` and use it with
the `dialogflow` package name. To make your code cleaner, import it as `df`.
All the following examples and usages use the `df` notation.

### Handling incoming request

When DialogFlow sends a request to your webhook, you can unmarshal the incoming
data to a `df.Request`.

### Responding with a fulfillment

## Examples

- [Using Gin](https://github.com/leboncoin/dialogflow-go-webhook/blob/master/examples/gin)
- [Using http](https://github.com/leboncoin/dialogflow-go-webhook/blob/master/examples/http)


@@ -7,9 +7,9 @@ import (
"strings"
)

// Response is the top-level struct holding all the information
// Request is the top-level struct holding all the information
// Basically links a response ID with a query result.
type Response struct {
type Request struct {
Session string `json:"session,omitempty"`
ResponseID string `json:"responseId,omitempty"`
QueryResult QueryResult `json:"queryResult,omitempty"`
@@ -18,12 +18,12 @@ type Response struct {

// GetParams simply unmarshals the parameters to the given struct and returns
// an error if it's not possible
func (rw *Response) GetParams(i interface{}) error {
func (rw *Request) GetParams(i interface{}) error {
return json.Unmarshal(rw.QueryResult.Parameters, &i)
}

// GetContext allows to search in the output contexts of the query
func (rw *Response) GetContext(ctx string, i interface{}) error {
func (rw *Request) GetContext(ctx string, i interface{}) error {
for _, c := range rw.QueryResult.OutputContexts {
if strings.HasSuffix(c.Name, ctx) {
return json.Unmarshal(c.Parameters, &i)
@@ -34,7 +34,7 @@ func (rw *Response) GetContext(ctx string, i interface{}) error {

// NewContext is a helper function to create a new named context with params
// name and a lifespan
func (rw *Response) NewContext(name string, lifespan int, params interface{}) (*Context, error) {
func (rw *Request) NewContext(name string, lifespan int, params interface{}) (*Context, error) {
var err error
var b []byte

@@ -0,0 +1,30 @@
package main

import (
"net/http"

"github.com/gin-gonic/gin"
df "github.com/leboncoin/dialogflow-go-webhook"
)

type MyParams struct {
City string `json:"city"`
Gender string `json:"gender"`
Age int `json:"age"`
}

func HandleWebhook(c *gin.Context) {
var err error
var dfr *df.Request

if err = c.BindJSON(&dfr); err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}

}

func main() {
r := gin.Default()
r.POST("/webhook")
}
@@ -0,0 +1,49 @@
package main

import (
"encoding/json"
"log"
"net/http"

df "github.com/leboncoin/dialogflow-go-webhook"
)

type params struct {
City string `json:"city"`
Gender string `json:"gender"`
Age int `json:"age"`
}

func webhook(rw http.ResponseWriter, req *http.Request) {
var err error
var dfr *df.Request
var p params

decoder := json.NewDecoder(req.Body)
if err = decoder.Decode(&dfr); err != nil {
rw.WriteHeader(http.StatusBadRequest)
return
}
defer req.Body.Close()

// Filter on action, using a switch for example

// Retrieve the params of the request
if err = dfr.GetParams(&p); err != nil {
rw.WriteHeader(http.StatusBadRequest)
return
}

// Retrieve a specific context
if err = dfr.GetContext("my-awesome-context", &p); err != nil {
rw.WriteHeader(http.StatusBadRequest)
return
}

// Do things with the context you just retrieved
}

func main() {
http.HandleFunc("/webhook", webhook)
log.Fatal(http.ListenAndServe(":8082", nil))
}

0 comments on commit d3c24c0

Please sign in to comment.
You can’t perform that action at this time.