Slack PHP API - 避免超时错误 [英] Slack PHP API - Avoid Timeout error

查看:26
本文介绍了Slack PHP API - 避免超时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Slack 自定义命令,但不太确定如何使用延迟消息,因为

  • 现在,当我调用 Yoda API 时,它给出了以下错误已达到超时".我读到了延迟响应,但不知道我应该如何从这里开始.
<块引用>

$chsres = curl_init();curl_setopt($chsres, CURLOPT_URL, "https://yoda.p.mashape.com/yoda?sentence=welcome+to+stack");curl_setopt($chsres, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($chsres, CURLOPT_SSL_VERIFYHOST, FALSE);curl_setopt($chsres, CURLOPT_VERBOSE, true);curl_setopt($chsres, CURLOPT_TIMEOUT, 45);curl_setopt($chsres, CURLOPT_RETURNTRANSFER, true);curl_setopt($chsres, CURLOPT_HTTPHEADER, array('Content-Type:application/json', "X-Mashape-Key:> deMeGoBfMvmshQSemozTqJEY9z0jp1eIhuAjsnx9cQAQsHUifD"));$resultchsres = curl_exec($chsres);回声 $resultchsres;

有人可以告诉我如何使用延迟响应消除超时错误吗?

更新代码:

$response_url = $_POST['response_url'];$text = $_POST['text'];$term = str_replace(' ', '+', $text);//https://paypal.slack.com/services/B0VQMHX8W#service_setup//初始响应200OK超时忽略用户中止(真);设置时间限制(0);ob_start();echo('{"response_type": "in_channel", "text": "正在检查中,请稍候..."}');标头($_SERVER["SERVER_PROTOCOL"] ." 200 OK");header("Content-Type: application/json");header('内容长度:'.ob_get_length());ob_end_flush();ob_flush();冲洗();$chsres = curl_init();curl_setopt_array($chsres, 数组(CURLOPT_URL =>"https://yoda.p.mashape.com/yoda?sentence=$term",CURLOPT_SSL_VERIFYPEER =>错误的,CURLOPT_SSL_VERIFYHOST =>错误的,CURLOPT_VERBOSE =>真的,CURLOPT_RETURNTRANSFER =>错误的,CURLOPT_HTTPHEADER =>array('Content-Type:application/json', "X-Mashape-Key: deMeGoBfMvmshQSemozTqJEY9z0jp1eIhuAjsnx9cQAQsHUifD"),CURLOPT_RETURNTRANSFER =>真的));$yodaresponse = curl_exec($chsres);$curl = curl_init();curl_setopt_array($curl, 数组(CURLOPT_URL =>$response_url,CURLOPT_POST =>1、CURLOPT_RETURNTRANSFER =>真的,CURLOPT_POSTFIELDS =>$yodaresponse));$resp = curl_exec($curl);var_dump($resp);curl_close($curl);

我仍然收到同样的错误该死——那个斜杠命令不起作用(错误消息:超时.在斜杠命令管理命令"

解决方案

你做的都是对的,只是需要改变顺序.

  1. 立即以 200 OK 响应响应原始请求.有关详细信息,请参阅此答案,但本质上:

    ignore_user_abort(true);ob_start();echo('{"response_type": "in_channel", "text": "正在检查中,请稍候..."}');标头($_SERVER["SERVER_PROTOCOL"] ." 200 OK");header("Content-Type: application/json");header('内容长度:'.ob_get_length());ob_end_flush();ob_flush();冲洗();

  2. 然后像您一样使用 curl 发出 Yoda API 请求

  3. 获得 Yoda 结果后,使用 curl 将它们发送到 $response_url 的 Slack,就像您正在做的那样.

I am trying to use Slack Custom command and not pretty sure how to use delayed messages since the Yoda Speak External API takes more than 3 seconds to respond.

I have done the following:

  • Sent the slack command /Yoda in my case and received the reponse_url.
  • Used the following to post the following to the response URL.

$data_string = '{"response_type": "in_channel", "text":"Checking,please wait..."}' ;
$chs = curl_init();
curl_setopt($chs, CURLOPT_URL, $response_url);
curl_setopt($chs, CURLOPT_POST, true);
curl_setopt($chs, CURLOPT_POSTFIELDS, $data_string); 
curl_setopt($chs, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($chs, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($chs, CURLOPT_RETURNTRANSFER, true);
curl_setopt($chs, CURLOPT_POST, 1);
curl_setopt($chs, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$results = curl_exec($chs);

  • Now, when I call the Yoda API, it gives the following error "Timeout was reached". I read about delayed responses but not sure how should I proceed from here.

$chsres = curl_init();
curl_setopt($chsres, CURLOPT_URL, "https://yoda.p.mashape.com/yoda?sentence=welcome+to+stack");
curl_setopt($chsres, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($chsres, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($chsres, CURLOPT_VERBOSE, true);
curl_setopt($chsres, CURLOPT_TIMEOUT, 45);
curl_setopt($chsres, CURLOPT_RETURNTRANSFER, true);
curl_setopt($chsres, CURLOPT_HTTPHEADER, array('Content-Type:application/json', "X-Mashape-Key:> deMeGoBfMvmshQSemozTqJEY9z0jp1eIhuAjsnx9cQAQsHUifD"));
$resultchsres = curl_exec($chsres);
echo $resultchsres;

Can someone please let me know how to get rid of the timeout error using delayed responses?

UPDATED CODE:

$response_url = $_POST['response_url'];
$text = $_POST['text'];

$term = str_replace(' ', '+', $text);

//https://paypal.slack.com/services/B0VQMHX8W#service_setup
//initial respond with 200OK for timeout
ignore_user_abort(true);
set_time_limit(0);
ob_start();
echo('{"response_type": "in_channel", "text": "Checking, please wait..."}');
header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
header("Content-Type: application/json");
header('Content-Length: '.ob_get_length());
ob_end_flush();
ob_flush();
flush();


    $chsres = curl_init();
    curl_setopt_array($chsres, array(
        CURLOPT_URL => "https://yoda.p.mashape.com/yoda?sentence=$term",
        CURLOPT_SSL_VERIFYPEER => FALSE,
        CURLOPT_SSL_VERIFYHOST => FALSE,
        CURLOPT_VERBOSE => true,
        CURLOPT_RETURNTRANSFER => FALSE,
        CURLOPT_HTTPHEADER => array('Content-Type:application/json', "X-Mashape-Key: deMeGoBfMvmshQSemozTqJEY9z0jp1eIhuAjsnx9cQAQsHUifD"),
        CURLOPT_RETURNTRANSFER => true
    ));
    $yodaresponse = curl_exec($chsres);

    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => $response_url,
        CURLOPT_POST => 1,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS => $yodaresponse
    ));

    $resp = curl_exec($curl);
    var_dump($resp);
    curl_close($curl);

I still get the same error "Darn – that slash command didn't work (error message: Timeout was reached). Manage the command at slash-command"

解决方案

You're doing all the right things, just need to change the order.

  1. Respond to the original request with a 200 OK response immediately. See this answer for details, but essentially:

    ignore_user_abort(true);
    ob_start();
    echo('{"response_type": "in_channel", "text": "Checking, please wait..."}');
    header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
    header("Content-Type: application/json");
    header('Content-Length: '.ob_get_length());
    ob_end_flush();
    ob_flush();
    flush();
    

  2. Then make the Yoda API request using curl, as you're doing

  3. Once you have the Yoda results, send them to Slack at $response_url using curl, as you're doing.

这篇关于Slack PHP API - 避免超时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