import os
import random
import requests
from dotenv import load_dotenv
from slacker import Slacker

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
load_dotenv(os.path.join(BASE_DIR, '.env'))

def get_env_variable(var_name):
    """ Get the environment variable or return exception """
    try:
        return os.environ[var_name]
    except KeyError:
        error_msg = "Set the %s env variable" % var_name
        raise EnvironmentError(error_msg)

SLACK_API_KEY = get_env_variable('SLACK_API_KEY')

OPTED_IN_USERS = [
    'phildini',
    'nicholle',
    'clach04',
    'erich',
]

MESSAGES = [
    "You look nice today.",
    "There are people in your life who think you're amazing.",
    "Whatever you decide for your body is right. No one else knows better.",
    "You do not owe it to anyone to look a particular way.",
    "Please try not to take yourself for granted. You're important.",
    "Hey, when's the last time you had water? Maybe you should have some now!",
    "Somebody misses you right now.",
    "Don't forget to breathe.",
    "Sometimes when things are falling apart, they may be falling into place.",
    "You are somebody's reason to smile.",
    "You are amazing!",
    "Just keep swimming!",
]

slack = Slacker(SLACK_API_KEY)

def main():
    user = random.choice(OPTED_IN_USERS)
    message = random.choice(MESSAGES)
    print(("{} - {}").format(user, message))
    message_response = slack.chat.post_message(
        "@{}".format(user),
        message,
        username="BuddyBot",
        icon_emoji=":heart:",
    )
    if user != 'phildini':
        note_response = slack.chat.post_message(
            "@phildini",
            "Message sent to {}.".format(user),
            username="BuddyBot",
            icon_emoji=":heart:",
        )

def get_users_from_slack():
    response = slack.users.list()
    users = response.body.get('members')
    usernames = [
        user.get('name') for user in users if (
            not user['deleted'] and not user['is_bot']
        )
    ]
    return random.choice(usernames)



if __name__ == '__main__':
    main()
@@ -63,16 +63,25 @@
 d_dotenv
+, set_key
 %0Afrom sl
@@ -1229,16 +1229,56 @@
 ming!%22,%0A
+    %22What's going well with your day?%22,%0A
 %5D%0A%0Aslack
@@ -1331,36 +1331,23 @@
 r = 
-random.choice(OPTED_IN_USERS
+get_valid_user(
 )%0A  
@@ -1427,16 +1427,301 @@
 ssage))%0A
+    send_buddybot_message(message=message, user=user)%0A    if user != 'phildini':%0A        send_buddybot_message(%0A            message='Message sent to %7B%7D'.format(user),%0A            user='phildini',%0A        )%0A%0Adef send_buddybot_message(message, user=None, channel=None):%0A    if user:%0A    
     mess
@@ -1768,16 +1768,20 @@
         
+  
+  
 %22@%7B%7D%22.fo
@@ -1796,24 +1796,28 @@
 r),%0A        
+    
 message,%0A   
@@ -1813,16 +1813,20 @@
 essage,%0A
+    
         
@@ -1846,32 +1846,36 @@
 dyBot%22,%0A        
+    
 icon_emoji=%22:hea
@@ -1876,24 +1876,28 @@
 =%22:heart:%22,%0A
+    
     )%0A    if
@@ -1897,34 +1897,23 @@
 %0A    if 
-user != 'phildini'
+channel
 :%0A      
@@ -1914,19 +1914,22 @@
         
-not
+messag
 e_respon
@@ -1975,18 +1975,28 @@
    %22
-@phildini%22
+#%7B%7D%22.format(channel)
 ,%0A  
@@ -2009,42 +2009,15 @@
     
-%22M
+m
 essage
- sent to %7B%7D.%22.format(user)
 ,%0A  
@@ -2088,24 +2088,245 @@
 %0A        )%0A%0A
+def get_valid_user():%0A    user = random.choice(OPTED_IN_USERS)%0A    if user == get_env_variable('LAST_USER'):%0A        user = get_valid_user()%0A    set_key(os.path.join(BASE_DIR, '.env'), 'LAST_USER', user)%0A    return user%0A%0A
 def get_user

Refactor BuddyBot a bit