Skip to content

Commit 015c531

Browse files
committed
Merge branch 'ngfw-Templates'
2 parents 535081c + adb1aa5 commit 015c531

File tree

7 files changed

+332
-2
lines changed

7 files changed

+332
-2
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,19 @@ $botman->hears('keyword', function (BotMan $bot) {
357357
});
358358
```
359359

360+
or even video.
361+
```php
362+
use Mpociot\BotMan\Messages\Message;
363+
364+
$botman->hears('keyword', function (BotMan $bot) {
365+
// Build message object
366+
$message = Message::create("Big Buck Bunny")
367+
->video( "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" );
368+
// Reply message object
369+
$botman->reply($message);
370+
});
371+
```
372+
360373

361374
Slack-specific fields and attachments:
362375

src/Mpociot/BotMan/Button.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,4 @@ public function jsonSerialize()
9696
{
9797
return $this->toArray();
9898
}
99-
}
99+
}

src/Mpociot/BotMan/Drivers/FacebookDriver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Mpociot\BotMan\Message;
77
use Mpociot\BotMan\Question;
88
use Illuminate\Support\Collection;
9+
use Mpociot\BotMan\Facebook\Template;
910
use Symfony\Component\HttpFoundation\Request;
1011
use Symfony\Component\HttpFoundation\ParameterBag;
1112
use Mpociot\BotMan\Messages\Message as IncomingMessage;
@@ -146,6 +147,7 @@ public function isBot()
146147
private function convertQuestion(Question $question)
147148
{
148149
$questionData = $question->toArray();
150+
149151
$replies = Collection::make($question->getButtons())->map(function ($button) {
150152
return [
151153
'content_type' => 'text',
@@ -183,6 +185,8 @@ public function reply($message, $matchingMessage, $additionalParameters = [])
183185
*/
184186
if ($message instanceof Question) {
185187
$parameters['message'] = $this->convertQuestion($message);
188+
} elseif ($message instanceof Template) {
189+
$parameters['message'] = $message->toArray();
186190
} elseif ($message instanceof IncomingMessage) {
187191
if (! is_null($message->getImage())) {
188192
unset($parameters['message']['text']);

src/Mpociot/BotMan/Drivers/WeChatDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class WeChatDriver extends Driver
2727
public function buildPayload(Request $request)
2828
{
2929
try {
30-
$xml = simplexml_load_string($request->getContent(), 'SimpleXMLElement', LIBXML_NOCDATA);
30+
$xml = @simplexml_load_string($request->getContent(), 'SimpleXMLElement', LIBXML_NOCDATA);
3131
$json = json_encode($xml);
3232
$data = json_decode($json, true);
3333
} catch (\Exception $e) {
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
3+
namespace Mpociot\BotMan\Facebook;
4+
5+
use JsonSerializable;
6+
7+
class Element implements JsonSerializable
8+
{
9+
/** @var string */
10+
protected $title;
11+
12+
/** @var string */
13+
protected $image_url;
14+
15+
/** @var string */
16+
protected $item_url;
17+
18+
/** @var string */
19+
protected $subtitle;
20+
21+
/** @var object */
22+
protected $buttons;
23+
24+
/**
25+
* @return static
26+
*/
27+
public static function create($title)
28+
{
29+
return new static($title);
30+
}
31+
32+
/**
33+
* @param string $title
34+
*/
35+
public function __construct($title)
36+
{
37+
$this->title = $title;
38+
}
39+
40+
/**
41+
* @param string $title
42+
* @return $this
43+
*/
44+
public function title($title)
45+
{
46+
$this->title = $title;
47+
48+
return $this;
49+
}
50+
51+
/**
52+
* @param string $subtitle
53+
* @return $this
54+
*/
55+
public function subtitle($subtitle)
56+
{
57+
$this->subtitle = $subtitle;
58+
59+
return $this;
60+
}
61+
62+
/**
63+
* @param string $image_url
64+
* @return $this
65+
*/
66+
public function image($image_url)
67+
{
68+
$this->image_url = $image_url;
69+
70+
return $this;
71+
}
72+
73+
/**
74+
* @param string $item_url
75+
* @return $this
76+
*/
77+
public function itemUrl($item_url)
78+
{
79+
$this->item_url = $item_url;
80+
81+
return $this;
82+
}
83+
84+
/**
85+
* @param ElementButton $button
86+
* @return $this
87+
*/
88+
public function addButton(ElementButton $button)
89+
{
90+
$this->buttons[] = $button->toArray();
91+
92+
return $this;
93+
}
94+
95+
/**
96+
* @param array $buttons
97+
* @return $this
98+
*/
99+
public function addButtons(array $buttons)
100+
{
101+
if (isset($buttons) && is_array($buttons)) {
102+
foreach ($buttons as $button) {
103+
if ($button instanceof ElementButton) {
104+
$this->buttons[] = $button->toArray();
105+
}
106+
}
107+
}
108+
109+
return $this;
110+
}
111+
112+
/**
113+
* @return array
114+
*/
115+
public function toArray()
116+
{
117+
return [
118+
'title' => $this->title,
119+
'image_url' => $this->image_url,
120+
'item_url' => $this->item_url,
121+
'subtitle' => $this->subtitle,
122+
'buttons' => $this->buttons,
123+
];
124+
}
125+
126+
/**
127+
* @return array
128+
*/
129+
public function jsonSerialize()
130+
{
131+
return $this->toArray();
132+
}
133+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Mpociot\BotMan\Facebook;
4+
5+
class ElementButton
6+
{
7+
/** @var string */
8+
protected $type = 'web_url';
9+
10+
/** @var string */
11+
protected $url;
12+
13+
/** @var string */
14+
protected $title;
15+
16+
/**
17+
* @param string $title
18+
*
19+
* @return static
20+
*/
21+
public static function create($title)
22+
{
23+
return new static($title);
24+
}
25+
26+
/**
27+
* @param string $title
28+
*/
29+
public function __construct($title)
30+
{
31+
$this->title = $title;
32+
}
33+
34+
/**
35+
* Set the button URL.
36+
*
37+
* @param string $url
38+
* @return $this
39+
*/
40+
public function url($url)
41+
{
42+
$this->url = $url;
43+
44+
return $this;
45+
}
46+
47+
/**
48+
* Set the button type.
49+
*
50+
* @param string $type
51+
* @return $this
52+
*/
53+
public function type($type)
54+
{
55+
$this->type = $type;
56+
57+
return $this;
58+
}
59+
60+
/**
61+
* @return array
62+
*/
63+
public function toArray()
64+
{
65+
return [
66+
'type' => $this->type,
67+
'url' => $this->url,
68+
'title' => $this->title,
69+
];
70+
}
71+
72+
/**
73+
* @return array
74+
*/
75+
public function jsonSerialize()
76+
{
77+
return $this->toArray();
78+
}
79+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
namespace Mpociot\BotMan\Facebook;
4+
5+
use JsonSerializable;
6+
7+
class Template implements JsonSerializable
8+
{
9+
/** @var string */
10+
protected $template_type = 'generic';
11+
12+
/** @var string */
13+
protected $text;
14+
15+
/** @var string */
16+
protected $value;
17+
18+
/** @var string */
19+
protected $name;
20+
21+
/** @var array */
22+
protected $elements = [];
23+
24+
/**
25+
* @param string $template_type The PHP template type to use
26+
* @return static
27+
*/
28+
public static function create($template_type = 'generic')
29+
{
30+
return new static($template_type);
31+
}
32+
33+
/**
34+
* @param string $template_type The PHP template type to use
35+
*/
36+
public function __construct($template_type)
37+
{
38+
$this->template_type = $template_type;
39+
}
40+
41+
/**
42+
* @param string $image_url
43+
* @return $this
44+
*/
45+
public function image($image_url)
46+
{
47+
$this->image_url = $image_url;
48+
49+
return $this;
50+
}
51+
52+
/**
53+
* @param Element $element
54+
* @return $this
55+
*/
56+
public function addElement(Element $element)
57+
{
58+
$this->elements[] = $element->toArray();
59+
60+
return $this;
61+
}
62+
63+
/**
64+
* @param array $elements
65+
* @return $this
66+
*/
67+
public function addElements(array $elements)
68+
{
69+
foreach ($elements as $element) {
70+
if ($element instanceof Element) {
71+
$this->elements[] = $element->toArray();
72+
}
73+
}
74+
75+
return $this;
76+
}
77+
78+
/**
79+
* @return array
80+
*/
81+
public function toArray()
82+
{
83+
return [
84+
'attachment' => [
85+
'type' => 'template',
86+
'payload' => [
87+
'template_type' => $this->template_type,
88+
'elements' => $this->elements,
89+
],
90+
],
91+
];
92+
}
93+
94+
/**
95+
* @return array
96+
*/
97+
public function jsonSerialize()
98+
{
99+
return $this->toArray();
100+
}
101+
}

0 commit comments

Comments
 (0)