Change Slack Status using API

By
 
import os
import time
import threading
from datetime import datetime, timezone, timedelta
from slack_sdk import WebClient
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler

# Initialize the Bolt app with the token and signing secret
app = App(token=os.getenv('SLACK_BOT_TOKEN'))
client = WebClient(token=os.getenv('SLACK_USER_TOKEN'))

# Dictionary to store user schedules
user_schedules = {}

def get_user_timezone(user_id):
    # Get the user's timezone from Slack
    user_info = client.users_info(user=user_id)
    tz_offset = user_info['user']['tz_offset']
    return timezone(timedelta(seconds=tz_offset))

def parse_status_message(text, tz):
    try:

About this template

This app allows users to schedule a status change on Slack by sending a direct message to the bot with their desired status, an optional emoji, and the start/end times. The app handles scheduling conflicts by prioritizing the most recent request and notifies the user accordingly. It also changes the user's Slack status at the scheduled times. If the user enters scheduling information in an incorrect format, the app sends a message explaining the correct format. The app uses the user's timezone from Slack for scheduling. Ensure the system clock is synchronized with the actual time to avoid scheduling issues. The app requires a user token to change the user's status.

Introduction to the Slack Status Scheduler Template

Welcome to the Slack Status Scheduler Template! This template allows you to create an application that enables users to schedule their Slack status updates in advance. Users can send a direct message to the bot with their desired status, an optional emoji, and the start/end times for the status. The app will handle scheduling conflicts, notify the user accordingly, and change the user's Slack status at the scheduled times. This guide will walk you through the steps to set up and use this template on the Lazy platform.

Getting Started with the Template

To begin using the Slack Status Scheduler Template, click "Start with this Template" on the Lazy platform. This will pre-populate the code in the Lazy Builder interface, so you won't need to copy, paste, or delete any code.

Initial Setup: Adding Environment Secrets

Before you can test and use the app, you'll need to set up some environment secrets within the Lazy Builder. These secrets include your Slack Bot Token and User Token, which are necessary for the app to interact with the Slack API on behalf of your Slack workspace and users.

  • Go to your Slack workspace and create a new Slack app if you haven't already.
  • From the Slack app settings, navigate to the OAuth & Permissions page and add the necessary scopes for your bot. You will need scopes like users:read, users.profile:write, and users.profile:read.
  • Install the app to your workspace and note down the Bot User OAuth Token and User OAuth Token provided by Slack.
  • In the Lazy Builder interface, go to the Environment Secrets tab.
  • Add two new secrets: SLACK_BOT_TOKEN and SLACK_USER_TOKEN, and paste the respective token values you obtained from Slack.

Test: Pressing the Test Button

Once you have set up the environment secrets, you can test the app by pressing the "Test" button. This will deploy the app and launch the Lazy CLI. If the app requires any additional user input, you will be prompted to provide it through the CLI.

Using the App

After testing and deploying the app, users can interact with it by sending direct messages to the bot in Slack. The message should contain the desired status, an optional emoji, and the start/end times in the following format:


:emoji: Status message HH:MM DD-MM-YYYY HH:MM DD-MM-YYYY

For example:


:vacation: On vacation 10:00 01-06-2023 17:00 05-06-2023

The app will parse the message, schedule the status update, and confirm the scheduling with the user. If the format is incorrect, the app will inform the user about the correct format.

Integrating the App

After deploying the app on Lazy, you can integrate it with your Slack workspace by inviting the bot to the channels where you want it to listen for status update requests or by sharing it with users who can send direct messages to the bot.

Remember, the app uses the user's timezone from Slack for scheduling, so ensure the system clock is synchronized with the actual time to avoid scheduling issues.

That's it! You've successfully set up the Slack Status Scheduler Template on Lazy. Users in your Slack workspace can now schedule their status updates with ease.

Category
Technology
Last published
April 7, 2024

More templates like this

Customisable Server Status Dashboard

This template allows you to create a dashboard to report about incidents regarding your server to your users.

Streamlit
Python

A bot that answers questions about data

Ask questions about your database via a chat bot. This chatbot connects to a SQLITE database, generates queries for it based on the schema and then runs the queries printing the response all in a nicely styled chat UI. This template is pre-loaded with sample data (car parts) to play around with - here are the sample data columns that you can query: part_number, part_name, price, units_left_in_stock, manufacturer .

Python
Flask

WhatsApp Bot Builder

The WhatsApp Bot Builder app allows users to create a WhatsApp bot that responds to every message with "Hello World".

WhatsApp
Twilio
Python
Home
/
Change Slack Status using API