【问题标题】:Make Slack API buttons "do something"让 Slack API 按钮“做某事”
【发布时间】:2020-07-01 22:46:16
【问题描述】:

编辑:因为我问了这个问题,所以我创建了一个 ngrok URL,以便在用户按下按钮时接收 Slack POST。

但是,每次我按下按钮时,我都会收到:"POST / HTTP/1.1" 404 -

我使用的是本地 Flask URL,代码如下:

from flask import Flask, request

app = Flask(__name__)


@app.route('/payload', methods=['POST'])
def incoming_slack_message():
    req = request.get_json(Force=True)
    info = request.form['channel_id']
    print(req)
    print(info)
    print('did it work???')
    return 'action successful'


@app.route('/slack/blocks', methods=['POST'])
def incoming_slack_options():
    req = request.get_json(Force=True)
    info = request.form['payload']
    print(req)
    print(info)
    print('Did it work here??')
    return 'ok'


if __name__ == '__main__':
    app.run(port=3000, debug = True)

我已经读到我需要在块中包含一个 callback_id 但无论何时我都会得到TypeError: 'NoneType' object is not subscriptable 这是我使用的块,少了 callback_id

{ "type": "section", "text": { "type": "mrkdwn", "text": "Once your machine is selected, click here." }, "accessory": { "type": "button", "text": { "type": "plain_text", "text": "Change", }, "value": "click_me_123", "action_id": "button" } }

我确信我的烧瓶代码有问题,因为我对 Flask 知之甚少。我不认为我的 Slack Block 有什么问题,但我仍然觉得应该有一个 callback_id。感谢您的时间。

【问题讨论】:

    标签: python url post flask slack-api


    【解决方案1】:

    好的,我得到了生成响应的按钮! Flask代码如下

    from flask import Flask, request, Response, jsonify
    import requests
    import json
    
    app = Flask(__name__)
    
    @app.route('/', methods=['POST'])
    def resp():
        data = request.json
        button_info = request.form['payload']
        webhook_url = 'webhook from slack'
        slack_data = { 'text': "How can I help you?"}
        response = requests.post(
        webhook_url, data=json.dumps(slack_data),
        headers={'Content-Type': 'application/json'}
        )
        return(jsonify(data), print(button_info))
    
    if __name__ == '__main__':
        app.run(port=3000, debug = True)
    
    

    button_info 将打印出所有数据。 我确信我可以摆脱其中一些行/导入,它仍然可以工作,但这将完成工作!

    *** 此代码与本地 Web 服务器的 ngrok 公共 URL 结合使用。

    brew cask install ngrok 后跟

    ngrok http 3000 匹配我代码中的 3000 端口

    webhook_url 来自 slack API 网站。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-13
      • 1970-01-01
      • 2013-05-04
      • 2012-10-24
      • 1970-01-01
      • 2013-07-19
      • 1970-01-01
      相关资源
      最近更新 更多