1

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);
}
Alexander
  • 51
  • 2
  • 8
  • There is no loop or anything in the code you have shown us, so with the information we have so far, there is no reasonable explanation for why this should send a 100 message, instead of a single one. Or are you talking about this sending the same message, every time you run the script? Well if you don’t want to send the message any more the second time, than you will have to store the info, that you _already_ sent it, somewhere … – CBroe Sep 11 '20 at 12:51
  • I run script just once and it sends 100 messages. There is no loop in my script and I don't know why it sends a lot of messages. I was thinking about to store sent data in database and check if it's sent... but I thought there is some simpler solution. – Alexander Sep 11 '20 at 13:00
  • 1
    This appears to originally have been a script meant to handle webhooks requests. Maybe the service is sending requests to your webhook endpoint, but the input event is neither `webhook` or `message`? In that case, your script would just go into the else branch, and … send a message again. Don’t try to handle multiple things that are so fundamentally different in nature, in one single script. Remove this part from the webhook script, and go create an extra script for your message sending stuff, and see if you still experience the same problem then. – CBroe Sep 11 '20 at 13:05
  • Thanks, now it works. I have one more question. How to add bot to some group? I need to send that message once in one place and all users need to see. I can see all subscribed users, but I can't see their messages with bot – Alexander Sep 11 '20 at 13:36

2 Answers2

0

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.

dabins
  • 11
  • 1
  • I have found ID of one subscribed user and added that static value... Now it works when I run a script, but there is another problem... script sends 100 same messages. How can I limit it to 1? – Alexander Sep 11 '20 at 12:42
0

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.

wowkin2
  • 5,895
  • 5
  • 23
  • 66