diff --git a/.gitignore b/.gitignore index 656ad45..e55c4e5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ secrete.py +ngrok.exe \ No newline at end of file diff --git a/__pycache__/secrete.cpython-37.pyc b/__pycache__/secrete.cpython-37.pyc index 57b2fd4..e755ebe 100644 Binary files a/__pycache__/secrete.cpython-37.pyc and b/__pycache__/secrete.cpython-37.pyc differ diff --git a/hi_funtion.py b/hi_funtion.py new file mode 100644 index 0000000..b9c9de6 --- /dev/null +++ b/hi_funtion.py @@ -0,0 +1,4 @@ +import re + +def hi_funtion(text): + if text not in re.compile('[^ ㄱ-ㅣ가-힣]+') \ No newline at end of file diff --git a/index.py b/index.py index 749889e..4f92b5f 100644 --- a/index.py +++ b/index.py @@ -4,8 +4,9 @@ # 개인 프로젝트 import # # # ####################################################################################### -import secrete +import secrete +import Python_Slack_Chatbot_Project.hi_funtion ####################################################################################### # # # # @@ -17,7 +18,7 @@ # -*- coding: utf-8 -*- import re import urllib.request - +from urllib import parse from bs4 import BeautifulSoup from flask import Flask @@ -31,8 +32,8 @@ import csv from operator import itemgetter -SLACK_TOKEN = "?" -SLACK_SIGNING_SECRET = "?" +SLACK_TOKEN = secrete.oauth1 +SLACK_SIGNING_SECRET = secrete.basic_secrete1 app = Flask(__name__) @@ -41,76 +42,37 @@ slack_web_client = WebClient(token=SLACK_TOKEN) -# 크롤링 함수 구현하기 -def _crawl_music_chart(text): - if not "music" in text: - return "`@<봇이름> music` 과 같이 멘션해주세요." - - url = "https://music.bugs.co.kr/chart" - - sourcecode = urllib.request.urlopen(url).read() - soup = BeautifulSoup(sourcecode, "html.parser") - - keywords = [] - title = soup.find_all("p", class_="title") - artist = soup.find_all("p", class_="artist") - for i in range(10): - keywords.append(str(i+1)+"위 : " + title[i].get_text().strip() + " / " + artist[i].get_text().strip()) - - # 한글 지원을 위해 앞에 unicode u를 붙혀준다. - return u'\n'.join(keywords) - +def crawl_image_in_url(text): + #print(text[13:]) + text.strip() + + url = "https://haemukja.com/recipes?utf8=%E2%9C%93&sort=rlv&name=%EB%B3%B6%EC%9D%8C%EB%B0%A5" #+ #parse.quote(text) #임의 URL 현: 볶음밥. + source_code = urllib.request.urlopen(url).read() + soup = BeautifulSoup(source_code, "html.parser") + image_source = [] + image_blocks = [] + for src_source in soup.find_all("a", class_="call_recipe thmb"): + image_source.append(src_source.find("img").get("src")) + print(image_source) + for block in range(len(image_source)): + image_blocks.append(ImageBlock(image_url = image_source[block],alt_text="이미지가안뜰때보이는문구")) + #print(image_blocks) + return (image_blocks) # 챗봇이 멘션을 받았을 경우 @slack_events_adaptor.on("app_mention") def app_mentioned(event_data): channel = event_data["event"]["channel"] text = event_data["event"]["text"] - - message = _crawl_music_chart(text) + + message = crawl_image_in_url(text) slack_web_client.chat_postMessage( - channel=channel, - text=message + channel=channel, + blocks=extract_json(message) ) - - # slack_web_client.chat_postMessage( - # channel = event_data["event"]["channel"], - # text = " => 네이버입니다" - # # text = "*진한 글씨* _비스듬한 글씨_ ~취소선~ `print(source code)`" - # ) - - # block1 = ImageBlock(image_url="https://previews.123rf.com/images/isselee/isselee1101/isselee110100100/8652164-%ED%9D%B0%EC%83%89-%EB%B0%B0%EA%B2%BD-%EC%95%9E%EC%9D%98-%ED%86%A0%EB%81%BC-%ED%86%A0%EB%81%BC.jpg",alt_text="이미지가안뜰때보이는문구") - # block2 = SectionBlock(fields=["text1", "text2"]) - # my_blocks= [block1, block2] - # slack_web_client.chat_postMessage( - # channel=channel, - # blocks=extract_json(my_blocks) - # ) - - # 다른 사람이 메시지를 올렸을 때, 그 사람에게만 답장하기 - # 참고: https://api.slack.com/methods/chat.postEphemeral - # slack_web_client.chat_postEphemeral( - # channel=channel, - # user=event_data["event"]["user"], - # text="조용히 하세요!" - # ) - - ## name으로 id값 받아오기 - # text = ["slackbot", "hyena", "qkrjh0904"] - # response = slack_web_client.users_list() - # for user in response["members"]: - # if user["name"] in text: - # slack_web_client.chat_postMessage( - # channel = channel, - # text = user["name"] + " : " + user["id"] - # ) - # slack_web_client.chat_postEphemeral( - # channel="channel", user=user_id, text="뭐하고 있니?" - # ) - -# / 로 접속하면 서버가 준비되었다고 알려줍니다. + @app.route("/", methods=["GET"]) def index(): return "

Server is ready.

" diff --git a/urlParse.py b/urlParse.py new file mode 100644 index 0000000..e69de29