首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >slack不使用slack和python烧瓶向通道发送消息。

slack不使用slack和python烧瓶向通道发送消息。
EN

Stack Overflow用户
提问于 2022-02-27 11:50:01
回答 1查看 1.1K关注 0票数 0
代码语言:javascript
复制
sql = sqlite3.connect('test1.db', check_same_thread=False)

env_path = Path('.') / '.env'
load_dotenv(dotenv_path=env_path)
app = Flask(__name__)

# SLACK_APP_TOKEN = os.environ["SLACK_APP_TOKEN"]
SLACK_BOT_TOKEN = os.environ["SLACK_BOT_TOKEN"]
SIGNING_SECRET = os.environ["SIGNING_SECRET"]
bolt_app = App(token=SLACK_BOT_TOKEN, signing_secret=SIGNING_SECRET, )
handler = SlackRequestHandler(bolt_app)
slack_event_adapter = SlackEventAdapter(os.environ['SIGNING_SECRET'],'/slack/events', app)
# slack_event_adapter = SlackEventAdapter(os.environ['SIGNING_SECRET'],'/slack/events', app)
client = slack.WebClient(token=os.environ["SLACK_BOT_TOKEN"])

BOT_ID = client.api_call("auth.test")['user_id']

'method 1'
@bolt_app.message("in")
def say_hello(message):
    user = message['user']
    print(user)
    client.chat_postMessage(channel='#slackbot2', text='Hello!')
'method 2`
@bolt_app.message("in")
def ask_who(message, say):
    say("_Who's there?_")
if __name__ == "__main__":
    app.run(port=5000, debug=True)

我正在使用松螺栓框架和烧瓶。当我想听我松懈的机器人应用程序中的" in“消息时,将msg发送到频道。它不发送,甚至不显示错误。

EN

回答 1

Stack Overflow用户

发布于 2022-04-14 20:35:13

请注意,您不能将‘#slackbot2’发送到通道,因为它不是一个通道id。

所以,修改一下您的代码:

代码语言:javascript
复制
@bolt_app.message("in")
def say_hello(client, message):
    user = message.get('user') # using get instead
    channel_id = message.get('channel') # using get
    print(user)
    client.chat_postMessage(channel=channel_id, text='Hello!')

在下面的示例中,有一个块回复的示例(块是Slack中的新文本选项),它监听单词H/hi H/hello.在您的例子中,您可以简单地使用行:@bolt_app.message("in")

代码语言:javascript
复制
@bolt_app.message(re.compile("(hi|hello|hey|help)", flags=re.IGNORECASE))
def reply_to_keyword(message, say, logger):
    """
    Messages can be listened for, using specific words and phrases.
    message() accepts an argument of type str or re.Pattern object
    that filters out any messages that don’t match the pattern.
    Note: your app *must* be present in the channels where this
    keyword or phrase is mentioned.
    Please see the 'Event Subscriptions' and 'OAuth & Permissions'
    sections of your app's configuration to add the following for
    this example to work:
    Event subscription(s):  message.channels
    Required scope(s):      channels:history, chat:write
    Further Information & Resources
    https://slack.dev/bolt-python/concepts#message-listening
    https://api.slack.com/messaging/retrieving
    """
    try:

        blocks = [
            {
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": "Hi there! This is just a test that everything works correctly!\nI can help you with filling your missing times by typing `/fill_time` at the message bar"
                },
                "accessory": {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "text": "Click Button",
                    },
                    "value": "button_value",
                    "action_id": "first_button"
                }
            }
        ]
        say(blocks=blocks)
    except Exception as e:
        logger.error(f"Error responding to message keyword 'hello': {e}")

欲了解更多信息- https://slack.dev/bolt-python/concepts#message-sending https://slack.dev/bolt-python/concepts#web-api

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71284512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档