P
P
Perokar2020-11-10 21:34:02
PHP
Perokar, 2020-11-10 21:34:02

How to send messages on Viber?

Hello. Gentlemen Guru and not really, the next question on the landing site is a simple form of two lines and you need to press a button so that the entered information is sent to the viber.
I created an account for the bot, found out its token. How to find out the id FIG knows (the instruction with postman does not work because requests for a token fly into the void)
The same piece of code hangs everywhere

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=', - где его взять этот id ? и как мне выполнять только часть c отправкой, какой смысл каждый раз создавать и описывать. 
    [
        'name' => 'Admin', // Имя отправителя. Максимум символов 28.
        'avatar' => 'http://avatar.example.com' // Ссылка на аватарку. Максимальный размер 100кб.
    ],
    'Test'
);


As I understand it, you need to use a webhook (what to bind it to?)
To the landing domain?
I call the script like this:
function connect () {
  $.ajax(
    {
      type:'POST',
      url:'Bot/viber.php',
      success: function(){console.log ('ok');}
    });
}


But I need to send 2 text variables to viber.php, which will then recover in the message when the button is pressed, the event hangs on the button.
I'm not particularly good with php or ajax requests.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Perokar, 2020-11-11
@Perokar

link mysite.ru/secret_path/h00k.php. error 404 doesn't open.
Maybe that's why I don't understand how webhookom can capture signed user id and send it to viber.php file

S
shurshur, 2020-11-10
@shurshur

The webhook needs to be set up. For example, mysite.ru/secret_path/h00k.php . Call https://chatapi.viber.com/pa/set_webhook to set it for your bot.
Next, you need to subscribe to your public account and write a message to it. This message will arrive in a webhook, it will have some user_id (the same user in different bots will have a different user_id for anonymity).
As long as a user is subscribed to a public account, messages can be sent to him by his user_id. Incoming messages from him and the statuses of sent ones will arrive in the webhook.
In general terms, yes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question