微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在没有输入事件的情况下向viber发送消息?

如何解决如何在没有输入事件的情况下向viber发送消息?

我有一个小问题,我有一个用于向viber发送消息的PHP脚本。问题在于,当我使用个人资料发送一些消息时,机器人会发送消息,但是我只想运行PHP脚本并发送消息进行聊天。

检查脚本:

<?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;
}
?>

当我通过浏览器运行脚本时,它转到ELSE,它应该立即发送消息...但是出现此错误

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

我应该放置一些静态接收者ID吗?以及如何获取某个用户的ID?

***更新*****

我在ELSE语句中添加了已订阅用户的静态ID ...现在,当我运行脚本时它会立即发送,但现在它会发送100条相同的消息。如何限制仅发送一条消息?

else {
    $message_to_replay = "Messageeeeeee";

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

解决方法

根据Viber documentation,您只能将消息发送到订阅您的漫游器的帐户:

send_message API允许帐户向订阅该帐户的Viber用户发送消息。

用户ID的要求是,它必须是已订阅漫游器的用户的用户ID,因此这应该是您检查的第一步。

编辑:我建议通过向您的机器人发送初始消息或其他东西来保存已订阅者的用户ID,然后您可以通过使用以前保存的用户ID向他们发送消息。

,

如果您的脚本在5秒钟内没有响应或HTML状态代码不等于200,则Viber服务器将尝试每分钟重新发送一次此Webhook。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。