All Projects → leboncoin → dialogflow-go-webhook

leboncoin / dialogflow-go-webhook

Licence: MIT license
Simple package to create DialogFlow v2 webhooks using Go

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to dialogflow-go-webhook

home assistant appdaemon alexa google
An AppDaemon application for Home Assistant, handles calls from Alexa Skill and a Google Action
Stars: ✭ 11 (-52.17%)
Mutual labels:  dialogflow, dialogflow-v2
convoworks-core
PHP framework for handling conversational services like Amazon Alexa skills, Google Assistant, Viber, FB messenger ...
Stars: ✭ 20 (-13.04%)
Mutual labels:  dialogflow
AngularAI
💬 Angular 6 AI (localhost version is working correctly)
Stars: ✭ 50 (+117.39%)
Mutual labels:  dialogflow
dialogflow-watchnow-messenger
WatchNow FB Messenger bot with DialogFlow & Golang 💬
Stars: ✭ 24 (+4.35%)
Mutual labels:  dialogflow
untoitpourcaramel
#UnToitPourCaramel – Outil agrégeant les annonces immobilières dans un tableau Trello
Stars: ✭ 44 (+91.3%)
Mutual labels:  leboncoin
uniFlow
Full Docs -->
Stars: ✭ 26 (+13.04%)
Mutual labels:  dialogflow
dialogflow-gateway-docs
📄 Docs for Dialogflow Gateway
Stars: ✭ 20 (-13.04%)
Mutual labels:  dialogflow
React.ai
It recognize your speech and trained AI Bot will respond(i.e Customer Service, Personal Assistant) using Machine Learning API (DialogFlow, apiai), Speech Recognition, GraphQL, Next.js, React, redux
Stars: ✭ 38 (+65.22%)
Mutual labels:  dialogflow
Hobo-Sapiens
A telegram bot to automatically search for apartments for rent
Stars: ✭ 37 (+60.87%)
Mutual labels:  leboncoin
simple-ansible-inventory
A simple, clean and easily readable Ansible inventory
Stars: ✭ 25 (+8.7%)
Mutual labels:  leboncoin
React-Chatbot
A DialogFlow Chatbot made using NodeJS and React.
Stars: ✭ 36 (+56.52%)
Mutual labels:  dialogflow
WhatsAppBotTut
Tutorial to create WhatsApp Bot using Twilio and Python
Stars: ✭ 131 (+469.57%)
Mutual labels:  dialogflow
scrapy-LBC
Araignée LeBonCoin avec Scrapy et ElasticSearch
Stars: ✭ 14 (-39.13%)
Mutual labels:  leboncoin
terraform-aws-nvme-example
Example to manage your EBS NVME volumes on AWS
Stars: ✭ 46 (+100%)
Mutual labels:  leboncoin
Briefly
source based news in short : Winner @MumbaiHackathon 2018
Stars: ✭ 35 (+52.17%)
Mutual labels:  dialogflow
dialogflow ros
A ROS wrapper for Google's NLP platform Dialogflow
Stars: ✭ 17 (-26.09%)
Mutual labels:  dialogflow
stumblybot
Simple robot that executes voice commands through Google Assistant.
Stars: ✭ 23 (+0%)
Mutual labels:  dialogflow
small-talk-rasa-stack
Collection of casual conversations that can be used with the Rasa Stack
Stars: ✭ 87 (+278.26%)
Mutual labels:  dialogflow
benchmark-nlp
NLP benchmark test sentences and full results
Stars: ✭ 13 (-43.48%)
Mutual labels:  dialogflow
doc2vec-api
document embedding and machine learning script for beginners
Stars: ✭ 92 (+300%)
Mutual labels:  dialogflow

Dialogflow Go Webhook

Go Version Go Version Go Report Card Build Status codecov License Godoc No Maintenance Intended

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.

Deprecation Notice

This project is no longer useful since the release of the Go SDK for Dialogflow's v2 API. See this article for a tutorial on how to use the protobuf definition to handle webhook.

Table of Content

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 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

In this section we'll use the gin router as it has some nice helper functions that will keep the code concise. For an example using the standard http router, see this example.

When DialogFlow sends a request to your webhook, you can unmarshal the incoming data to a df.Request. This, however, will not unmarshal the contexts and the parameters because those are completely dependent on your data models.

package main

import (
	"net/http"

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

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

func webhook(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", webhook)
	if err := r.Run("127.0.0.1:8001"); err != nil {
		panic(err)
	}
}

Retrieving params and contexts

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

func webhook(c *gin.Context) {
	var err error
	var dfr *df.Request
	var p params

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

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

In this example we're getting the DialogFlow request and unmarshalling the params to a defined struct. This is why json.RawMessage is used in both the Request.QueryResult.Parameters and in the Request.QueryResult.Contexts.

This also allows you to filter and route according to the action and intent DialogFlow detected, which means that depending on which action you detected, you can unmarshal the parameters and contexts to a completely different data structure.

The same thing can be done for contexts :

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

func webhook(c *gin.Context) {
	var err error
	var dfr *df.Request
	var p params

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

	if err = dfr.GetContext("my-awesome-context", &p); err != nil {
		c.AbortWithStatus(http.StatusBadRequest)
		return
	}
}

Responding with a fulfillment

DialogFlow expects you to respond with what is called a fulfillment.

This package supports every rich response type.

func webhook(c *gin.Context) {
	var err error
	var dfr *df.Request
	var p params

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

	// Send back a fulfillment
	dff := &df.Fulfillment{
		FulfillmentMessages: df.Messages{
			df.ForGoogle(df.SingleSimpleResponse("hello", "hello")),
			{RichMessage: df.Text{Text: []string{"hello"}}},
		},
	}
	c.JSON(http.StatusOK, dff)
}

Examples

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].