andy-franklin/slack-app-messaging

一个用于快速构建与 Slack API 一起使用的消息和对话框的软件包

dev-master 2018-11-05 13:21 UTC

This package is auto-updated.

Last update: 2024-09-06 09:19:16 UTC


README

本软件包提供了与 Slack API 一起使用的 Slack 响应的基本构建块。

安装

composer require andy-franklin/slack-app-messaging

构建交互式消息

https://api.slack.com/interactive-messages

    public function sendLoginOrRegisterMessageResponse($responseUrl)
    {
        $message = new Message();
        $attachment = new Attachment();
        $loginAction = new Action();
        $registerAction = new Action();
        
        $loginAction
            ->setName('login')
            ->setText('Login to your Account')
            ->setType('button')
            ->setValue('login');
            
        $registerAction
            ->setName('register')
            ->setText('Register an Account')
            ->setType('button')
            ->setValue('register');
            
        $attachment
            ->setTitle('Your Slack account needs to be linked with your account to continue.')
            ->addAction($loginAction)
            ->addAction($registerAction)
            ->setCallbackId('loginRegister');
            
        $message->addAttachment($attachment);
        
        //POST this message as JSON to the responseUrl from the original Slack request
        $client = new Client();
        $response = $client->post($responseUrl, [
            'body' => $this->serializer->serialize($message, 'json')
        ]);
        return $response;
    }

需要根据 callbackId 设置操作端点并采取适当的操作。

打开对话框

https://api.slack.com/dialogs

    public function sendLoginDialog($triggerId)
    {
        //Build the dialog with a single username field.
        $textElement = new TextElement();
        $textElement
            ->setName('username')
            ->setLabel('Username')
            ->setPlaceholder('Enter your username')
            ->setHint('The username you have registered');
        $dialog = new Dialog();
        $dialog
            ->setTitle('Login')
            ->setCallbackId('submit-login')
            ->setSubmitLabel('Submit')
            ->addElement($textElement);
        
        //Add the triggerId from the initial Slack request
        $dialogResponse = new DialogResponse();
        $dialogResponse
            ->setTriggerId($triggerId)
            ->setDialog($dialog);
        
        //Generate a \GuzzleHttp\Psr7\Request
        $request = $dialogResponse->getPostRequest('dialog.open', $this->serializer, $this->slackXoxpToken);
        
        //Send this request
        $client = new Client();
        $response = $client->send($request);
    }