Skip to content

Commit a8f2ae3

Browse files
committed
added whatsapp message sending and media download api calls
1 parent a5293e1 commit a8f2ae3

4 files changed

Lines changed: 185 additions & 13 deletions

File tree

src/Plugins/Messenger/Traits/AccessesUserProfile.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,15 @@ trait AccessesUserProfile
2626
*
2727
* @param AbstractEndPoint $user
2828
* @param string $fields
29-
* @return UserProfile|null
29+
* @return UserProfile
3030
*/
3131
public function getUserProfile(AbstractEndPoint $user, $fields = "first_name,last_name,profile_pic")
3232
{
3333
$this->checkForPageAccessTokenAndGraphApiVersion();
3434

3535
$url = sprintf($this->url . "/%s/%s?fields=$fields&access_token=%s", $this->graph_api_version, $user->getId(), $this->page_access_token);
3636

37-
try{
38-
$response = $this->httpClient->get($url)->getBody()->getContents();
39-
return UserProfile::create((array)json_decode($response));
40-
}catch (\Exception $exception){
41-
return null;
42-
}
37+
$response = $this->httpClient->get($url)->getBody()->getContents();
38+
return UserProfile::create((array)json_decode($response));
4339
}
4440
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: hp
5+
* Date: 23/05/2022
6+
* Time: 19:25
7+
*/
8+
9+
namespace Andre\Bionic\Plugins\WhatsApp\Traits;
10+
11+
12+
trait HandlesMedia
13+
{
14+
/**
15+
* Retrieve media url
16+
*
17+
* @param $media_id
18+
* @return string
19+
*/
20+
protected function retrieveMediaURL($media_id)
21+
{
22+
$url = sprintf($this->url . "/%s/%s?access_token=%s", $this->graph_api_version, $media_id, $this->access_token);
23+
24+
$this->checkForAccessToken();
25+
26+
return $this->httpClient->get($url);
27+
}
28+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: hp
5+
* Date: 23/05/2022
6+
* Time: 13:04
7+
*/
8+
9+
namespace Andre\Bionic\Plugins\WhatsApp\Traits;
10+
11+
12+
use Andre\Bionic\AbstractMessage;
13+
14+
trait SendsMessages
15+
{
16+
/**
17+
* Send text
18+
*
19+
* @param AbstractMessage $message
20+
* @param $recipient
21+
* @param $type
22+
* @return \Psr\Http\Message\ResponseInterface
23+
*/
24+
public function sendMessage($type, AbstractMessage $message, $recipient) {
25+
return $this->send(['type' => $type, 'to' => $recipient, $type => $message->toArray()]);
26+
}
27+
28+
/**
29+
* send message
30+
*
31+
* @param $data
32+
* @return \Psr\Http\Message\ResponseInterface
33+
*/
34+
protected function send(array $data)
35+
{
36+
$this->checkForPhoneNumberId();
37+
38+
$this->checkForAccessToken();
39+
40+
return $this->httpClient->post($this->messagingFullUrl(), [
41+
'json' => array_merge($data, [
42+
"messaging_product" => "whatsapp",
43+
"recipient_type" => "individual",
44+
])
45+
]);
46+
}
47+
48+
/**
49+
* messaging full url
50+
*
51+
* @return string
52+
*/
53+
protected function messagingFullUrl()
54+
{
55+
return sprintf($this->url . "/%s/%s/messages?access_token=%s", $this->graph_api_version, $this->phone_number_id, $this->access_token);
56+
}
57+
}

src/Plugins/WhatsApp/WhatsAppPlugin.php

Lines changed: 97 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,86 @@
1313
use Andre\Bionic\Plugins\AbstractBionicPlugin;
1414
use Andre\Bionic\Plugins\Messenger\Messages\Change;
1515
use Andre\Bionic\Plugins\Messenger\Messages\EntryItem;
16+
use Andre\Bionic\Plugins\WhatsApp\Traits\HandlesMedia;
17+
use Andre\Bionic\Plugins\WhatsApp\Traits\SendsMessages;
18+
use GuzzleHttp\Client as HttpClient;
1619

1720
class WhatsAppPlugin extends AbstractBionicPlugin
1821
{
22+
use SendsMessages, HandlesMedia;
23+
24+
/**
25+
* @var HttpClient $httpClient
26+
*/
27+
protected $httpClient;
28+
1929
/**
2030
* @var WhatsAppWebHookEvent $webHookEvent
2131
*/
2232
protected $webHookEvent;
2333

34+
/**
35+
* @var string $graph_api_version
36+
*/
37+
protected $graph_api_version = 'v13.0';
38+
39+
/**
40+
* @var string $phone_number_id
41+
*/
42+
protected $phone_number_id;
43+
2444
/**
2545
* @var string $access_token
2646
*/
2747
protected $access_token;
2848

49+
/**
50+
* @var string $url
51+
*/
52+
protected $url = "https://graph.facebook.com";
53+
54+
/**
55+
* create new whatsApp plugin instance
56+
*
57+
* MessengerPlugin constructor.
58+
* @param array $config
59+
*/
60+
public function __construct($config = [])
61+
{
62+
parent::__construct($config);
63+
$this->httpClient = $this->newHttpClient();
64+
}
65+
66+
/**
67+
* Set phone number id
68+
*
69+
* @param string $phone_number_id
70+
*/
71+
public function setPhoneNumberId($phone_number_id)
72+
{
73+
$this->phone_number_id = $phone_number_id;
74+
}
75+
76+
/**
77+
* set new access token
78+
*
79+
* @param string $access_token
80+
*/
81+
public function setAccessToken($access_token)
82+
{
83+
$this->access_token = $access_token;
84+
}
85+
86+
/**
87+
* set new graph api version
88+
*
89+
* @param $graph_api_version
90+
*/
91+
public function setGraphApiVersion($graph_api_version)
92+
{
93+
$this->graph_api_version = $graph_api_version;
94+
}
95+
2996
/**
3097
* create a new web hook event from web hook data
3198
*/
@@ -46,6 +113,21 @@ public function emitEvents(AbstractBionic $bionic)
46113
$this->iterateOverObjectsAndEmitEvents();
47114
}
48115

116+
/**
117+
* create new http client
118+
*
119+
* @return HttpClient
120+
*/
121+
protected function newHttpClient()
122+
{
123+
return new HttpClient([
124+
'headers' => [
125+
'Content-Type' => 'application/json'
126+
],
127+
'verify' => false
128+
]);
129+
}
130+
49131
private function iterateOverObjectsAndEmitEvents()
50132
{
51133
/**
@@ -87,22 +169,22 @@ private function iterateOverObjectsAndEmitEvents()
87169
$this->bionic->emit('message.contacts', [$this, $contacts, $sent_contacts]);
88170

89171
if ($errors = $message->getErrors())
90-
$this->bionic->emit('message.errors', [$this, $errors]);
172+
$this->bionic->emit('message.errors', [$this, $contacts, $errors]);
91173

92174
if ($image = $message->getImage())
93-
$this->bionic->emit('message.image', [$this, $image]);
175+
$this->bionic->emit('message.image', [$this, $contacts, $image]);
94176

95177
if ($document = $message->getDocument())
96-
$this->bionic->emit('message.document', [$this, $document]);
178+
$this->bionic->emit('message.document', [$this, $contacts, $document]);
97179

98180
if ($voice = $message->getVoice())
99-
$this->bionic->emit('message.voice', [$this, $voice]);
181+
$this->bionic->emit('message.voice', [$this, $contacts, $voice]);
100182

101183
if ($sticker = $message->getSticker())
102-
$this->bionic->emit('message.sticker', [$this, $sticker]);
184+
$this->bionic->emit('message.sticker', [$this, $contacts, $sticker]);
103185

104186
if ($system = $message->getSystem())
105-
$this->bionic->emit('message.system', [$this, $system]);
187+
$this->bionic->emit('message.system', [$this, $contacts, $system]);
106188
}
107189
}
108190

@@ -139,4 +221,13 @@ protected function checkForAccessToken()
139221
if (!$this->access_token)
140222
throw new \InvalidArgumentException('An access token has not been specified!');
141223
}
224+
225+
/**
226+
* check if access token has been provided before making any http request
227+
*/
228+
protected function checkForPhoneNumberId()
229+
{
230+
if (!$this->phone_number_id)
231+
throw new \InvalidArgumentException('A phone number id has not been specified!');
232+
}
142233
}

0 commit comments

Comments
 (0)