'slack bot does not send message to the channel using slack bolt and python flask
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)
I am using slack bolt framework and flask. when i wanna listen the "in" message in my slack bot apps and send msg to the channel. It does not send and even it does not show the error.
Solution 1:[1]
Note that you can't send '#slackbot2` to the channel because it is not a channel id.
So, fixing your code a bit:
@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!')
Here's an example for block reply ( block is the new text option in Slack )
In the below example, it listens to the words H/hi H/hello...
In your case, you can simply use your line:
@bolt_app.message("in")
@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}")
For more info - https://slack.dev/bolt-python/concepts#message-sending https://slack.dev/bolt-python/concepts#web-api
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tomer |
