Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

팀 빌딩 api 서버에 올리기 #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.idea
.ipynb_checkpoints
.mypy_cache
.vscode
__pycache__
.pytest_cache
htmlcov
dist
site
.coverage
coverage.xml
.netlify
test.db
log.txt
Pipfile.lock
env3.*
env
docs_build
venv
docs.zip
archive.zip

# vim temporary files
*~
.*.sw?

#properties
properties.py
Empty file added __init__.py
Empty file.
Binary file added __pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added __pycache__/main.cpython-310.pyc
Binary file not shown.
Empty file added app/__init__.py
Empty file.
Binary file added app/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added app/__pycache__/main.cpython-310.pyc
Binary file not shown.
Empty file added app/api/__init__.py
Empty file.
Binary file added app/api/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Empty file added app/api/routes/__init__.py
Empty file.
Binary file added app/api/routes/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added app/api/routes/__pycache__/api.cpython-310.pyc
Binary file not shown.
15 changes: 15 additions & 0 deletions app/api/routes/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from fastapi import APIRouter, Body, Depends, HTTPException
from starlette.status import HTTP_400_BAD_REQUEST

from app.services.startMessage import startMessage
from app.services.makeTeamMessage import makeTeamMessage

router = APIRouter()

@router.get("/make-team/start")
def startMessageController():
startMessage()

@router.get("/make-team/end")
def makeTeamMessageController():
makeTeamMessage()
15 changes: 15 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from fastapi import FastAPI
from starlette.exceptions import HTTPException
from starlette.middleware.cors import CORSMiddleware

from app.api.routes.api import router as api_router

def get_application() -> FastAPI:

application = FastAPI()

application.include_router(api_router, prefix="/api")

return application

app = get_application()
Empty file added app/services/__init__.py
Empty file.
Binary file added app/services/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added app/services/__pycache__/service.cpython-310.pyc
Binary file not shown.
24 changes: 12 additions & 12 deletions dedenziBot.py → app/services/dedenziBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
class SlackAPI:
def __init__(self):
self.client = WebClient(properties.TOKEN)

# 채널이 없으면 예외던지기
def get_channel_id(self, channel_name):
channel_id=""
result = self.client.conversations_list()

try:
channels = result.data['channels']
# 채널이 없으면 여기서 IndexError가 터짐
Expand All @@ -18,35 +18,35 @@ def get_channel_id(self, channel_name):
result = self.client.conversations_list(types=["private_channel"])
channels = result.data['channels']
channel = list(filter(lambda c: c["name"] == channel_name, channels))[0]

channel_id = channel["id"]

return channel_id

# 만약 return하는 값이 비어있거나 한명이라면, 아무도 모이지 않았어요...! 라는 메시지 던지는 예외처리
# reaction에 값이 없다면? 예외던지기
def get_reactions(self, channel_id, timestamp):
result = self.client.reactions_get(channel = channel_id, timestamp=timestamp)
jsonObject = result.data['message']
# 만약 reaction이 없다면, KeyError 터짐
reactions = jsonObject['reactions']

for reaction in reactions:
if reaction.get('name') == properties.TARGET_REACTION :
# 요기서 users가 없거나 1명 있으면 사람이 없다는 예외 던짐
return_result = reaction.get('users')
return_result = reaction.get('users')
if len(return_result) <= 1:
raise Exception('팀 만들만큼 사람이 없어요!')
return return_result
# list들의 list를 받아서 post한다.

# list들의 list를 받아서 post한다.
def post_message(self, channel_name, message_list):
for message in message_list:
self.client.chat_postMessage(channel = channel_name, text=message + properties.TEAM_MATCHING_MESSAGE)

def post_init_message(self, channel_name):
result = self.client.chat_postMessage(channel=channel_name, text= properties.SLACK_INIT_MESSAGE)
return result.data['ts']

def post_error_message(self, channel_name, error_contents):
self.client.chat_postMessage(channel=channel_name, text = error_contents)
self.client.chat_postMessage(channel=channel_name, text = error_contents)
30 changes: 15 additions & 15 deletions divide.py → app/services/divide.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,38 @@
def divid_member(members):
if type(members) is str:
members = list(members)

d_members = []

# set을 활용한 무작위 진행
members = set(members)
members = list(members)
total_member = len(members)

if total_member < properties.LIMITED_NUMBER_OF_PEOPLE:
return list(members)

# limit만큼 데이터를 집어넣어서 list를 만든다.
# list를 모으는 list를 만든다.
# 저장된 데이터를 return한다.
# 저장된 데이터를 return한다.
while len(members) >= properties.LIMITED_NUMBER_OF_PEOPLE:
temp_list = []
for i in range(0, properties.LIMITED_NUMBER_OF_PEOPLE):
temp_list.append(members.pop())
# print(temp_list)
d_members.append(temp_list)

# 만약, 팀 갯수가 남은 사람 수보다 작다면, 남은 사람들로 팀을 하나 만든다.
if len(d_members) < len(members):
d_members.append(members)
else:
else:
for member_list in d_members:
if len(members) <= 0:
if len(members) <= 0:
break
member_list.append(members.pop())

return d_members

def member_lists_to_message_lists(member_list):
# member_list가 만약 여러 팀일 때면, user들이 들어가는 String list로 변환해주기
# member_list에 list가 하나라면, user들이 들어가는 String 변환해서 던져주기
Expand All @@ -47,13 +47,13 @@ def member_lists_to_message_lists(member_list):
user_mention_list = ""
for user in user_list:
user_mention_list += '<@'+user+'> '
result_lists.append(user_mention_list)
result_lists.append(user_mention_list)
user_mention_list = ""

else :
user_mention_list += '<@'+user_list+'> '

if len(user_mention_list) != 0:
result_lists.append(user_mention_list)
return result_lists

return result_lists
25 changes: 25 additions & 0 deletions app/services/makeTeamMessage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from . import dedenziBot
import properties
from . import divide
import sys

def makeTeamMessage():
slack = dedenziBot.SlackAPI()

try:
channel_id = slack.get_channel_id(properties.CHANNEL_NAME)
# 만약에 숫자 없으면 예외처리
result = slack.get_reactions(channel_id, sys.argv[1])
divid_members = divide.divid_member(result)
members_message_form = divide.member_lists_to_message_lists(divid_members)
print(members_message_form)
slack.post_message(properties.CHANNEL_NAME, members_message_form)

except IndexError:
print("해당하는 메시지가 존재하지 않아요! timestamp를 다시 확인해주세요!")

except KeyError:
slack.post_error_message(channel_id, "등록된 이모지가 없는데요??? 확인 후 재시도 부탁드려요!")

except Exception as e:
slack.post_error_message(channel_id, "참여자가 너무 적네요..! 저 바빠요.")
11 changes: 11 additions & 0 deletions app/services/startMessage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from . import dedenziBot
import properties

def startMessage():
slack = dedenziBot.SlackAPI()

try:
ts = slack.post_init_message(properties.CHANNEL_NAME)
print(properties.CHANNEL_NAME)
except:
print("없는 채널이에요! 채널 이름을 다시 설정하세요!")
File renamed without changes.
24 changes: 0 additions & 24 deletions makeTeamMessage.py

This file was deleted.

Loading