-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram_chatbot.py
172 lines (141 loc) ยท 8.19 KB
/
telegram_chatbot.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import config
import requests
from telegram.ext import (Updater, Filters, CommandHandler, MessageHandler, CallbackQueryHandler)
from telegram import (ReplyKeyboardMarkup, KeyboardButton)
from crawler import (covid_data, naver_news)
from runner import logger
import time
command_help = '* ๋์๋ง - /help \n* ๊ตญ๋ด ์ด ํ์ง์ ์ - /total\n* ์๋๋ณ ํ์ง์ ์ - /citylines\n* ๋ค์ด๋ฒ๋ด์ค ๋ฐ๋ก ๋ฐ๊ธฐ - /naver_news\n* ์ฝ๋ก๋ ์๋ฆผ ๋ฑ๋ก - /notify\n* ๊ณต์ ๋ง์คํฌ ํ๋งค ํํฉ - /find_mask\n'
def _start(bot, update):
user = update.message.from_user
name = f'{user.last_name}, {user.first_name} ์ ์ ์ ์'
print(logger.info(name))
message = f'[ Show Corona Infos ]\n์ฝ๋ก๋ ์๋ฆฌ๋ฏธ๋ฅผ ์์ํฉ๋๋ค ๐๐ป\n\n๐ฒ ์ง๋ณ๊ด๋ฆฌ๋ณธ๋ถ, ๋ค์ด๋ฒ RSS๋ฅผ ์ด์ฉํด\n๊ตญ๋ด ์ฝ๋ก๋ ์ด ํ์ง์ ์์ ์ค์๊ฐ์ผ๋ก\n์ต์ ๋ด์ค๋ฅผ ๋ฐ์๋ณผ ์ ์์ต๋๋ค :)\n\n๐๐ปโโ๏ธโ/help ๋ช
๋ น์ด๋ฅผ ์
๋ ฅ ํ ์ด์ฉํ์ธ์ !\n์ฒซ ๋ช
๋ น์ด๋ 5~10์ด ์์๋ ์ ์์ต๋๋ค.\n\n์ง๋ฌธ ์ฌํญ์ [email protected]\n\nCreated by LEE, Woo-won\n'
bot.send_message(chat_id=update.message.chat_id, text=message)
def _help(bot, update):
message = f'๐ฌ ์ฝ๋ก๋ ์ฑ๋ด ๋์ฐ๋ฏธ์
๋๋ค :)\n\n{command_help}'
bot.send_message(chat_id=update.message.chat_id, text=message)
def _total(bot, update):
data = covid_data.get_total_cityline() # ๊ตญ๋ด ํ์ง์ ์ ๋ณด
bot.send_message(chat_id=update.message.chat_id, text=data)
def _citylines(bot, update):
data = covid_data.get_all_citylines() # ๊ตญ๋ด ๋์๋ณ ํ์ง์ ์ ๋ณด
bot.send_message(chat_id=update.message.chat_id, text=data)
def _naver_news(bot, update):
news = naver_news.get_current_news(update.message.from_user.first_name) # ๋ค์ด๋ฒ ๋ด์ค
bot.send_message(chat_id=update.message.chat_id, text=news, parse_mode='HTML')
def _find_mask(bot, update):
reply_markup = ReplyKeyboardMarkup(
[[KeyboardButton('ํ์ฌ ์์น ๊ณต์ ํ๊ธฐ',request_location=True)]],
resize_keyboard= True,
one_time_keyboard=False,
)
message = "[ ๊ณต์ ๋ง์คํฌ ํ๋งค ํํฉ ์กฐํ ]\n\n๐ฅ ๊ณต์ ๋ง์คํฌ ํ๋งค์ฒ ๋ฐ ์ฌ๊ณ ํํฉ์ ๋ณด๋ ค๋ฉด\n๐พ ์ฑ๋ด์ด ํ์ฌ ์์น๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋๋ก\nํ๋จ์ ๋ฌ ์์น ๊ณต์ ๋ฒํผ์ ํด๋ฆญํด์ฃผ์ธ์ !\n\n๐ซ ์ฑ๋ด์ ์๋ฌด๋ฐ ๋ฐ์์ด ์๋ค๋ฉด ๐ซ\n* ์ง์ ๊ฐ ๋๋ฐ์ด์ค ์ค์ ์ ๋ค์ด๊ฐ์\nํ
๋ ๊ทธ๋จ ์์น ์๋น์ค๋ฅผ ํ์ฉํด์ฃผ์ธ์.\n\n* ๋๋ ํ์ฌ ์์น๋ฅผ ์ง์ ์ ์กํด์ฃผ์ธ์.\n๋ฐฉ๋ฒ) 'ํด๋ฆฝ' ๋ชจ์ ์์ด์ฝ์ ํด๋ฆญํฉ๋๋ค.\n์ดํ์ '์์น' ๋ฒํผ ํด๋ฆญ ํ ํ์ฌ ์์น ์ ์ก !"
bot.send_message(chat_id=update.message.chat_id, text=message, reply_markup=reply_markup)
def _location(bot, update):
message = None
if update._effective_message:
message = update._effective_message
else:
message = update.message
current_pos = (message.location.latitude, message.location.longitude)
stores = public_mask_api(current_pos, 2)
mask = {
'plenty' : 'โ
์ถฉ๋ถํจ - 100๊ฐ ์ด์',
'some' : '๐ณ ์ ๋นํจ - 30๊ฐ ~ 99๊ฐ',
'few' : '๐ ๋ถ์กฑํจ - 2๊ฐ ~ 29๊ฐ',
'empty' : '๐ซ ํ๋งค์ค์ง - ์ฌ๊ณ ์์',
'break' : '๐ซ ํ๋งค์ค์ง - ์ฌ๊ณ ์์'
}
wcon_x, wcon_y = transcoord_api(current_pos[1], current_pos[0])
# q = ๊ณต์ ๋ง์คํฌํ๋งค์ฒ
message = f"[ ํ๋งค์ฒ ๋ฐ ์ฌ๊ณ ํํฉ ์กฐํ ]\n\n์ค์ ์์น, ํ์๋ ์ฌ๊ณ ์๋์ ์ค์ฐจ๊ฐ\n์์ ์ ์์ผ๋ ์ฐธ๊ณ ์ฉ์ผ๋ก ๋ด์ฃผ์ธ์ :)\n\n๐ฆ ๋ง์คํฌ ์ฌ๊ณ ์ํ ๋ถ๋ฅ ๐ฆ\n{mask['plenty']}\n{mask['some']}\n{mask['few']}\n{mask['empty']}\n์ถ์ฒ ํ๋งค์ฒ 2๊ณณ์ ํ์ฌ์์น ๊ธฐ์ค\n500m ์ด๋ด์ ์ฌ๊ณ ๋ง์ ์์
๋๋ค !\n\n๐บ ์ฃผ๋ณ ๋ชจ๋ ํ๋งค์ฒ ๋ณด๊ธฐ\n<a href='https://m.map.kakao.com/actions/searchView?q=%ea%b3%b5%ec%a0%81%eb%a7%88%ec%8a%a4%ed%81%ac%ed%8c%90%eb%a7%a4%ec%b2%98&wx={wcon_x}&wy={wcon_y}#!/all/map/place'>https://map.kakao.com</a>"
bot.send_message(chat_id=update.message.chat_id, text=message, parse_mode='HTML', disable_web_page_preview=1)
# ์ถ์ฒ ํ๋งค์ฒ ์์ ๊ฒฝ์ฐ
if not stores:
message = '๐
๐ปโโ๏ธ ํ์ฌ ์์น ๊ธฐ์ค 500m์ด๋ด์ ํ๋งค์ ์ด ์์ต๋๋ค.'
bot.send_message(chat_id=update.message.chat_id, text=message)
return
message = None
for store in stores:
time.sleep(1.2)
message = f"[ ์ถ์ฒ ํ๋งค์ฒ ๋ฐ๋ก ๊ฐ๊ธฐ ]\n๐จ ํ๋งค์ฒ - {store['name']}\n{mask[store['remain_stat']]}\nโฐ ์
๊ณ ์๊ฐ - {store['stock_at']}\n\n๐บ ๊ธธ์ฐพ๊ธฐ\n<a href='{store['url']}'>https://map.kakao.com</a>"
bot.send_message(chat_id=update.message.chat_id, text=message, parse_mode='HTML')
def _notify(bot, update):
message = '[ ์ฝ๋ก๋ ๋ด์ค ์๋ฆฌ๋ฏธ ๊ตฌ๋
]\n\n๐ฐ NAVER ์ฝ๋ก๋ ์ต์ ๋ด์ค๋ฅผ ๊พธ์คํ๊ฒ\n์ค์๊ฐ ์๋ฆผ์ผ๋ก ๋ฐ์๋ณด๋ ค๋ฉด ~!\n๐พ ํ
๋ ๊ทธ๋จ ์ฑ๋ด์ ์ฐธ์ฌํด๋ณด์ธ์ !\nhttps://t.me/ShowCoronaNews'
bot.send_message(chat_id=update.message.chat_id, text=message)
def _unknown(bot, update):
bot.send_message(chat_id=update.message.chat_id, text='ํด๋น ๋ช
๋ น์ด๋ ์กด์ฌํ์ง ์์ต๋๋ค ๐
๐ปโโ๏ธ')
def _error(bot, update, error):
print(logger.info(f'Error : {error}'))
def _run():
updater = Updater(config.TELEGRAM_TOKEN)
dispatcher = updater.dispatcher
# Run the bot & Register handler
updater.start_polling()
dispatcher.add_handler(CommandHandler('start', _start))
dispatcher.add_handler(CommandHandler('help', _help))
dispatcher.add_handler(CommandHandler('total', _total))
dispatcher.add_handler(CommandHandler('citylines', _citylines))
dispatcher.add_handler(CommandHandler('naver_news', _naver_news))
dispatcher.add_handler(CommandHandler('find_mask', _find_mask))
dispatcher.add_handler(CommandHandler('notify', _notify))
# Filters์ ์ญํ ?
dispatcher.add_handler(MessageHandler(Filters.location, _location))
dispatcher.add_handler(MessageHandler(Filters.command, _unknown)) # ๋ฑ๋ก๋์ง ์์ ์ปค๋งจ๋๊ฐ filter์ ๊ฑธ๋ ค์ ํธ๋ค๋ฌ๋ก ๋ฐ์
# bot's error handler
dispatcher.add_error_handler(_error)
# Run the bot until you press Ctrl-C
updater.idle()
updater.stop()
# ์ขํ WGS84 -> WCONGNAMUL ๋ณํ
def transcoord_api(x_loc, y_loc):
transcoord_url = f'https://dapi.kakao.com/v2/local/geo/transcoord.json?x={x_loc}&y={y_loc}&input_coord=WGS84&output_coord=WCONGNAMUL'
headers = {
'Content-Type': 'application/json; charset=utf-8',
'Authorization' : f'KakaoAK {config.KAKAO_API_KEY}'
}
res = requests.get(transcoord_url, headers= headers)
wcon = res.json()['documents'][0]
return wcon['x'], wcon['y']
# ๊ณต์ ๋ง์คํฌ API ํธ์ถ
def public_mask_api(pos, count):
public_mask = 'https://8oi9s0nnth.apigw.ntruss.com/corona19-masks/v1/storesByGeo/json'
payload = {
'lat': pos[0],
'lng': pos[1],
'm': 500
}
res = requests.get(public_mask, params=payload)
stores = res.json()['stores']
# ์ฌ๊ณ ์ํ - 100๊ฐ ์ด์(๋
น์): 'plenty' / 30๊ฐ ์ด์ 100๊ฐ๋ฏธ๋ง(๋
ธ๋์): 'some' / 2๊ฐ ์ด์ 30๊ฐ ๋ฏธ๋ง(๋นจ๊ฐ์): 'few' / 1๊ฐ ์ดํ(ํ์): 'empty' / ํ๋งค์ค์ง: 'break'
stat = {
'plenty' : [],
'some' : [],
'few' : [],
'empty' : [],
'break' : []
}
types = list(stat.keys()) # dict key ์์๋๋ก ์ ์ฅ๋จ
# ์ฌ๊ณ ์ํ ๋ถ๋ฅ
for store in stores:
for _type in types:
if store['remain_stat'] == _type:
stat[_type].append(store)
# plenty๋ถํฐ ์ญ ์ฐพ์ผ๋ฉด์ 3๊ฐ ์ถ์ถ
stores = []
complete = False
for _type in types:
if stat[_type] and complete is False:
for store in stat[_type]:
stores.append(store)
if len(stores) == count:
complete = True
break
s_x, s_y = transcoord_api(pos[1], pos[0])
# url ๋งํฌ ์ถ๊ฐ
for store in stores:
e_x, e_y = transcoord_api(store['lng'], store['lat'])
kakao_map = f"https://map.kakao.com/?sX={s_x}&sY={s_y}&sName=ํ์ฌ+์์น&eX={e_x}&eY={e_y}&eName={store['name']}"
store['url'] = kakao_map
return stores