-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZimaBlue.py
133 lines (94 loc) Β· 5.36 KB
/
ZimaBlue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import json
import requests
from bs4 import BeautifulSoup
from slacker import Slacker
from flask import Flask, request, make_response
from NoticeCrawler import NoticeCrawler
from CafeteriaCrawler import CafeteriaCrawler
from BusInfomationCrawler import BusInformation
from pypapago import Translator
slack = None
with open('config.json') as json_file:
token = json.load(json_file)["Bot Token"]
slack = Slacker(token)
cafeteriaUrl = 'https://coop.koreatech.ac.kr/dining/menu.php'
businformationUrl = 'https://hantalk.io/bus'
generalNoticeUrl = 'https://www.koreatech.ac.kr/kor/CMS/NoticeMgr/list.do?mCode=MN230' # μΌλ°κ³΅μ§
scholarshipNoticeUrl = 'https://www.koreatech.ac.kr/kor/CMS/NoticeMgr/scholarList.do?mCode=MN231' # μ₯ν곡μ§
bachelorNoticeUrl = 'https://www.koreatech.ac.kr/kor/CMS/NoticeMgr/bachelorList.do?mCode=MN233' # νμ¬κ³΅μ§
app = Flask(__name__)
# μ΄λ²€νΈ νΈλ€νλ ν¨μ
def event_handler(event_type, slack_event):
if event_type == "app_mention":
channel = slack_event["event"]["channel"]
userMessage = slack_event["event"]["blocks"][0]['elements'][0]['elements'][1]['text']
attachments_dict = dict()
attachments_dict['color'] = '#2398cf'
if '곡μ§' in userMessage :
if 'μ₯ν' in userMessage :
req = requests.get(scholarshipNoticeUrl)
noticeType = 'μ₯ν곡μ§'
elif 'νμ¬' in userMessage :
req = requests.get(bachelorNoticeUrl)
noticeType = 'νμ¬κ³΅μ§'
else :
req = requests.get(generalNoticeUrl)
noticeType = 'μΌλ°κ³΅μ§'
html = req.text
soup = BeautifulSoup(html, 'html.parser')
Notice = NoticeCrawler(soup, noticeType)
attachments_dict = Notice.crawling().getAnswer()
elif 'νμ' in userMessage :
req = requests.get(cafeteriaUrl)
html = req.text
soup = BeautifulSoup(req.content.decode('euc-kr','replace'),'html.parser')
Cafeteria = CafeteriaCrawler(soup)
attachments_dict['text'] = Cafeteria.crawling().getAnswer()
elif 'λ²μ' in userMessage :
pypapago = Translator()
attachments_dict['pretext'] = '*[λ²μ] ννκ³ λ λ§νλ€. *:penguin:'
attachments_dict['text'] = '```'+pypapago.translate(userMessage[3:])+'```'
attachments_dict['mrkdwn_in'] = ["text", "pretext"]
elif 'λ²μ€' in userMessage or 'λμ±' in userMessage:
req = requests.get(businformationUrl)
html = req.text
soup = BeautifulSoup(html, 'html.parser')
BusInfo = BusInformation(soup)
attachments_dict = BusInfo.crawling().getAnswer()
elif 'μλ
' in userMessage :
attachments_dict['text'] = 'λλ *μ§λ§λΈλ£¨*:small_blue_diamond: μ§λ¦¬λ₯Ό μ°Ύμ μ΄κ³³κΉμ§ μμ£ . \nμκ°μ΄ μΌλ§ λ¨μ§ μμμ΅λλ€. ~*μ΄ νλμ΄ μ μ λ§μ§λ§μ΄ λ κ² μ
λλ€.*~'
else :
attachments_dict['text'] = 'λ¬΄μ¨ λ§μΈμ§ λͺ¨λ₯΄κ² λ€μ..'
""" attachments_dict = dict()
attachments_dict['pretext'] = "attachments λΈλ‘ μ μ λνλλ text"
attachments_dict['title'] = "λ€λ₯Έ ν
μ€νΈ λ³΄λ€ ν¬κ³ λ³Όλλμ΄μ 보μ΄λ title"
attachments_dict['title_link'] = "https://corikachu.github.io"
attachments_dict['fallback'] = "ν΄λΌμ΄μΈνΈμμ λ
Έν°νΌμΌμ΄μ
μ 보μ΄λ ν
μ€νΈ μ
λλ€. attachment λΈλ‘μλ λνλμ§ μμ΅λλ€"
attachments_dict['text'] = "λ³Έλ¬Έ ν
μ€νΈ! 5μ€μ΄ λμ΄κ°λ©΄ *show more*λ‘ λ³΄μ΄κ² λ©λλ€.\n1\n2\n3\n4\n5\n6\n7\n8\n9\n1\n2\n3\n4\n5\n6\n7\n8\n9"
attachments_dict['mrkdwn_in'] = ["text", "pretext"] # λ§ν¬λ€μ΄μ μ μ©μν¬ μΈμλ€μ μ νν©λλ€.
attachments = [attachments_dict] """
#slack.chat.post_message(channel="#channel", text=None, attachments=attachments, as_user=True)
# attchment μνμ λ°λ 보λ΄λ μ½λλ‘.
slack.chat.post_message(channel, attachments=[attachments_dict],as_user= True)
# Image attachments
""" [
{
"fallback": "Required plain-text summary of the attachment.",
"text": "Optional text that appears within the attachment",
"image_url": "https://mblogthumb-phinf.pstatic.net/MjAxNjEwMjJfNjAg/MDAxNDc3MTM5MDkzMTY5.nTQZS9VKPU3Y1P0J-nOcN4JMz75qU00n09XpQcGJZkAg.3fNACGwA3s_2TSRQxnY6sQDokClABM5fUumyIXAYdQUg.PNG.bugman1303/JW_T-Rex.png?type=w2"
}
] """
return make_response("μ± λ©μ
λ©μμ§κ° 보λ΄μ‘μ΅λλ€.", 200, )
message = "[%s] μ΄λ²€νΈ νΈλ€λ¬λ₯Ό μ°Ύμ μ μμ΅λλ€." % event_type
return make_response(message, 200, {"X-Slack-No-Retry": 1})
@app.route("/slack", methods=["GET", "POST"])
def hears():
slack_event = json.loads(request.data)
if "challenge" in slack_event:
return make_response(slack_event["challenge"], 200, {"content_type": "application/json"})
if "event" in slack_event:
event_type = slack_event["event"]["type"]
return event_handler(event_type, slack_event)
return make_response("μ¬λ μμ²μ μ΄λ²€νΈκ° μμ΅λλ€.", 404, {"X-Slack-No-Retry": 1})
if __name__ == '__main__':
app.run('0.0.0.0', port=8080)