【问题标题】:Python Slackbot not Showing as active and Not respondingPython Slackbot 未显示为活动且未响应
【发布时间】:2020-09-01 23:09:08
【问题描述】:

slackbot 未显示为活动且未响应。我已经使用 ngrok 从我的 localhost 设置隧道,以允许验证 slack 机器人。在 slack 上,它显示请求 URL 已经过验证。我还订阅了 slack 事件。

我正在关注https://medium.com/developer-student-clubs-tiet/how-to-build-your-first-slack-bot-in-2020-with-python-flask-using-the-slack-events-api-4b20ae7b4f86,以使该机器人正常工作。任何帮助都会很棒。谢谢。

from flask import Flask, Response
from slackeventsapi import SlackEventAdapter
import os
from threading import Thread
from slack import WebClient


# This `app` represents your existing Flask app
app = Flask(__name__)

greetings = ["hi", "hello", "hello there", "hey"]

SLACK_SIGNING_SECRET = os.environ['SLACK_SIGNING_SECRET']
slack_token = os.environ['SLACK_BOT_TOKEN']
VERIFICATION_TOKEN = os.environ['VERIFICATION_TOKEN']

#instantiating slack client
slack_client = WebClient(slack_token)

# An example of one of your Flask app's routes
@app.route("/")
def event_hook(request):
    json_dict = json.loads(request.body.decode("utf-8"))
    if json_dict["token"] != VERIFICATION_TOKEN:
        return {"status": 403}

    if "type" in json_dict:
        if json_dict["type"] == "url_verification":
            response_dict = {"challenge": json_dict["challenge"]}
            return response_dict
    return {"status": 500}
    return


slack_events_adapter = SlackEventAdapter(
    SLACK_SIGNING_SECRET, "/slack/events", app
)  


@slack_events_adapter.on("app_mention")
def handle_message(event_data):
    def send_reply(value):
        event_data = value
        message = event_data["event"]
        if message.get("subtype") is None:
            command = message.get("text")
            channel_id = message["channel"]
            if any(item in command.lower() for item in greetings):
                message = (
                    "Hello <@%s>! :tada:"
                    % message["user"]  # noqa
                )
                slack_client.chat_postMessage(channel=channel_id, text=message)
    thread = Thread(target=send_reply, kwargs={"value": event_data})
    thread.start()
    return Response(status=200)


# Start the server on port 3000
if __name__ == "__main__":
  app.run(port=3000)

【问题讨论】:

  • "app_mention" 帮助了我很多。感谢您的代码。

标签: python-3.x slack slack-api


【解决方案1】:

这是我遇到的错误代码: urllib.error.URLError:

这把我带到了这个 StackOverflow 线程: Scraping: SSL: CERTIFICATE_VERIFY_FAILED error for http://en.wikipedia.org

在 python 文件夹中运行认证文件允许 slack 机器人与通道交互。机器人现在正在工作,但未显示为活动状态。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-08
    • 2017-08-16
    • 2012-01-25
    • 2016-08-12
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 2021-11-07
    相关资源
    最近更新 更多