使Slack API按钮“执行某些操作”

问题描述 投票:0回答:1

编辑:因为我问了这个问题,所以我创建了一个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个回答
0
投票

好,我已经获得了生成回复的按钮! Flask代码如下

from flask import Flask, request, Response
import requests
import json

app = Flask(__name__)

@app.route('/', methods=['POST'])
def resp():
    data = request.json
    #if data['event']['type'] == "app_mention":
    #user_text = data['payload']
    #print(data)
    #user = data['event']['user']
    webhook_url = 'Your Webhook URL'
    slack_data = { 'text': "How can I help you ?"}
    response = requests.post(
    webhook_url, data=json.dumps(slack_data),
    headers={'Content-Type': 'application/json'}
    )
    return(response )

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

我仍然不知道如何获取数据,#的零件产生错误,但我会继续寻找。

© www.soinside.com 2019 - 2024. All rights reserved.