How to send message to viber without input event?

3.4k Views Asked by At

I have a little problem, I have a php script for sending messages to viber. The problem is that bot sends message when I send some message with my profile, but I want only to run php script and send message to chat.

Check the script:

<?php

use Alserom\Viber\Request\Type\SendMessage;

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$access_token = "TOKEN HERE";

$request = file_get_contents("php://input");
$input = json_decode($request, true);

if ($input['event'] == 'webhook') {
    $webhook_response['status'] = 0;
    $webhook_response['status_message'] = "ok";
    $webhook_response['event_tyes'] = "delivered";
    echo json_encode($webhook_response);
    die;
} else if ($input['event'] == 'message') {
    $text_received = $input['message']['text'];
    $sender_id = $input['sender']['id'];
    $sender_name = $input['sender']['name'];

    $message_to_replay = "hello";

    $data['auth_token'] = $access_token;
    $data['receiver'] = $sender_id;
    $data['type'] = "text";
    $data['text'] = $message_to_replay;
    sendMessage($data);
} else {
    $message_to_replay = "Messageeeeeee";

    $data['auth_token'] = $access_token;
    /* $data['receiver'] = $sender_id; */
    $data['type'] = "text";
    $data['text'] = $message_to_replay;
    sendMessage($data);
}

function sendMessage($data) {
    $url = "https://chatapi.viber.com/pa/send_message";
    $jsonData = json_encode($data);
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    $result = curl_exec($ch);
    return $result;
}
?>

When I run script through browser, it goes to ELSE and it should send message immediately... but I get this error:

{
"status": 999,
"status_message": "Bad receiver ID",
"message_token": 5485177081717755848,
"chat_hostname": "SN-CHAT-20_"
}

Should I put some static receiver ID? And how to get ID of some user?

*** UPDATE *****

I have added static ID of subscribed user in ELSE statement... now it sends immediately when I run script, but now it sends 100 same messages. How to limit sending only one message?

else {
    $message_to_replay = "Messageeeeeee";

    $data['auth_token'] = $access_token;
    $data['receiver'] = "staticID";
    $data['type'] = "text";
    $data['text'] = $message_to_replay;
    sendMessage($data);
}
2

There are 2 best solutions below

0
On

If your script didn't responded in 5 seconds or responded with HTML status code not equal 200 - then Viber servers will try to resend this webhook every minute.

1
On

According to the Viber documentation, you can only send messages to accounts subscribing to your bot:

The send_message API allows accounts to send messages to Viber users who subscribe to the account.

A requirement for the user id is, that it has to be a user id of a user who subscribed to the bot, so this should be your first step to check.

Edit: I would recommend saving the user ids of those who subscribed by sending an initial message or something to your bot and then you can send messages to them by taking the previously saved user ids.