8000 Dialogflow middleware using api v2 by eclips16 · Pull Request #1010 · botman/botman · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Dialogflow middleware using api v2 #1010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: 2.0
Choose a base branch
from

Conversation

eclips16
Copy link
@eclips16 eclips16 commented May 3, 2019

composer require google/cloud-dialogflow

environment variables to add to your .env file:
GOOGLE_CLOUD_PROJECT=project-id
GOOGLE_APPLICATION_CREDENTIALS=/path/to/project-secret.json

get your application credentials file from the google cloud console (Service account)

Drop-in replacement for the v1 Middleware

Maybe someone can help writing tests ? :)

composer require google/cloud-dialogflow
environment variables to add to your .env file:
GOOGLE_CLOUD_PROJECT=project-id
GOOGLE_APPLICATION_CREDENTIALS=/path/to/project-secret.json
get your application credentials file from the google cloud console (Service account)
Drop-in replacement for the v1 Middleware
@codecov
Copy link
codecov bot commented May 3, 2019

Codecov Report

Attention: Patch coverage is 83.92857% with 18 lines in your changes missing coverage. Please review.

Please upload report for BASE (2.0@ac9ecf7). Learn more about missing BASE report.
Report is 77 commits behind head on 2.0.

Files with missing lines Patch % Lines
src/Middleware/DialogFlowV2/Client.php 79.24% 11 Missing ⚠️
src/Middleware/DialogFlowV2.php 75.86% 7 Missing ⚠️
Additional details and impacted files
@@          Coverage Diff           @@
##             2.0    #1010   +/-   ##
======================================
  Coverage       ?   80.61%           
  Complexity     ?      632           
======================================
  Files          ?       52           
  Lines          ?     1718           
  Branches       ?        0           
======================================
  Hits           ?     1385           
  Misses         ?      333           
  Partials       ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@eclips16
Copy link
Author

only one month remaining before v1 shutdown
I am using this middleware for about 3 months now without any problem
maybe someone can help me to write tests ?

@atsoares
Copy link

Hi, @eclips16 I'm trying to use your middleware but I can't, can you help me giving an small example of how you are using it? I'm getting an error when I try to use like this:

$botman = resolve('botman');

$dialogflow = DialogflowV2::create();
$botman->middleware->received($text);
$botman->hears('Default Welcome Intent', function (BotMan $bot) {
        
        $extras = $bot->getMessage()->getExtras();
        $apiReply = $extras['apiReply'];
        $apiAction = $extras['apiAction'];
        $apiIntent = $extras['apiIntent'];
        
        $bot->reply($apiReply);
        
})->middleware($dialogflow);

@eclips16
Copy link
Author

@atsoares what error are you getting ?
this is how I use it:

$dialogflow = DialogflowV2::create()->listenForAction();
$botman->middleware->received($dialogflow);

$botman->hears('input.welcome', function ($bot) {
    $bot->reply('a reply');
});
8000

pay attention to the code you pasted:
you are missing the listenForAction() call and you are passing $text instead of $dialogflow in the received method.
Also note that you need to define an action in your intent and hear this action instead of the name of the intent (like in the original middleware for v1)

@atsoares
Copy link

Hi @eclips16, thanks for that, now there's no error but I didn't figure out how to get the dialogflow response as botman reply.

My problem is, I have a beautiful agent in Dialogflow that works perfectly already with his own fulfillment inline editor. I'm trying to use BotMan just to use the botman widget as widget in a website, receiving the message typed from the user, passing it to dialogflow and getting back all the response from that, including the rich responses. I tried to use botcopy or kommunicate but they are expensive for my case.

@eclips16
Copy link
Author

I am afraid I won't be able to help you with so little information on the code.
Also did you try to use v1 first ? it is still not deprecated and will only be on March.

@atsoares
Copy link

Oh man, I will try it with V1. Thanks!

@atsoares
Copy link

@eclips16 mate, thanks for the help but it still doesn't work. I kept the v1 version and my code is:

   DriverManager::loadDriver(WebDriver::class);
   $botman = BotManFactory::create($config);

   $dialogflow = Dialogflow::create('apikey')->listenForAction();
   $botman->middleware->received($dialogflow);

  $botman->hears('input.welcome', function (BotMan $bot) {
    
    console.log('works');
    $extras = $bot->getMessage()->getExtras();
    $apiReply = $extras['apiReply'];
    $apiAction = $extras['apiAction'];
    $apiIntent = $extras['apiIntent'];
    $bot->typesAndWaits(1);
    $bot->reply($apiReply);
     
  })->middleware($dialogflow);

  $botman->listen();

