-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#testapi.php#
273 lines (229 loc) · 7.72 KB
/
#testapi.php#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
$dbhost = 'localhost';
$dbport = '5432';
$dbname = 'chatwoot_production';
$dbuser = 'chatwoot';
$dbpass = 'Usgtexty99!!';
$chatwoot_token = 'ech';
$chatwoot_url = 'https://chat.textymedia.com';
$inbox_id = 3;
$account_id = 1;
try{
$db = new PDO('pgsql:host='.$dbhost.';port='.$dbport.';dbname='.$dbname, $dbuser, $dbpass);
}
catch (PDOException $e) {
error_log($e->getMessage(), 0);
print_r($e->getMessage());
}
// Presets
$name = 'Inbound SMS';
$email = '';
$phone_number = '18585551001';
$messageIn = 'Test Successful!';
$query = $phone_number;
$contact = search_contact($account_id, $query);
if(count($contact->payload) > 0) {
$contact_id = $contact->payload[0]->id;
$source_id = $contact->payload[0]->contact_inboxes[0]->source_id;
} else {
$name = $phone_number;
$contact_payload = create_contact($inbox_id, $account_id, $email, $name, $phone_number);
$source_id = $contact_payload->payload[0]->contact_inboxes[0]->source_id;
}
$sql = "SELECT * FROM conversations WHERE account_id = {$account_id} AND inbox_id = {$inbox_id} AND status != 1 AND contact_inbox_id = (SELECT id FROM contact_inboxes WHERE source_id = '{$source_id}')";
echo "\n\n\nSQL: ".$sql."\n\n\n";
$dbquery = $db->query($sql);
$result = $dbquery->fetchAll(PDO::FETCH_ASSOC);
$conversation_id = $result[0]['id'];
if(count($result) > 0 ){
// using $conversation_id above from sql query
} else {
$conversation_payload = create_conversation($account_id, $source_id);
$conversation_id = $conversation_payload->id;
}
// Create message
$create_message_payload = create_message($account_id, $conversation_id, $messageIn);
/**
* Search Contact
*
* @param
* account_id int id of account // will be 2
* query string query you want to search
*/
function search_contact($account_id, $query) {
global $chatwoot_token, $chatwoot_url;
$url = "{$chatwoot_url}/api/v1/accounts/{$account_id}/contacts/search?q={$query}";
$options = array(
'http' => array(
'method' => 'GET',
// 'content' => json_encode( $data ),
'header'=> "Content-Type: application/json\r\n" .
"Accept: application/json\r\n" .
"api_access_token: {$chatwoot_token}"
),
'ssl'=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
error_log("Contact: {$result}",0);
$response = json_decode($result);
print_r($response);
return $response;
}
/**
* Create a Contact
*
* @param
* inbox_id int inbox's id
* account_id int account's id
* name string name of contact you wanna create
* email string email of contact you wanna create
* phone_number string phone number of contact you wanna create
*/
function create_contact($inbox_id, $account_id, $email, $name, $phone_number) {
global $chatwoot_token, $chatwoot_url;
$url = "{$chatwoot_url}/api/v1/accounts/{$account_id}/contacts";
$data = array('inbox_id' => $inbox_id,
'email' => $email,
'name' => $name,
'phone_number' => $phone_number);
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $data ),
'header'=> "Content-Type: application/json\r\n" .
"Accept: application/json\r\n" .
"api_access_token: {$chatwoot_token}"
),
'ssl'=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
error_log("Contact created: {$result}",0);
$response = json_decode($result);
print_r($response);
return $response;
}
/**
* Get a Contact
*
* @param
* account_id int account's id
* contact_id int id of contact get from checking contact
*/
function get_contact($account_id, $contact_id) {
global $chatwoot_token, $chatwoot_url;
$url = "{$chatwoot_url}/api/v1/accounts/{$account_id}/contacts/{$contact_id}";
$options = array(
'http' => array(
'method' => 'GET',
'header'=> "Content-Type: application/json\r\n" .
"Accept: application/json\r\n" .
"api_access_token: {$chatwoot_token}"
),
'ssl'=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
error_log("Contact: {$result}",0);
$response = json_decode($result);
print_r($response);
return $response;
}
/**
* Create Conversation
*
* @param
* account_id int account's id
* source_id string Source's id you received from create_contact API
*/
function create_conversation($account_id, $source_id) {
global $chatwoot_token, $chatwoot_url;
$url = "{$chatwoot_url}/api/v1/accounts/{$account_id}/conversations";
$data = array('source_id' => $source_id);
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $data ),
'header'=> "Content-Type: application/json\r\n" .
"Accept: application/json\r\n" .
"api_access_token: {$chatwoot_token}"
),
'ssl'=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
error_log("Created Conversation: {$result}",0);
$response = json_decode($result);
print_r($response);
return $response;
}
/**
* Get Conversation
*
* @param
* account_id int account's id
* conversation_id int id of conversation
*/
function get_conversation($account_id, $conversation_id) {
global $chatwoot_token, $chatwoot_url;
$url = "{$chatwoot_url}/api/v1/accounts/{$account_id}/conversations/{$conversation_id}";
$options = array(
'http' => array(
'method' => 'GET',
'header'=> "Content-Type: application/json\r\n" .
"Accept: application/json\r\n" .
"api_access_token: {$chatwoot_token}"
),
'ssl'=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
error_log("Conversation: {$result}",0);
$response = json_decode($result);
print_r($response);
return $response;
}
/**
* Create Message
*/
function create_message($account_id, $conversation_id, $messageIn){
global $chatwoot_url, $chatwoot_token;
$url = "{$chatwoot_url}/api/v1/accounts/{$account_id}/conversations/{$conversation_id}/messages";
$data = array("content" => $messageIn, "message_type" => "incoming", "private" => false, "content_type" => "text",);
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $data ),
'header'=> "Content-Type: application/json\r\n" .
"Accept: application/json\r\n" .
"api_access_token: {$chatwoot_token}"
),
'ssl'=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
error_log("chatwoot response: {$result}",0);
$response = json_decode($result);
print_r($response);
return $result;
}
?>