Last active
February 12, 2024 14:54
-
-
Save kjenney/21106a7a50a97e6dd828c48150511cc7 to your computer and use it in GitHub Desktop.
Slack App with Socket Mode Sending Interactive Messages and Client Sending Non-Interactive Messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import logging | |
from threading import Thread | |
from slack_bolt import App | |
from slack_bolt.adapter.socket_mode import SocketModeHandler | |
import os | |
import threading | |
import time | |
logging.basicConfig(level=logging.DEBUG) | |
# Replace with your actual values | |
SLACK_BOT_TOKEN = os.environ["SLACK_BOT_TOKEN"] | |
SLACK_APP_TOKEN = os.environ["SLACK_APP_TOKEN"] | |
SLACK_CHANNEL_ID = os.environ["SLACK_CHANNEL"] | |
# Define thread-safe function | |
lock = threading.Lock() | |
def send_direct_message_without_trigger(app): | |
with lock: | |
app.client.chat_postMessage(channel=SLACK_CHANNEL_ID, text="Testing sending message") | |
# Initialize Slack app | |
app = App(token=SLACK_BOT_TOKEN) | |
# Add command handler (optional) | |
@app.command("/test") | |
def hello_command(ack, body): | |
user_id = body["user_id"] | |
ack(f"Hi <@{user_id}>!") | |
# Background thread function | |
def background_process(): | |
while True: | |
send_direct_message_without_trigger(app) | |
time.sleep(30) # Adjust interval as needed | |
# Create and start the thread | |
thread = Thread(target=background_process) | |
thread.daemon = True | |
thread.start() | |
# Run the app | |
SocketModeHandler(app, SLACK_APP_TOKEN).start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment