【问题标题】:Viber Bot on PHP. onConversation problem with return welcome messagePHP 上的 Viber 机器人。返回欢迎消息的onConversation问题
【发布时间】:2021-10-19 19:48:27
【问题描述】:

onConversation 事件触发,但用户没有任何反应。 我试图修复错误,但消息尚未发送。所有其他机器人的功能都工作正常。

请告诉我错误在哪里以及如何解决。

$bot = new Bot(['token' => $apiKey]);
    $bot
        ->onConversation(function ($event) use ($bot, $botSender, $log ) {

            $log->info('onConversation ' . var_export($event, true));

            $context = $event->getContext();
            if ($context != "" && $context != null) {
                add_with_referral($event->getSender()->getId(), $event->getSender()->getName(), $context);
            } else {
                add_user($event->getSender()->getId(), $event->getSender()->getName(), $log);
            }

            return (new \Viber\Api\Message\Text)
                ->setSender($botSender)
                ->setText("Can i help you?");
        })

我使用 SDK Bogdaan/viber-bot

【问题讨论】:

    标签: php viber viber-bot


    【解决方案1】:

    Нашел решение。 Удаляем стандартное событие bot->onConversation()。 В самое начало файла bot.php пишем вот этот код:

    file_put_contents("viber.json",file_get_contents("php://input"));
    $viber = file_get_contents("viber.json");
    $viber = JSON_decode($viber);
    
    function send($message){
    
        $curl = curl_init();
    
        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://chatapi.viber.com/pa/send_message",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => JSON_encode($message),
            CURLOPT_HTTPHEADER => array(
                "Cache-Control: no-cache",
                "Content-Type: application/JSON",
                "X-Viber-Auth-Token: YOUR VIBER TOKEN"
            ),
        ));
    
        $response = curl_exec($curl);
        $err = curl_error($curl);
    
    
        curl_close($curl);
    
        if ($err) {
            echo "cURL Error #:" . $err;
        } else {
            echo $response;
        }
    
    }
    
    $main_menu = [[
        "Columns"=> 6,
        "Rows"=> 2,
        "BgColor" => "#808B96",
        "ActionType" => "reply",
        "ActionBody" => "subscribe_button",
        "Text" => "<font color=\"#F8F9F9\"><b>Subscribe</b></font>",
        "TextSize" => "regular"
    ]];
    
    if ($viber->event == "conversation_started"){
        $message['receiver'] = $viber->user->id;
        $message['type'] = "text";
        $message['text'] = "text";
        $message['keyboard'] = [
            "Type" => "keyboard",
            "Revision" => 1,
            "DefaultHeight" => false,
            "Buttons" => $main_menu
        ];
        send($message);
        exit;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-07
      • 2020-05-18
      • 2021-11-13
      • 2021-02-22
      • 2018-04-21
      • 2022-01-23
      • 2021-01-22
      • 2021-06-01
      相关资源
      最近更新 更多