Skip to content

Instantly share code, notes, and snippets.

View jattoabdul's full-sized avatar
👑
ruling the world

Jatto Abdulqahhar jattoabdul

👑
ruling the world
View GitHub Profile
@jattoabdul
jattoabdul / part_2_worker.py
Created July 11, 2018 09:28
Worker Entry file for my tutorial series on "How to Build A Task Notification Bot for Slack with Python"
from app.actions import Actions
from app.utils.slackhelper import SlackHelper
# Main function
def main():
slackhelper = SlackHelper()
actions = Actions(slackhelper)
actions.notify_channel()
def notify_channel(self):
print('Worker is running..., waiting till 8:00')
while True:
curent_time = datetime.now()
current_hour = curent_time.hour
current_minute = curent_time.minute
if current_hour - 8 > 0:
sleep_time = 24 - current_hour + 8 - (current_minute / 60)
elif current_hour - 8 < 0:
sleep_time = 8 - current_hour - (current_minute / 60)
def notify_channel(self):
print('Worker is running...')
while True:
# curent_time = datetime.now()
# current_hour = curent_time.hour
# current_minute = curent_time.minute
# if current_hour - 8 > 0:
# sleep_time = 24 - current_hour + 8 - (current_minute / 60)
# elif current_hour - 8 < 0:
# sleep_time = 8 - current_hour - (current_minute / 60)
from flask_api import FlaskAPI
from config.env import app_env
from app.utils.slackhelper import SlackHelper
from flask import request, jsonify
from app.actions import Actions
from re import match
allowed_commands = [
'show-task'
def __perform_send_action(self, task_cells):
recipient = self.user_info['user']['id']
for index, row in enumerate(task_cells):
text_detail = (
'*Task #{} for {}:* \n\n'
'*Hey {},* Today is the check-in day for your writeup titled\n'
'`{}`.\n\n'
'Whats the status of the article?\n'
'PS: Please reply to this thread, the managers will review and reply you ASAP').format(
str(index + 1), row['Next Check-In'], row['Name'],
def show_tasks(self, date=None):
if date in ['today', 'tomorrow', 'yesterday']:
day_date_param = self.__convert_to_date(date)
task_cells = list(filter(lambda x: datetime.strptime(self.__num_suffix(x['Next Check-In']), '%d %B %Y').date() == day_date_param, self.sheet))
if task_cells:
self.__perform_send_action(task_cells)
else:
return {'text': 'No task assigned to be checked in {}, try another date'.format(date)}
else:
date_param = date.replace('-', ' ')
def help(self):
"""
Return the Available commands in the system and their usage format
"""
return {
'text': 'Available Commands: \n `/ranti my-task e.g. /ranti my-task` \n To get task assigned to you.\n'
' \n `/ranti show-task [date]{dth-month-year} e.g. /ranti show-task 5th-june-2018` \n Show all tasks for a particular date \n'
'\n `/ranti show-task [today] e.g. /ranti show-task today` \n Show all tasks for today \n'
'\n `/ranti show-task [tomorrow] e.g. /ranti show-task tomorrow` \n Show all tasks for tomorrow \n'
'\n `/ranti help` \n This help information \n \n Ranti Ver: 1.0'}
def my_tasks(self):
email = self.user_info['user']['profile']['email']
recipient = self.user_info['user']['id']
task_cells = list(filter(lambda x: x['Email Address'] == email, self.sheet))
for index, row in enumerate(task_cells):
text_detail = (
'*Task #{} for {}:* \n\n'
'*Hey {},* Today is the check-in day for your writeup titled\n'
'`{}`.\n\n'
'Whats the status of the article?\n'
def __convert_to_date(self, date_string):
today = date.today()
if date_string == 'today':
return today
elif date_string == 'yesterday':
return today - timedelta(days=1)
elif date_string == 'tomorrow':
return today + timedelta(days=1)
else:
return today
def __num_suffix(self, check_in_date):
"""
Strip the date suffix and return the date
Before comparing the date
"""
date_value = str(check_in_date).split(' ')
day_value = date_value[0][:-2]
date_value[0] = day_value
return ' '.join(date_value)