When I use a basic command like hears('hello'), the bot replies, but not when I use middleware.
Also, the connection with dialogflow is okay, bc I can see at history in dialogflow's console all the words that I have been sent using to test.

@eclips16
Copy link
Author

Did you define "input.welcome" in the action field for your intent ?
Screen Shot 2019-11-21 at 0 11 43

@atsoares
Copy link
atsoares commented Nov 21, 2019

Sure man I did it, the agent is already done and as I said the History logs at Dialogflow's console shows all the text that I sent when I was testing the botman widget.

@eclips16
Copy link
Author

console.log('works');
maybe use error_log('works);
then try to add some error_log into the code of the middleware:

error_log(json_encode($message->getPayload()));
error_log(json_encode($message->getExtras()));

to see the intent and action returned by dialogflow

@atsoares
Copy link
atsoares commented Nov 21, 2019

Thanks mate, I don't know why but after that I used the error_log and checked at error.log, the bot started to reply. Really, I just checked the error log and everything was there, the bot just didn't want to reply I'm guessing.

edit: actually maybe the whole problem was that console.log('works'); line...

@eclips16
Copy link
Author

console.log is for javascript

did you try to use my dialogflow v2 middleware now that it is working for you with v1 ? :)

@ranrinc
Copy link
ranrinc commented Nov 22, 2019

Hi @eclips16,

Special thank you, for making this middleware. It run almost flawless. However today I encounter this problem

[2019-11-22 08:08:06] local.ERROR: Call to a member function getFields() on null {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Call to a member function getFields() on null at /studio/vendor/botman/botman/src/Middleware/DialogflowV2.php:113)
[stacktrace]

In your case the error stuck in line 132 unsure what happen. Maybe you can help.

@eclips16
Copy link
Author
8000

@ranrinc can you please tell me how to reproduce this issue ?
it seems to be related to the contexts of the query but i don't understand why as it is working for me

@ranrinc
Copy link
ranrinc commented Nov 22, 2019

@eclips16 mate, thanks for the help but it still doesn't work. I kept the v1 version and my code is:

   DriverManager::loadDriver(WebDriver::class);
   $botman = BotManFactory::create($config);

   $dialogflow = Dialogflow::create('apikey')->listenForAction();
   $botman->middleware->received($dialogflow);

  $botman->hears('input.welcome', function (BotMan $bot) {
    
    console.log('works');
    $extras = $bot->getMessage()->getExtras();
    $apiReply = $extras['apiReply'];
    $apiAction = $extras['apiAction'];
    $apiIntent = $extras['apiIntent'];
    $bot->typesAndWaits(1);
    $bot->reply($apiReply);
     
  })->middleware($dialogflow);

  $botman->listen();

When I use a basic command like hears('hello'), the bot replies, but not when I use middleware.
Also, the connection with dialogflow is okay, bc I can see at history in dialogflow's console all the words that I have been sent using to test.

try this code

and make sure you install google API library and add its need information into your .env
Furthermore be sure you install some PHP requirement

<?php
use App\Http\Controllers\BotManController;
use BotMan\BotMan\BotMan;
use BotMan\Botman\Middleware\DialogflowV2;
use BotMan\Drivers\Telegram\TelegramDriver;

$botman = resolve('botman');
$botman->middleware->sending($middleware);

$dialogflow = DialogflowV2::create('en')->listenForAction();
$botman->middleware->received($dialogflow);

$botman->hears('input.welcome', function (BotMan $bot) {
	$extras = $bot->getMessage()->getExtras();
        $apiReply = $extras['apiReply'];
        $bot->reply($apiReply);
    })->middleware($dialogflow);

@eclips16
Copy link
Author

@eclips16 mate, thanks for the help but it still doesn't work. I kept the v1 version and my code is:

   DriverManager::loadDriver(WebDriver::class);
   $botman = BotManFactory::create($config);

   $dialogflow = Dialogflow::create('apikey')->listenForAction();
   $botman->middleware->received($dialogflow);

  $botman->hears('input.welcome', function (BotMan $bot) {
    
    console.log('works');
    $extras = $bot->getMessage()->getExtras();
    $apiReply = $extras['apiReply'];
    $apiAction = $extras['apiAction'];
    $apiIntent = $extras['apiIntent'];
    $bot->typesAndWaits(1);
    $bot->reply($apiReply);
     
  })->middleware($dialogflow);

  $botman->listen();

