Slack bot|slack boltの使用


ゆとりロボットを用いてSlash boltコマンドを作成し,メソッドを探す過程でPythonのツールとして使用するslask boltを発見した.
パンツロボットを使いたいチャンネルはパンツロボットを招待しなければなりません.

Slack


slash command

  • Slasickermandを使用すると、Sleekに関連付けられたアプリケーションにデータ負荷を送信できます.
  • 2ビルトインコマンドを除いて作成すればよい.
  • スラッシュコマンドを生成するには:

  • POST法
  • を用いる.
  • 要求URL:チャンネル
  • confirming receipt


  • ユーザからの要求があれば、応答としてHTTP 200を送信する必要がある.エラーメッセージが表示されます.

  • タイムアウトエラーが発生する場合は、3秒以内にメモで送信する必要があります.
  • Bolt SDKの使用


    Sleekは、SlackアプリケーションのPythonライブラリを構築するためのslack boltという最新のプラットフォーム機能を提供しています.したがって、対応するライブラリを使用して、スラッシュコマンドのイベントとそのビューを作成できます.

    App Initializing

    import os
    from slack_bolt import App
    
    # Initializes your app with your bot token and signing secret
    app = App(
        token=os.environ.get("SLACK_BOT_TOKEN"),
        signing_secret=os.environ.get("SLACK_SIGNING_SECRET")
    )
    slashコマンドを登録するには、commandメソッドとviewメソッドが必要です.
    可用性は次のとおりです.

    command


    それぞれdecoratorとmethodの例です.二者択一でいいです.
    # Use this method as a decorator
    @app.command("/echo")
    def repeat_text(ack, say, command):
        # Acknowledge command request
        ack()
        say(f"{command['text']}")
    
    # Pass a function to this method
    app.command("/echo")(repeat_text)

    view


    それぞれdecoratorとmethodの例です.
    handle submissionメソッド内のack()は、view submissionイベントを識別し、モードウィンドウを閉じ、パラメータとして入力されたclientを介してチャネルに必要なメッセージを送信することができる.
    # Use this method as a decorator
    @app.view("view_1")
    def handle_submission(ack, body, client, view):
        # Assume there's an input block with <code>block\_c</code> as the block_id and <code>dreamy\_input</code>
        hopes_and_dreams = view["state"]["values"]["block_c"]["dreamy_input"]
        user = body["user"]["id"]
        # Validate the inputs
        errors = {}
        if hopes_and_dreams is not None and len(hopes_and_dreams) <= 5:
            errors["block_c"] = "The value must be longer than 5 characters"
        if len(errors) > 0:
            ack(response_action="errors", errors=errors)
            return
        # Acknowledge the view_submission event and close the modal
        ack()
        # Do whatever you want with the input data - here we're saving it to a DB
    
    # Pass a function to this method
    app.view("view_1")(handle_submission)
    モードウィンドウを開く
    ユーザーがパンツにスラッシュを入力すると、対応するスラッシュのモデルウィンドウが表示されます.Modalは、ユーザデータを収集し、複数の情報をユーザに公開できるウィンドウである.slack boltを使用して作成できます.
    モードウィンドウを開くには、chat_postMessageおよびtrigger_idの有効なパラメータをview payloadメソッドに渡す必要がある.これにより、生成されたアプリケーションは、要求URLを介してviews.openを送信し、対応するviewを実行する.
    # Listen for a shortcut invocation
    @app.shortcut("open_modal")
    def open_modal(ack, body, client):
        # Acknowledge the command request
        ack()
        # Call views_open with the built-in client
        client.views_open(
            # Pass a valid trigger_id within 3 seconds of receiving it
            trigger_id=body["trigger_id"],
            # View payload
            view={
                "type": "modal",
                # View identifier
                "callback_id": "view_1",
                "title": {"type": "plain_text", "text": "My App"},
                "submit": {"type": "plain_text", "text": "Submit"},
                "blocks": [
                    {
                        "type": "section",
                        "text": {"type": "mrkdwn", "text": "Welcome to a modal with _blocks_"},
                        "accessory": {
                            "type": "button",
                            "text": {"type": "plain_text", "text": "Click me!"},
                            "action_id": "button_abc"
                        }
                    },
                    {
                        "type": "input",
                        "block_id": "input_c",
                        "label": {"type": "plain_text", "text": "What are your hopes and dreams?"},
                        "element": {
                            "type": "plain_text_input",
                            "action_id": "dreamy_input",
                            "multiline": True
                        }
                    }
                ]
            }
        )
    チャンネルにメッセージを送信
    result = client.chat_postMessage(
            channel=channel_id, 
            text="Hello world"
        )
    基本的には上記の形式であり、blockメッセージを送信する際にblockパラメータを追加すればよい.
    client.chat_postMessage(channel=user, blocks=blocks, text=msg)
    Blockにアクセスできる内容については、次のフォーマットを参照してください.
    [{"type": "section", "text": {"type": "plain_text", "text": "Hello world"}}]
    →postMessageメソッドを使用するにはtrigger_idが必要であることに注意してください.これは、自分が生成したゆったりした設定で確認して追加します.
  • の関連内容はここです。で説明されている.
  • を除き、より詳細な内容はここです。で説明されている.