首页 > 解决方案 > 将使用事件 api(HTTP 端点)的 Slack 应用程序迁移到 Slack 套接字模式

问题描述

我已经使用 PHP(HTTP 端点)实现了一个 Slack 应用程序。在 slack 斜杠命令下,我插入了打开对话框 (Slack Modal) 的请求 URL(指向我用 PHP 编写的代码以打开 Slack Modal 的 HTTP 端点)。提交表单后,它将转到 GitHub,我已在交互性和快捷方式(有效负载)的请求 URL 下插入了我用 PHP 编写的代码的 URL。

但由于安全措施,我想将应用程序迁移到套接字模式。有人可以让我知道如何在我的本地 VM 中实现 PHP 的套接字模式吗?

这是在松弛中打开对话框的代码

<?php

$slack_token = 'xoxb-******************';

// Dialog Form:
$dialog = [
  'callback_id' => 'git_issue',
  'title' => 'Add issue',
  'submit_label' => 'Create',
  'elements' => [
    [
      'type' => 'text',
      'label' => 'Name',
      'name' => 'name'
    ],
    [
      'type' => 'text',
      'label' => 'E-mail',
      'name' => 'email'
    ],
    $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://slack.com/api/dialog.open');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/x-www-form-urlencoded'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

// set the POST query parameters
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($query));

// execute curl request
$response = curl_exec($ch);

如何将应用程序迁移到套接字模式?

提前致谢

标签: phpwebsocketbotsslack-apislack-commands

解决方案


我使用这个自己编写的代码。可能它并不完美,但它有效:

<?php
    require 'vendor/autoload.php';
    
    // Slack config:
    $slack_token = 'xapp-*';
    $bot_token = 'xoxb-*';
    
    $url = false;
    
    $dialog = [
      'callback_id' => 'git_issue',
      'title' => 'Add new GitHub Issue',
      'submit_label' => 'Create',
      'elements' => [
        [
          'type' => 'text',
          'label' => 'Developer\'s Name',
          'name' => 'name'
        ],
      ]
    ];
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://slack.com/api/apps.connections.open');
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/x-www-form-urlencoded',
        'Authorization: Bearer '.$slack_token
    ]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    
    // set the POST query parameters
    curl_setopt($ch, CURLOPT_POST, true);
    
    // execute curl request
    $response = curl_exec($ch);
    
    // close
    curl_close($ch);
    
    if ($response) {
        $resp = json_decode($response);
        if (isset($resp->url)) {
            $url = $resp->url;
        }
    }
    
    if ($url) {
        echo 'Slack returned the websocket URL'.PHP_EOL;
        $client = new WebSocket\Client($url, ['timeout' => 3600]);
        while (true) {
            try {
                $message = $client->receive();
                
                echo $message.PHP_EOL.PHP_EOL;
    
                if ($message) {
                    $data = json_decode($message);
                    if (isset($data->payload)) {
                        if (isset($data->payload->command) && $data->payload->command == '/git') {
                            // Open Dialog:
    
                            // define POST query parameters
                            $query = [
                                'token' => $bot_token,
                                'dialog' => json_encode($dialog),
                                'trigger_id' => $data->payload->trigger_id,
                            ];
    
                            // define the curl request
                            $ch = curl_init();
                            curl_setopt($ch, CURLOPT_URL, 'https://slack.com/api/dialog.open');
                            curl_setopt($ch, CURLOPT_HTTPHEADER, [
                                'Content-Type: application/x-www-form-urlencoded'
                            ]);
                            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    
                            // set the POST query parameters
                            curl_setopt($ch, CURLOPT_POST, true);
                            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($query));
    
                            // execute curl request
                            $response = curl_exec($ch);
    
                            // close
                            curl_close($ch);
                            
                            $query = [
                                'envelope_id' => $data->envelope_id
                            ];
                            $client->text(json_encode($query));
                            // -----------
                        }
    
                        if (isset($data->payload->type) && $data->payload->type == 'dialog_submission') {
                            
                            // collect submitted data:
                            $name = $data->payload->submission->name;
                            
    
                            $query = [
                                'envelope_id' => $data->envelope_id,
                            ];
                            $client->text(json_encode($query));
                        }
                    }
                }
               
            } catch (\WebSocket\ConnectionException $e) {
                // Possibly log errors
                var_dump($e);
            }
        }
        $client->close();
    }

配置您的 Slack 应用程序并创建令牌:

  • 转到https://api.slack.com/apps页面并选择现有的应用程序或创建一个新应用程序
  • 进入“基本信息”页面,找到“App-Level Tokens”部分。创建一个新令牌。我使用这些范围“连接:写入,授权:读取”
  • 转到“OAuth & Permissions”页面并生成“Bot User OAuth Token”
  • 转到“斜线命令”页面并创建一个命令。在我的代码示例中,我们将其命名为“/git”
  • 转到“套接字模式”页面并使用滑动按钮启用该模式

对于通过 PHP 的 websockets 连接,我使用来自 GitHub的 Textalk/websocket-php实现。


推荐阅读