【问题标题】:Api.ai send email from php webhookApi.ai 从 php webhook 发送电子邮件
【发布时间】:2017-11-29 02:12:50
【问题描述】:

我有一个 php webhook,我在 Heroku 上托管它,evertyhing 工作正常。当我在 api.ai 上对其进行测试时,机器人会以我想要的方式回答我,所以它可以正常工作。我的问题是:如何从我的 webhook 发送电子邮件或在数据库中插入文本? 这是我的代码:

<?php 
$method = $_SERVER['REQUEST_METHOD'];
//$msg = "First line of text\nSecond line of text";

// use wordwrap() if lines are longer than 70 characters
//$msg = wordwrap($msg,70);

// send email
//mail("moscosisi@gmail.com","My subject",$msg);


// Process only when method is POST
if($method == 'POST'){

$requestBody = file_get_contents('php://input');
$json = json_decode($requestBody);

$equis = $json->result->parameters->equis;

switch ($equis) {
    case 'hi':
        $speech = "Hi, Nice to meet you";

        break;

    case 'bye':
        $speech = "Bye, good night";
        break;

    case 'anything':
        $speech = "Yes, you can type anything here.";
        break;

    default:
        $speech = "Sorry, I didnt get that. Please ask me something 
else.";
        break;
}


$response = new \stdClass();
$response->speech = $speech;
$response->displayText = $speech;
$response->source = "Alex";

echo json_encode($response);
}
else
{
echo "Method not allowed";
}

?>

【问题讨论】:

  • 电子邮件是通过回调作为参数发送给您的,这是一个我如何在 PHP 中发送电子邮件 的问题,或者它是一个 我如何匹配上下文操作中的电子邮件?
  • btw 实现适用于您收集数据或需要查询其他数据时,而不是基本的机器人响应,它们应该有意图地完成。
  • 我想通过电子邮件发送 $spech 但我不知道该怎么做

标签: php heroku webhooks dialogflow-es


【解决方案1】:

您似乎已将所需的大部分部件放在一起。在你弄清楚你想要$speech 是什么之后,你可以使用你注释掉的部分来发送消息。也许是这样的:

switch ($equis) {
    case 'hi':
        $speech = "Hi, Nice to meet you";

        break;

    case 'bye':
        $speech = "Bye, good night";
        break;

    case 'anything':
        $speech = "Yes, you can type anything here.";
        break;

    default:
        $speech = "Sorry, I didnt get that. Please ask me something 
else.";
        break;
}

mail("moscosisi@gmail.com","My subject",$msg);

同样,您可以使用从 Dialogflow 发送的参数中获得的值执行数据库操作或任何其他操作。

关于此的更令人困惑的部分是您似乎没有注意触发了什么 Intent 或 Action。这将在

$json->result->action

$json->result->metadata->intentName

通常,您使用其中之一来确定为什么您被调用,然后使用参数来确定基于此的操作。

【讨论】:

    猜你喜欢
    • 2011-03-04
    • 1970-01-01
    • 2011-09-16
    • 2014-05-23
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多