Skip to content

Commit cde8dbe

Browse files
committed
initial commit
1 parent 396ef63 commit cde8dbe

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

menu.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
$main_menu = [[
4+
"Columns"=> 6,
5+
"Rows"=> 1,
6+
"BgColor" => "#808B96",
7+
"ActionType" => "reply",
8+
"ActionBody" => "action1",
9+
"Text" => "<font color=\"#F8F9F9\"><b>Button 1</b></font>",
10+
"TextSize" => "regular"
11+
]];

setup.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
$curl = curl_init();
4+
5+
curl_setopt_array($curl, array(
6+
CURLOPT_URL => "https://chatapi.viber.com/pa/set_webhook",
7+
CURLOPT_RETURNTRANSFER => true,
8+
CURLOPT_ENCODING => "",
9+
CURLOPT_MAXREDIRS => 10,
10+
CURLOPT_TIMEOUT => 30,
11+
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
12+
CURLOPT_CUSTOMREQUEST => "POST",
13+
CURLOPT_POSTFIELDS => "{ \r\n \"url\": \"https://your-domain/viber.php\",\r\n \"event_types\":[\r\n \"conversation_started\"\r\n ]\r\n}",
14+
CURLOPT_HTTPHEADER => array(
15+
"Cache-Control: no-cache",
16+
"Content-Type: application/JSON",
17+
"X-Viber-Auth-Token: VIBER-TOKEN"
18+
),
19+
));
20+
21+
$response = curl_exec($curl);
22+
$err = curl_error($curl);
23+
24+
curl_close($curl);
25+
26+
if ($err) {
27+
echo "cURL Error #:" . $err;
28+
} else {
29+
echo $response;
30+
}

viber.php

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
require_once 'menu.php';
4+
5+
file_put_contents("viber.json",file_get_contents("php://input"));
6+
$viber = file_get_contents("viber.json");
7+
$viber = JSON_decode($viber);
8+
9+
function send($message){
10+
11+
$curl = curl_init();
12+
13+
curl_setopt_array($curl, array(
14+
CURLOPT_URL => "https://chatapi.viber.com/pa/send_message",
15+
CURLOPT_RETURNTRANSFER => true,
16+
CURLOPT_ENCODING => "",
17+
CURLOPT_MAXREDIRS => 10,
18+
CURLOPT_TIMEOUT => 30,
19+
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
20+
CURLOPT_CUSTOMREQUEST => "POST",
21+
CURLOPT_POSTFIELDS => JSON_encode($message),
22+
CURLOPT_HTTPHEADER => array(
23+
"Cache-Control: no-cache",
24+
"Content-Type: application/JSON",
25+
"X-Viber-Auth-Token: VIBER-TOKEN"
26+
),
27+
));
28+
29+
$response = curl_exec($curl);
30+
$err = curl_error($curl);
31+
32+
33+
curl_close($curl);
34+
35+
if ($err) {
36+
echo "cURL Error #:" . $err;
37+
} else {
38+
echo $response;
39+
}
40+
41+
}
42+
43+
if ($viber->event == "conversation_started"){
44+
$message['receiver'] = $viber->user->id;
45+
$message['type'] = "text";
46+
$message['text'] = "Welcome messages";
47+
$message['keyboard'] = [
48+
"Type" => "keyboard",
49+
"Revision" => 1,
50+
"DefaultHeight" => false,
51+
"Buttons" => $main_menu
52+
];
53+
send($message);
54+
exit;
55+
}
56+
57+
if ($viber->event == "message"){
58+
59+
if ($viber->message->text == "action1"){
60+
61+
$message['receiver'] = $viber->sender->id;
62+
$message['type'] = "text";
63+
$message['text'] = "Button 1";
64+
$message['keyboard'] = [
65+
"Type" => "keyboard",
66+
"Revision" => 1,
67+
"DefaultHeight" => false,
68+
"Buttons" => $main_menu
69+
70+
];
71+
send($message);
72+
exit;
73+
}
74+
}

0 commit comments

Comments
 (0)