首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP上的Viber机器人。返回欢迎消息时出现onConversation问题

PHP上的Viber机器人。返回欢迎消息时出现onConversation问题
EN

Stack Overflow用户
提问于 2021-08-17 23:53:48
回答 1查看 76关注 0票数 0

事件onConversation被触发,但用户什么也得不到。我试图修复错误,但消息尚未发送。所有其他僵尸程序的功能都运行正常。

请告诉我错误在哪里,以及我如何修复它。

代码语言:javascript
复制
$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

EN

回答 1

Stack Overflow用户

发布于 2021-08-18 21:14:49

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

代码语言:javascript
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68825196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档