Answer the question
In order to leave comments, you need to log in
How to send message in viber using PHP API?
Hello! The task is: send a message to a user in Viber, knowing only his phone number .
Is it possible to do this with Viber API?
Answer the question
In order to leave comments, you need to log in
This code helped me
class Viber
{
private $url_api = "https://chatapi.viber.com/pa/";
private $token = "";
public function message_post
(
$from, // ID администратора Public Account.
array $sender, // Данные отправителя.
$text // Текст.
)
{
$data['from'] = $from;
$data['sender'] = $sender;
$data['type'] = 'text';
$data['text'] = $text;
return $this->call_api('post', $data);
}
private function call_api($method, $data)
{
$url = $this->url_api.$method;
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\nX-Viber-Auth-Token: ".$this->token."\r\n",
'method' => 'POST',
'content' => json_encode($data)
)
);
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
return json_decode($response);
}
}
$Viber = new Viber();
$Viber->message_post(
'01234567890A=',
[
'name' => 'Admin', // Имя отправителя. Максимум символов 28.
'avatar' => 'http://avatar.example.com' // Ссылка на аватарку. Максимальный размер 100кб.
],
'Test'
);
<?php
function send_message($receiverID,$TextMessage){
$curl = curl_init();
$json_data = '{
"receiver":"'.$receiverID.'",
"min_api_version":1,
"sender":{
"name":"NameBot",
"avatar":" avatar.example.com "
},
"tracking_data":"tracking data",
"type":"text",
"text":"'.$TextMessage.'"
}
';
$data = json_decode($json_data); // Convert the code to json
curl_setopt_array($curl, array(
CURLOPT_URL => " https://chatapi.viber.
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($data) , // send code
CURLOPT_HTTPHEADER => array(
"Cache-Control: no-cache",
"Content-Type: application/JSON",
"X-Viber-Auth-Token: 0000c419ece7d075-4c64680ae0e809a8-ab8000624a14e0000"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
}
// message in viber
send_message('yI8UmH+jb9ZAzyYtU/mYwg==',' Hello This is a bot!');
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question