When I use a basic command like hears('hello'), the bot replies, but not when I use middleware.
Also, the connection with dialogflow is okay, bc I can see at history in dialogflow's console all the words that I have been sent using to test.

try this code

and make sure you install google API library and add its need information into your .env
Furthermore be sure you install some PHP requirement

<?php
use App\Http\Controllers\BotManController;
use BotMan\BotMan\BotMan;
use BotMan\Botman\Middleware\DialogflowV2;
use BotMan\Drivers\Telegram\TelegramDriver;

$botman = resolve('botman');
$botman->middleware->sending($middleware);

$dialogflow = DialogflowV2::create('en')->listenForAction();
$botman->middleware->received($dialogflow);

$botman->hears('input.welcome', function (BotMan $bot) {
	$extras = $bot->getMessage()->getExtras();
        $apiReply = $extras['apiReply'];
        $bot->reply($apiReply);
    })->middleware($dialogflow);

Okey I already have this code in place but I do not have the error.
What is in your welcome intent ? Can you make a screenshot of the config ?

@ranrinc
Copy link
ranrinc commented Nov 22, 2019

@ranrinc can you please tell me how to reproduce this issue ?
it seems to be related to the contexts of the query but i don't understand why as it is working for me

@eclips16,

Here is the raw json from 2 intents... it work on dialogflow console btw..

{
  "responseId": "f68a5",
  "queryResult": {
    "queryText": "1",
    "action": "input.quantity",
    "parameters": {
      "quantity": 1
    },
    "allRequiredParamsPresent": true,
    "fulfillmentText": "You picked 1, would you like me to process your order now?",
    "fulfillmentMessages": [
      {
        "text": {
          "text": [
            "You picked 1, would you like me to process your order now?"
          ]
        }
      }
    ],
    "outputContexts": [
      {
        "name": "projects/contexts/input_quantity_confirmation",
        "lifespanCount": 1,
        "parameters": {
          "quantity.original": "1",
          "quantity": 1
        }
      },
      {
        "name": "projects/contexts/session_var",
        "lifespanCount": 50,
        "parameters": {
          "product_name.original": "batik",
          "quantity.original": "1",
          "order_type.original": "buy",
          "quantity": 1,
          "choose_product_option.original": "1",
          "order_type": "buy ",
          "choose_product_option": 1,
          "product_name": "batik"
        }
      }
    ],
    "intent": {
      "name": "projects/",
      "displayName": "order.quantity (step [4])"
    },
    "intentDetectionConfidence": 0.999,
    "diagnosticInfo": {
      "webhook_latency_ms": 959
    },
    "languageCode": "en"
  },
  "webhookStatus": {
    "message": "Webhook execution successful"
  }
}

in conjuction to this intent

{
  "responseId": "0eb6a",
  "queryResult": {
    "queryText": "y",
    "action": "order.quantity.yes",
    "parameters": {},
    "allRequiredParamsPresent": true,
    "fulfillmentText": "Got it, so now we would like to get your personal information. To begin please telling me your name?",
    "fulfillmentMessages": [
      {
        "text": {
          "text": [
            "Got it, so now we would like to get your personal information. To begin please telling me your name?"
          ]
        }
      }
    ],
    "outputContexts": [
      {
        "name": "projects/contexts/input_name",
        "lifespanCount": 1
      },
      {
        "name": "projects/contexts/session_var",
        "lifespanCount": 50,
        "parameters": {
          "product_name.original": "batik",
          "quantity.original": "1",
          "quantity": 1,
          "order_type.original": "buy",
          "choose_product_option.original": "1",
          "order_type": "buy ",
          "choose_product_option": 1,
          "product_name": "batik"
        }
      }
    ],
    "intent": {
      "name": "projects/",
      "displayName": "order.quantity.yes (step [5])"
    },
    "intentDetectionConfidence": 1,
    "languageCode": "en"
  }
}

Again thank you for all the help

@eclips16
Copy link
Author

I am sorry @ranrinc but I am enable to load the intents in my dialogflow console
Can you please export them ?
If I can't reproduce the bug, I won't be able to fix it

@atsoares
Copy link
atsoares commented Nov 22, 2019

console.log is for javascript

did you try to use my dialogflow v2 middleware now that it is working for you with v1 ? :)

