def add_new_user_to_db(member_dict, channel_id): email = member_dict['profile'].get("email") new_replier = Replier(slack_id=member_dict["id"], name=member_dict["name"], email=email, channel_id=channel_id) # as we do not know yet which user is, we place them in level '1' and class 'limbo' new_replier.level_id = 1 new_replier.classe = "limbo" with session_scope() as session: session.add(new_replier) try: send_email(receiver_email=email,text_message=WELCOME_EXPERIA_EMAIL_TEXT, subject="ExperIA - Acesso Autorizado",filename='helpers/docs/ExperIA.pdf', receiver_id=member_dict["id"]) except: functions_log(message="It was not possible to send the email for user {}".format(member_dict["id"]), file_name="send_emails_log")
def handle_home_event(event_data): """ Handle events when someone makes a click on Home Tab. Automate the storage of new users in the database. """ user_new_id = event_data['event']['user'] with session_scope() as session: user_replier = Replier.get_replier_by_slack_id(session,slack_id=user_new_id) # if the user is not in the database yet, create a temporary register if user_replier == None: user_replier = Replier(channel_id="none") # or, if the user's slack_id is empty if len(user_replier.channel_id) <= 4: client = WebClient(token=SLACK_BOT_OAUTH_ACCESS_TOKEN) channels = client.conversations_list(types="im")["channels"] users = slack_client.users_list() # get info of new user from user_list and conversations_list for memb_dict in users["members"]: if memb_dict["id"] == user_new_id: channel_id = next((channel["id"] for channel in channels if channel["user"] == memb_dict["id"]), None) if user_replier.channel_id == "none": # create a new register add_new_user_to_db(memb_dict, channel_id) else: # fill channel_id if missing user_replier.channel_id = channel_id with session_scope() as session: session.add(user_replier) # create and show the Home Tab's format and Jit's link for each user HOME_message['blocks'][-6]['elements'][0]['url'] = "https://meet.jit.si/ExperIA" + user_new_id slack_client.views_publish(user_id=user_new_id, view=HOME_message)