python-dialog-example icon indicating copy to clipboard operation
python-dialog-example copied to clipboard

Using deprecated slackclient

Open mike-bailey opened this issue 3 years ago • 1 comments

Howdy, looks like these samples use slackclient when Slack is telling everyone to use slack-sdk. Any way they could be updated? Thank you!

mike-bailey avatar Apr 10 '22 02:04 mike-bailey

@mike-bailey i was going through this example project and faced same problem. I am sharing changes as per recent version.

from flask import Flask, request, make_response, Response
import os
import json

from slack_sdk import WebClient

SLACK_BOT_TOKEN = os.environ["SLACK_BOT_TOKEN"]
SLACK_VERIFICATION_TOKEN = os.environ["SLACK_SIGNING_SECRET"]

slack_client = WebClient(token=SLACK_BOT_TOKEN)

app = Flask(__name__)

COFFEE_ORDERS = {}

user_id = "U06B48VC9QT" 

order_dm = slack_client.chat_postMessage(
  channel=user_id,
  text="I am Coffeebot ::robot_face::, and I'm here to help bring you fresh coffee :coffee:",
  attachments=[{
    "text": "",
    "callback_id": user_id + "coffee_order_form",
    "color": "#3AA3E3",
    "attachment_type": "default",
    "actions": [{
      "name": "coffee_order",
      "text": ":coffee: Order Coffee",
      "type": "button",
      "value": "coffee_order"
    }]
  }]
)

COFFEE_ORDERS[user_id] = {
    "order_channel": order_dm["channel"],
    "message_ts": "",
    "order": {}
}

@app.route("/slack/message_actions", methods=["POST"])
def message_actions():
    # Parse the request payload
    message_action = json.loads(request.form["payload"])
    user_id = message_action["user"]["id"]

    if message_action["type"] == "interactive_message":
        # Add the message_ts to the user's order info
        COFFEE_ORDERS[user_id]["message_ts"] = message_action["message_ts"]

        # Show the ordering dialog to the user
        open_dialog = slack_client.dialog_open(
            trigger_id=message_action["trigger_id"],
            dialog={
                "title": "Request a coffee",
                "submit_label": "Submit",
                "callback_id": user_id + "coffee_order_form",
                "elements": [
                    {
                        "label": "Coffee Type",
                        "type": "select",
                        "name": "meal_preferences",
                        "placeholder": "Select a drink",
                        "options": [
                            {
                                "label": "Cappuccino",
                                "value": "cappuccino"
                            },
                            {
                                "label": "Latte",
                                "value": "latte"
                            },
                            {
                                "label": "Pour Over",
                                "value": "pour_over"
                            },
                            {
                                "label": "Cold Brew",
                                "value": "cold_brew"
                            }
                        ]
                    }
                ]
            }
        )

        print(open_dialog)

        # Update the message to show that we're in the process of taking their order
        slack_client.chat_update(
            channel=COFFEE_ORDERS[user_id]["order_channel"],
            ts=message_action["message_ts"],
            text=":pencil: Taking your order...",
            attachments=[]
        )

    elif message_action["type"] == "dialog_submission":
        coffee_order = COFFEE_ORDERS[user_id]

        # Update the message to show that we're in the process of taking their order
        slack_client.chat_update(
            channel=COFFEE_ORDERS[user_id]["order_channel"],
            ts=coffee_order["message_ts"],
            text=":white_check_mark: Order received!",
            attachments=[]
        )

    return make_response("", 200)

if __name__ == "__main__":
    app.run(port=int(os.environ.get("PORT", 3000)))

ankitagrawal648 avatar Dec 28 '23 10:12 ankitagrawal648