From 7ec4861fe348fee84bd643dcec42079b3b9df561 Mon Sep 17 00:00:00 2001 From: keenborder786 <21110290@lums.edu.pk> Date: Wed, 17 May 2023 02:38:14 +0500 Subject: [PATCH] [feat]: Slack Bot Added and .env file added --- README.md | 4 +++- config.py | 7 ++++++- main.py | 12 ++++++++++++ slack_bot/__init__.py | 1 + slack_bot/bot.py | 32 ++++++++++++++++++++++++++++++++ upwork/__init__.py | 2 -- upwork_bot/__init__.py | 2 ++ {upwork => upwork_bot}/bot.py | 15 +++++++++++++-- {upwork => upwork_bot}/client.py | 0 9 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 slack_bot/__init__.py create mode 100644 slack_bot/bot.py delete mode 100644 upwork/__init__.py create mode 100644 upwork_bot/__init__.py rename {upwork => upwork_bot}/bot.py (70%) rename {upwork => upwork_bot}/client.py (100%) diff --git a/README.md b/README.md index bf3e15b..f8adba2 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ -# Upwork SlackBot \ No newline at end of file +# Upwork SlackBot + +## TO-DO \ No newline at end of file diff --git a/config.py b/config.py index a6e33a7..a0820f9 100644 --- a/config.py +++ b/config.py @@ -1,5 +1,10 @@ import os +from dotenv import load_dotenv +load_dotenv() CONSUMER_KEY = os.environ['CONSUMER_KEY'] CONSUMER_SECRET = os.environ['CONSUMER_SECRET'] -QUERY = os.environ['QUERY'] \ No newline at end of file +QUERY = os.environ['QUERY'] +SLACK_BOT_TOKEN = os.environ['SLACK_BOT_TOKEN'] +SLACK_APP_TOKEN = os.environ['SLACK_APP_TOKEN'] +CHANNEL_ID = os.environ['CHANNEL_ID'] \ No newline at end of file diff --git a/main.py b/main.py index e69de29..716c36c 100644 --- a/main.py +++ b/main.py @@ -0,0 +1,12 @@ +from slack_bot import Slackbot +from upwork_bot import UpworkBot +from config import ( + QUERY, + SLACK_APP_TOKEN,CHANNEL_ID) +slack_bot = Slackbot(SLACK_APP_TOKEN,CHANNEL_ID) +upwork_bot = UpworkBot(QUERY) + +html_data = upwork_bot.get_data() +jobs_data = upwork_bot.parse_data(html_data) +formatted_response = upwork_bot.format_data(jobs_data) +slack_bot.send_update(formatted_response) \ No newline at end of file diff --git a/slack_bot/__init__.py b/slack_bot/__init__.py new file mode 100644 index 0000000..5b4ec8b --- /dev/null +++ b/slack_bot/__init__.py @@ -0,0 +1 @@ +from slack_bot.bot import Slackbot \ No newline at end of file diff --git a/slack_bot/bot.py b/slack_bot/bot.py new file mode 100644 index 0000000..a82e748 --- /dev/null +++ b/slack_bot/bot.py @@ -0,0 +1,32 @@ +from slack_bolt import App +from slack.errors import SlackApiError +from slack_bolt.adapter.socket_mode import SocketModeHandler + + + + +class Slackbot: + def __init__(self, slack_app_token,channel_id): + """ + + + + """ + self.app = App(token=slack_app_token) + # ID of channel you want to post message to + self.channel_id = channel_id + def send_update(self,message_payload): + """ + + + + """ + try: + # Call the conversations.list method using the WebClient + result = self.app.client.chat_postMessage( + channel=self.channel_id, + text = message_payload, + mrkdwn=True + ) + except SlackApiError as e: + print(f"Error: {e}") diff --git a/upwork/__init__.py b/upwork/__init__.py deleted file mode 100644 index a924e9b..0000000 --- a/upwork/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -from upwork.bot import Bot -from upwork.client import client \ No newline at end of file diff --git a/upwork_bot/__init__.py b/upwork_bot/__init__.py new file mode 100644 index 0000000..cf0b053 --- /dev/null +++ b/upwork_bot/__init__.py @@ -0,0 +1,2 @@ +from upwork_bot.bot import UpworkBot +from upwork_bot.client import Client \ No newline at end of file diff --git a/upwork/bot.py b/upwork_bot/bot.py similarity index 70% rename from upwork/bot.py rename to upwork_bot/bot.py index a54db3c..31c3462 100644 --- a/upwork/bot.py +++ b/upwork_bot/bot.py @@ -3,7 +3,7 @@ from bs4 import BeautifulSoup -class Bot: +class UpworkBot: def __init__(self,query): self.query = query def get_data(self): @@ -27,4 +27,15 @@ def parse_data(self, html_data): jobs_data = {} for job in job_list: jobs_data[job.find('a')['href']] = job.text - return jobs_data \ No newline at end of file + return jobs_data + + def format_data(self,jobs_data): + """ + + + + + """ + job_formatted_list = f"*Following are the new {self.query} jobs that have been posted* \n" + for job_link,job_text in jobs_data.items(): job_formatted_list += f" - https://www.upwork.com{job_link} \n" + return job_formatted_list diff --git a/upwork/client.py b/upwork_bot/client.py similarity index 100% rename from upwork/client.py rename to upwork_bot/client.py