Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
secrete.py
ngrok.exe
Binary file modified __pycache__/secrete.cpython-37.pyc
Binary file not shown.
4 changes: 4 additions & 0 deletions hi_funtion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import re

def hi_funtion(text):
if text not in re.compile('[^ ㄱ-ㅣ가-힣]+')
90 changes: 26 additions & 64 deletions index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
# 개인 프로젝트 import #
# #
#######################################################################################
import secrete

import secrete
import Python_Slack_Chatbot_Project.hi_funtion
#######################################################################################
# #
# #
Expand All @@ -17,7 +18,7 @@
# -*- coding: utf-8 -*-
import re
import urllib.request

from urllib import parse
from bs4 import BeautifulSoup

from flask import Flask
Expand All @@ -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__)
Expand All @@ -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 = "<http://www.naver.com|여기를 클릭하세요> => 네이버입니다"
# # 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 "<h1>Server is ready.</h1>"
Expand Down
Empty file added urlParse.py
Empty file.