1313use Andre \Bionic \Plugins \AbstractBionicPlugin ;
1414use Andre \Bionic \Plugins \Messenger \Messages \Change ;
1515use 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
1720class 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