├── .env ├── README.md └── bot.py /.env: -------------------------------------------------------------------------------- 1 | SLACK_TOKEN_= 2 | SIGNING_SECRET_= 3 | ADMIN_TOKEN_= 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Slack-Bot 2 | 3 | # 💻 Launch Your Software Development Career Today! 4 | 5 | 🎓 **No degree? No problem!** My program equips you with everything you need to break into tech and land an entry-level software development role. 6 | 7 | 🚀 **Why Join?** 8 | - 💼 **$70k+ starting salary potential** 9 | - 🕐 **Self-paced:** Complete on your own time 10 | - 🤑 **Affordable:** Low risk compared to expensive bootcamps or degrees 11 | - 🎯 **45,000+ job openings** in the market 12 | 13 | 👉 **[Start your journey today!](https://techwithtim.net/dev)** 14 | No experience needed—just your determination. Future-proof your career and unlock six-figure potential like many of our students have! 15 | -------------------------------------------------------------------------------- /bot.py: -------------------------------------------------------------------------------- 1 | import slack 2 | import os 3 | from pathlib import Path 4 | from dotenv import load_dotenv 5 | from flask import Flask, request, Response 6 | from slackeventsapi import SlackEventAdapter 7 | import string 8 | from datetime import datetime, timedelta 9 | import time 10 | 11 | load_dotenv() 12 | 13 | app = Flask(__name__) 14 | slack_event_adapter = SlackEventAdapter( 15 | os.environ['SIGNING_SECRET_'], '/slack/events', app) 16 | 17 | client = slack.WebClient(token=os.environ['SLACK_TOKEN_']) 18 | BOT_ID = client.api_call("auth.test")['user_id'] 19 | 20 | message_counts = {} 21 | welcome_messages = {} 22 | 23 | BAD_WORDS = ['hmm', 'no', 'tim'] 24 | 25 | SCHEDULED_MESSAGES = [ 26 | {'text': 'First message', 'post_at': ( 27 | datetime.now() + timedelta(seconds=20)).timestamp(), 'channel': 'C01BXQNT598'}, 28 | {'text': 'Second Message!', 'post_at': ( 29 | datetime.now() + timedelta(seconds=30)).timestamp(), 'channel': 'C01BXQNT598'} 30 | ] 31 | 32 | 33 | class WelcomeMessage: 34 | START_TEXT = { 35 | 'type': 'section', 36 | 'text': { 37 | 'type': 'mrkdwn', 38 | 'text': ( 39 | 'Welcome to this awesome channel! \n\n' 40 | '*Get started by completing the tasks!*' 41 | ) 42 | } 43 | } 44 | 45 | DIVIDER = {'type': 'divider'} 46 | 47 | def __init__(self, channel): 48 | self.channel = channel 49 | self.icon_emoji = ':robot_face:' 50 | self.timestamp = '' 51 | self.completed = False 52 | 53 | def get_message(self): 54 | return { 55 | 'ts': self.timestamp, 56 | 'channel': self.channel, 57 | 'username': 'Welcome Robot!', 58 | 'icon_emoji': self.icon_emoji, 59 | 'blocks': [ 60 | self.START_TEXT, 61 | self.DIVIDER, 62 | self._get_reaction_task() 63 | ] 64 | } 65 | 66 | def _get_reaction_task(self): 67 | checkmark = ':white_check_mark:' 68 | if not self.completed: 69 | checkmark = ':white_large_square:' 70 | 71 | text = f'{checkmark} *React to this message!*' 72 | 73 | return {'type': 'section', 'text': {'type': 'mrkdwn', 'text': text}} 74 | 75 | 76 | def send_welcome_message(channel, user): 77 | if channel not in welcome_messages: 78 | welcome_messages[channel] = {} 79 | 80 | if user in welcome_messages[channel]: 81 | return 82 | 83 | welcome = WelcomeMessage(channel) 84 | message = welcome.get_message() 85 | response = client.chat_postMessage(**message) 86 | welcome.timestamp = response['ts'] 87 | 88 | welcome_messages[channel][user] = welcome 89 | 90 | 91 | def list_scheduled_messages(channel): 92 | response = client.chat_scheduledMessages_list(channel=channel) 93 | messages = response.data.get('scheduled_messages') 94 | ids = [] 95 | for msg in messages: 96 | ids.append(msg.get('id')) 97 | 98 | return ids 99 | 100 | 101 | def schedule_messages(messages): 102 | ids = [] 103 | for msg in messages: 104 | response = client.chat_scheduleMessage( 105 | channel=msg['channel'], text=msg['text'], post_at=msg['post_at']).data 106 | id_ = response.get('scheduled_message_id') 107 | ids.append(id_) 108 | 109 | return ids 110 | 111 | 112 | def delete_scheduled_messages(ids, channel): 113 | for _id in ids: 114 | try: 115 | client.chat_deleteScheduledMessage( 116 | channel=channel, scheduled_message_id=_id) 117 | except Exception as e: 118 | print(e) 119 | 120 | 121 | def check_if_bad_words(message): 122 | msg = message.lower() 123 | msg = msg.translate(str.maketrans('', '', string.punctuation)) 124 | 125 | return any(word in msg for word in BAD_WORDS) 126 | 127 | 128 | @ slack_event_adapter.on('message') 129 | def message(payload): 130 | event = payload.get('event', {}) 131 | channel_id = event.get('channel') 132 | user_id = event.get('user') 133 | text = event.get('text') 134 | 135 | if user_id != None and BOT_ID != user_id: 136 | if user_id in message_counts: 137 | message_counts[user_id] += 1 138 | else: 139 | message_counts[user_id] = 1 140 | 141 | if text.lower() == 'start': 142 | send_welcome_message(f'@{user_id}', user_id) 143 | elif check_if_bad_words(text): 144 | ts = event.get('ts') 145 | client.chat_postMessage( 146 | channel=channel_id, thread_ts=ts, text="THAT IS A BAD WORD!") 147 | 148 | 149 | @ slack_event_adapter.on('reaction_added') 150 | def reaction(payload): 151 | event = payload.get('event', {}) 152 | channel_id = event.get('item', {}).get('channel') 153 | user_id = event.get('user') 154 | 155 | if f'@{user_id}' not in welcome_messages: 156 | return 157 | 158 | welcome = welcome_messages[f'@{user_id}'][user_id] 159 | welcome.completed = True 160 | welcome.channel = channel_id 161 | message = welcome.get_message() 162 | updated_message = client.chat_update(**message) 163 | welcome.timestamp = updated_message['ts'] 164 | 165 | 166 | @ app.route('/message-count', methods=['POST']) 167 | def message_count(): 168 | data = request.form 169 | user_id = data.get('user_id') 170 | channel_id = data.get('channel_id') 171 | message_count = message_counts.get(user_id, 0) 172 | 173 | client.chat_postMessage( 174 | channel=channel_id, text=f"Message: {message_count}") 175 | return Response(), 200 176 | 177 | 178 | if __name__ == "__main__": 179 | schedule_messages(SCHEDULED_MESSAGES) 180 | ids = list_scheduled_messages('C01BXQNT598') 181 | delete_scheduled_messages(ids, 'C01BXQNT598') 182 | app.run(debug=True) 183 | --------------------------------------------------------------------------------