by Lazy Sloth
Change Slack Status using API
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:
Frequently Asked Questions
How can this Slack Status Change app benefit businesses with remote teams?
The Slack Status Change app can greatly benefit businesses with remote teams by improving communication and transparency. Team members can easily schedule their availability or unavailability, such as for meetings, lunch breaks, or time off. This helps colleagues know when someone is reachable, reducing interruptions and improving overall team productivity. For example, a team member could set their status to "In a client meeting" from 2 PM to 4 PM, allowing others to plan their communications accordingly.
Can this app be used for tracking employee work hours or attendance?
While the Slack Status Change app is primarily designed for improving communication, it could potentially be adapted for basic time tracking or attendance monitoring. However, it's important to note that it relies on user-initiated status updates and may not be as accurate or comprehensive as dedicated time-tracking solutions. If a company wants to use it for this purpose, they should clearly communicate the policy to employees and possibly modify the app to include specific status options for clocking in and out.
How does this app handle different time zones for global teams?
The Slack Status Change app is designed with global teams in mind. It uses the get_user_timezone
function to retrieve each user's timezone from their Slack profile. This ensures that all scheduled status changes are correctly timed regardless of where team members are located. For instance, if a team member in New York schedules a status change for 9 AM their time, the app will automatically adjust the timing for a colleague viewing it in London, showing it as 2 PM their time.
How can I modify the app to support custom status emoji options?
To support custom status emoji options, you can modify the parse_status_message
function to recognize a predefined set of emoji keywords. Here's an example of how you could implement this:
```python def parse_status_message(text, tz): emoji_map = { "meeting": ":calendar:", "lunch": ":fork_and_knife:", "vacation": ":palm_tree:", # Add more mappings as needed }
parts = text.split()
first_word = parts[0].lower()
if first_word in emoji_map:
emoji = emoji_map[first_word]
status = ' '.join(parts[1:-4])
else:
emoji = None
status = ' '.join(parts[:-4])
# Rest of the function remains the same
...
```
This modification allows users to simply type keywords like "meeting" or "lunch" at the beginning of their message, and the app will automatically assign the corresponding emoji.
Can the Slack Status Change app be integrated with calendar applications for automatic status updates?
While the current implementation doesn't include calendar integration, it's certainly possible to extend the Slack Status Change app to work with calendar applications. You would need to add a function that periodically checks the user's calendar and schedules status updates accordingly. Here's a basic example of how you might implement this:
```python import datetime from google.oauth2.credentials import Credentials from googleapiclient.discovery import build
def check_calendar_and_update_status(user_id): # This assumes you've set up Google Calendar API access creds = Credentials.from_authorized_user_file('token.json', ['https://www.googleapis.com/auth/calendar.readonly']) service = build('calendar', 'v3', credentials=creds)
now = datetime.datetime.utcnow().isoformat() + 'Z'
events_result = service.events().list(calendarId='primary', timeMin=now,
maxResults=10, singleEvents=True,
orderBy='startTime').execute()
events = events_result.get('items', [])
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
end = event['end'].get('dateTime', event['end'].get('date'))
status = f"In a meeting: {event['summary']}"
schedule_status(user_id, status, ":calendar:", start, end)
# You would then need to run this function periodically for each user ```
This integration would allow the Slack Status Change app to automatically update a user's status based on their calendar events, further enhancing its utility for businesses.
Created: | Last Updated:
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
, andusers.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
andSLACK_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.
Template Benefits
-
Improved Time Management: This template allows employees to easily schedule their status changes in advance, helping teams better manage their time and expectations around colleague availability.
-
Enhanced Communication: By automating status updates, this tool ensures that team members are always aware of their colleagues' current status, reducing miscommunication and improving overall team coordination.
-
Increased Productivity: With automatic status changes, employees can focus on their work without the need to manually update their status, leading to fewer interruptions and increased productivity.
-
Better Work-Life Balance: The ability to schedule status changes in advance supports flexible working arrangements and helps employees maintain a healthier work-life balance by clearly communicating their working hours or time off.
-
Streamlined Leave Management: This template can be particularly useful for managing and communicating various types of leave (e.g., vacation, sick leave, or personal time off), making it easier for both employees and managers to track and manage time off.