Actually it doesn't because I don't use Laravel, its an old pure php website that I'm working with.
But thanks for your help, maybe if I use something like vlucas/phpdotenv I could read the env file. I'll try it later.

@ranrinc
Copy link
ranrinc commented Nov 25, 2019

I am sorry @ranrinc but I am enable to load the intents in my dialogflow console
Can you please export them ?
If I can't reproduce the bug, I won't be able to fix it

Since my dialogflow consist 3rd party API is possible for me to share the DF so you can seeing it? instead of recreating? because I believe if I give you the save DF.. it wont work without the backend connection

@keshansandeepa
Copy link

@eclips16 I'm getting Could not construct ApplicationDefaultCredentials
and i put the credentials path in env file like this
GOOGLE_APPLICATION_CREDENTIALS=storage/app/google/******.json
but its not working , any help

@ranrinc
Copy link
ranrinc commented Nov 26, 2019

@eclips16 I'm getting Could not construct ApplicationDefaultCredentials
and i put the credentials path in env file like this
GOOGLE_APPLICATION_CREDENTIALS=storage/app/google/******.json
but its not working , any help

You need to wrote a full path of the location of your json file. It can be anywhere if you run on Linux.

@menno-dev
Copy link
menno-dev commented Jan 22, 2020

@eclips16 Unfortunately I have this error in my log, any idea?
[2020-01-22 20:53:30] local.ERROR: Call to a member function getFields() on null {"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Call to a member function getFields() on null at /BotManStudio/vendor/botman/botman/src/Middleware/DialogflowV2.php:132)
[stacktrace]

So when the intent has context(s), I get this error and nor reply. When there is no context set to an intent, it works fine.

@tommiekn
Copy link

Any update on this pull request?

Dialogflow api v.1 gets deprecated on May 31st, 2020

@Benjizle
Copy link

Hello Guys,
Great work here, any idea how I can use Botman studio for my Dialogflow fulfilments?

I am building a WhatsApp bot that connects to Dialogflow via Twilio. I want a way for Botman to pick up specific actions or intents and execute some code, this is easy if I was using Botman directly.
I have tried eristemena/botman-driver-dialogflow which does not work with the V2 drivers.
I subscribed to @mpociot video tutorial but did find anything in this regard.

any pointers?

@Hikingyo
@mpociot

@TavoNiievez
Copy link

@eclips16 Thanks you, works correctly.

@E-Haroun
Copy link
E-Haroun commented Jan 1, 2021

Hello, Please tell me
Does Botman still working with this version of DialogFlow "v2" ??

@E-Haroun
Copy link
E-Haroun commented Jan 1, 2021

Hello @eclips16 , i tried your middleware DialogflowV2 and i got this error
`"""
{

"message": "IAM permission 'dialogflow.sessions.detectIntent' on 'projects\/assistant-bot-shco\/agent' denied.",


"code": 7,


"status": "PERMISSION_DENIED",


"details": []

}
"""`

@E-Haroun
Copy link
E-Haroun commented Jan 1, 2021

@E-Haroun random question: Did you start getting this error message?

	Google\ApiCore\ValidationException
	Could not construct ApplicationDefaultCredentials 

if so, my app stopped working too.

No Bro

@E-Haroun
Copy link
E-Haroun commented Jan 1, 2021

@TavoNiievez can please tell me steps to link dialogflow v2 with botman
i get the .json file that containe the private-key ...
is there a methode to generate Client access token in V2?

@eclips16
Copy link
Author
eclips16 commented Jan 1, 2021

Hello @eclips16 , i tried your middleware DialogflowV2 and i got this error
`"""
{

"message": "IAM permission 'dialogflow.sessions.detectIntent' on 'projects\/assistant-bot-shco\/agent' denied.",


"code": 7,


"status": "PERMISSION_DENIED",


"details": []

}
"""`

For me it is a permission issue: check that the service account you created have the Dialog api admin and dialog api client permission and it should work

@E-Haroun
Copy link
E-Haroun commented Jan 1, 2021

Hello @eclips16 , i tried your middleware DialogflowV2 and i got this error
`"""
{

"message": "IAM permission 'dialogflow.sessions.detectIntent' on 'projects\/assistant-bot-shco\/agent' denied.",


"code": 7,


"status": "PERMISSION_DENIED",


"details": []

}
"""`

For me it is a permission issue: check that the service account you created have the Dialog api admin and dialog api client permission and it should work

yes i did it, i gave all permission ... but same error

@eclips16
Copy link
Author
eclips16 commented Jan 1, 2021

Go to your google console and create a new service account for this project.
Give this account only dialoglow api admin and client permissions.
Download the json file and use this one.
Is it better ?

@eclips16
Copy link
Author
eclips16 commented Jan 1, 2021

Ho and @E-Haroun
GOOGLE_APPLICATION_CREDENTIALS=/app/yourproject-xxxx-xxxxxxxxxxxx.json
the path need to be an absolute path to the file

@E-Haroun
Copy link
E-Haroun commented Jan 5, 2021

Ho and @E-Haroun
GOOGLE_APPLICATION_CREDENTIALS=/app/yourproject-xxxx-xxxxxxxxxxxx.json
the path need to be an absolute path to the file

thank you so much

@suwendrarms
Copy link

@eclips16 can help plz.
how to get botman view input text for before process

@eclips16
Copy link
Author

@suwendrarms I am sorry but I did not get your question

@suwendrarms
Copy link

@eclips16 my project botman and dialogflowv2 connection part is done. first thanks for this .but i need add google translate for this. example view input put sinhala language massage then it translate english and add the botman process.

@eclips16
Copy link
Author

@suwendrarms You need to create a botman middleware and load it before the dialogflow middleware
You should be able to translate your text before it reach dialogflow (using the received function of the middleware)

@suwendrarms
Copy link

@eclips16 can you show example code for botman middleware get input text and set input for translated text

@eclips16
Copy link
Author

@suwendrarms you can check the doc for botman middleware here: https://botman.io/2.0/middleware
then inside your middleware you get the input text with $text = $message->getText(); and you can set it with $message->setText('your translated text');

@suwendrarms
Copy link

@suwendrarms you can check the doc for botman middleware here: https://botman.io/2.0/middleware
then inside your middleware you get the input text with $text = $message->getText(); and you can set it with $message->setText('your translated text');
@eclips16 thank you so much it is working

@suwendrarms
Copy link

@eclips16 thank you so much

@vincent659
Copy link

Hi @eclips16 Thank you for updating this middleware it meant a lot to us!

However, I've tried to include all the files needed for DialogFlow2 in the vendor/botman/botman/src accompany with my google application credentials within the .env file, but I'm still getting no responses. Is it currently still working for you guys? Any help would be very valuable thank you so much!

@eclips16
Copy link
Author

@vincent659 do you get some kind of error ?
It is still working here yes :)

@vincent659
Copy link

Hi thank you for the fast reply @eclips16 . I've got it working in the end thank you so much!

@genkovich
Copy link

Hi,
I put your code into an independent package:
https://github.com/genkovich/DialogFlowBotManMiddleware

May be useful for someone.

@vincent659
Copy link

Dear Eclips16,

I've also put your code into an independent package:

https://github.com/vincent659/botman-studio-dialogflow-v2
https://packagist.org/packages/vincechen/botman-studio-dialogflow-v2

I also thought it would be useful for someone and thought no one had created it yet.

Thank you!

@TavoNiievez
Copy link

Maybe it's time to fork botman...

@gdownunder
Copy link

Hi,
I've implemented the v2 middleware successfully in a project but faced some challenges when I wanted to test the islist function in Dialogflow. I'm unable to extract the values from the json file returned from dialogflow for that specific parameter. Other parameters where islist is not selected works fine.

I've tried to understand the middleware code and I extracted the data with a data dump on $queryParameters on line 120 in client.php but it seems like the following foreach loop doesn't cater for this setup as I'm unable to retrieve the values after this.

My questions is if anyone here has successfully implemented the islist option in Dialogflow and got suggestions on how I can proceed.

Thank you for any insight!

Client.php code
Screen Shot 2021-04-07 at 08 49 59

Raw file from dialogflow:
Screen Shot 2021-04-07 at 08 45 52

@adnandogar
Copy link

Ho and @E-Haroun
GOOGLE_APPLICATION_CREDENTIALS=/app/yourproject-xxxx-xxxxxxxxxxxx.json
the path need to be an absolute path to the file

thank you so much

How you get resolved this issue, i checked with absolute path but still i am getting this error:

In CredentialsWrapper.php line 256:

Could not construct ApplicationDefaultCredentials

In ApplicationDefaultCredentials.php line 189:

Could not load the default credentials. Browse to https://developers.google
.com/accounts/docs/application-default-credentials for more information

@199ocero
Copy link

Hi I'm new to Botman and can someone ask me how to put google credentials in env? Can't seem to work it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0