1

I was following the step on this website but when I try to push on Heroku and connect to it with Dialogflow Fulfillment, my website page always says 404 not found.

The heroku logs always show this:

2020-01-17T15:53:17.864991+00:00 heroku[router]: at=error code=H10 desc="App crashed" 
method=POST path="/webhook" host=herokugit-test.herokuapp.com request_id=bfed8942-7009-474c-a67c-b84b5e79a94f fwd="64.233.172.43" dyno= connect= service= status=503 bytes= 
protocol=https

Here is the code:

package main

import (

    "fmt"
    "net/http"
    "github.com/gin-gonic/gin"
    "github.com/golang/protobuf/jsonpb"
    "github.com/sirupsen/logrus"
    "google.golang.org/genproto/googleapis/cloud/dialogflow/v2"
)

func handleWebhook(c *gin.Context) {
    var err error

    wr := dialogflow.WebhookRequest{}
    if err = jsonpb.Unmarshal(c.Request.Body, &wr); err != nil {
        logrus.WithError(err).Error("Couldn't Unmarshal request to jsonpb")
        c.Status(http.StatusBadRequest)
        return
    }
    fmt.Println(wr.GetQueryResult().GetOutputContexts())
}

func main() {

    var err error

    r := gin.Default()
    r.POST("/webhook", handleWebhook)

    if err = r.Run("127.0.0.1:8080"); err != nil {
        logrus.WithError(err).Fatal("Couldn't start server")
    }
}

How to fix it? And how to show the right WebhookRequest on my website page?

Thanks a lot for helping me.

Ullaakut
  • 3,554
  • 2
  • 19
  • 34
YuKai Wang
  • 11
  • 1
  • Hi, it might be that when you run `fmt.Println(wr.GetQueryResult().GetOutputContexts())`, `wr` is nil, or `wr.GetQueryResult()` is nil. Can you add checks to ensure that they are not, and maybe add some debug logs? – Ullaakut Jan 17 '20 at 16:11

1 Answers1

0

I think you should obtain the port from the Heroku runtime

var port = os.Getenv("PORT")

It looks like it is hardcoded.

Beppe C
  • 11,256
  • 2
  • 19
  • 41