-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.php
More file actions
95 lines (85 loc) · 3.6 KB
/
parser.php
File metadata and controls
95 lines (85 loc) · 3.6 KB
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
<?php
if (!defined('BOT_MIDRASH')) die('{"code":200}');
class parser{
private static function parseStatus($status) {
$res = new waUpdateStatus();
$res->messageId = $status['id'];
$res->timestamp = $status['timestamp'];
$res->from = new waUpdateContact();
$res->from->phoneNumber = $status['recipient_id'];
if ($status['status'] == "read") {
$res->status = avalibleWaUpdateStatuses::READ;
}
else if ($status['status'] == "delivered") {
$res->status = avalibleWaUpdateStatuses::DELIVERED;
}
else if ($status['status'] == "sent") {
$res->status = avalibleWaUpdateStatuses::SENT;
}
else {
$res->status = avalibleWaUpdateStatuses::WAITING;
}
return $res;
}
private static function parseMessage($contact, $message) {
$res = new waUpdateMessage();
if ($message['type'] == "text"){
$res = new waUpdateMessageText();
$res->type = avalibleWaMessagesTypes::TEXT;
$res->body = $message['text']['body'];
$res->content = $res->body;
}
else if ($message['type'] == "image") {
$res = new waUpdateMessageImage();
$res->type = avalibleWaMessagesTypes::IMAGE;
$res->imageId = $message['image']['id'];
$res->caption = $message['image']['caption'];
$res->content = $res->caption;
}
else if ($message['type'] == "interactive") {
if ($message['interactive']['type'] == "button_reply") {
$res = new waUpdateMessageButtonReply();
$res->type = avalibleWaMessagesTypes::BUTTON_REPLY;
$res->payload = $message['interactive']['button_reply']['id'];
$res->text = $message['interactive']['button_reply']['title'];
$res->content = $res->payload;
}
else {
$res = new waUpdateMessageListReply();
$res->type = avalibleWaMessagesTypes::LIST_REPLY;
$res->payload = $message['interactive']['list_reply']['id'];
$res->text = $message['interactive']['list_reply']['title'];
$res->description = $message['interactive']['list_reply']['description'];
$res->content = $res->payload;
}
}
else if ($message['type'] == "button") {
$res = new waUpdateMessageButton();
$res->type = avalibleWaMessagesTypes::BUTTON;
$res->payload = $message['button']['payload'];
$res->text = $message['button']['text'];
$res->content = $res->payload;
}
else {
$res->type = avalibleWaMessagesTypes::UNKNOWN;
$res->content = NULL;
}
$res->messageId = $message['id'];
$res->timestamp = $message['timestamp'];
$res->from = new waUpdateContact();
$res->from->phoneNumber = $contact['wa_id'];
$res->from->displayName = $contact['profile']['name'];
return $res;
}
static function parse($json) {
if (isset($json['entry'][0]['changes'][0]['value']['statuses'])) {
return self::parseStatus($json['entry'][0]['changes'][0]['value']['statuses'][0]);
}
else if (isset($json['entry'][0]['changes'][0]['value']['messages'])) {
return self::parseMessage($json['entry'][0]['changes'][0]['value']['contacts'][0], $json['entry'][0]['changes'][0]['value']['messages'][0]);
}
else {
helpers::logErrorToFile("ERROR! parse type not found!");
}
}
}