Skip to content

Commit 54e95f7

Browse files
authored
Update wit middleware. botman#1186 (botman#1195)
* Update wit middleware. The actual system doesn't seems to be compatible with the actual response format from wit. * Update WitTest.php Update the tests with the new response format from wit.ai * Update WitTest.php * Update WitTest.php
1 parent 127f7dc commit 54e95f7

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

src/Middleware/Wit.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public function received(IncomingMessage $message, $next, BotMan $bot)
8787

8888
$responseData = Collection::make(json_decode($response->getContent(), true));
8989
$message->addExtras('entities', $responseData->get('entities'));
90+
$message->addExtras('intents', $responseData->get('intents'));
9091

9192
return $next($message);
9293
}
@@ -99,16 +100,12 @@ public function received(IncomingMessage $message, $next, BotMan $bot)
99100
*/
100101
public function matching(IncomingMessage $message, $pattern, $regexMatched)
101102
{
102-
$entities = Collection::make($message->getExtras())->get('entities', []);
103-
104-
if (! empty($entities)) {
105-
foreach ($entities as $name => $entity) {
106-
if ($name === 'intent') {
107-
foreach ($entity as $item) {
108-
if ($item['value'] === $pattern && $item['confidence'] >= $this->minimumConfidence) {
109-
return true;
110-
}
111-
}
103+
$intents = Collection::make($message->getExtras())->get('intents', []);
104+
105+
if (!empty($intents)) {
106+
foreach ($intents as $intent) {
107+
if (($intent['name'] === $pattern || $intent['id'] === $pattern) && $intent['confidence'] >= $this->minimumConfidence) {
108+
return true;
112109
}
113110
}
114111
}

tests/Middleware/WitTest.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function it_adds_entities_to_the_message()
2727
$messageText = 'This will be my message text!';
2828
$message = new IncomingMessage($messageText, '', '');
2929

30-
$response = new Response(json_encode(['entities' => ['foo' => 'bar']]));
30+
$response = new Response(json_encode(['entities' => ['foo' => 'bar'], 'intents' => []]));
3131

3232
$http = m::mock(Curl::class);
3333
$http->shouldReceive('get')
@@ -46,6 +46,7 @@ public function it_adds_entities_to_the_message()
4646

4747
$this->assertSame([
4848
'entities' => ['foo' => 'bar'],
49+
'intents' => []
4950
], $message->getExtras());
5051
}
5152

@@ -58,19 +59,20 @@ public function it_matches_intents()
5859
$response = new Response('{
5960
"msg_id": "eb458be1-43e0-47c0-88b2-efbc9fa3240a",
6061
"_text": "I am happy",
62+
"intents": [
63+
{
64+
"id": "123456",
65+
"name": "emotion",
66+
"confidence": 0.7343395827157483
67+
}
68+
],
6169
"entities": {
6270
"emotion": [
6371
{
6472
"confidence": 0.9775576413827303,
6573
"type": "value",
6674
"value": "happy"
6775
}
68-
],
69-
"intent": [
70-
{
71-
"confidence": 0.7343395827157483,
72-
"value": "emotion"
73-
}
7476
]
7577
}
7678
}
@@ -102,19 +104,20 @@ public function it_does_not_match_intents_with_lower_confidence()
102104
$response = new Response('{
103105
"msg_id": "eb458be1-43e0-47c0-88b2-efbc9fa3240a",
104106
"_text": "I am happy",
107+
"intents": [
108+
{
109+
"id": "123456",
110+
"name": "emotion",
111+
"confidence": 0.343395827157483,
112+
}
113+
],
105114
"entities": {
106115
"emotion": [
107116
{
108117
"confidence": 0.9775576413827303,
109118
"type": "value",
110119
"value": "happy"
111120
}
112-
],
113-
"intent": [
114-
{
115-
"confidence": 0.343395827157483,
116-
"value": "emotion"
117-
}
118121
]
119122
}
120123
}

0 commit comments

Comments
 (0)