Toggle navigation
Toggle navigation
This project
Loading...
Sign in
wlstp8473
/
telegram_Messenger_Chatbot
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
wlstp8473
2021-06-03 07:21:22 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
b58834f1e8302bf603ac75a765d6f31929b233f3
b58834f1
1 parent
acde8d31
covid.py
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
80 additions
and
0 deletions
covid.py
covid.py
0 → 100644
View file @
b58834f
import
telegram
from
telegram.ext
import
Updater
from
telegram.ext
import
MessageHandler
,
Filters
from
bs4
import
BeautifulSoup
from
selenium
import
webdriver
import
urllib.request
as
req
import
os
from
selenium.webdriver.support.ui
import
WebDriverWait
from
selenium.webdriver.support
import
expected_conditions
as
EC
from
selenium.webdriver.common.by
import
By
#크롤링 관련 함수
def
covid_num_crawling
():
code
=
req
.
urlopen
(
"https://search.naver.com/search.naver?where=nexearch&sm=top_hty&fbm=0&ie=utf8&query=
%
ED
%99%95%
EC
%
A7
%84%
EC
%9
E
%90
"
)
soup
=
BeautifulSoup
(
code
,
"html.parser"
)
info_num
=
soup
.
select
(
"div.status_today em"
)
result
=
int
(
info_num
[
0
]
.
string
)
+
int
(
info_num
[
1
]
.
string
)
return
result
def
covid_news_crawling
():
code
=
req
.
urlopen
(
"https://search.naver.com/search.naver?where=news&sm=tab_jum&query=
%
EC
%
BD
%94%
EB
%
A1
%9
C
%
EB
%82%98
"
)
soup
=
BeautifulSoup
(
code
,
"html.parser"
)
title_list
=
soup
.
select
(
"a.news_tit"
)
output_result
=
""
for
i
in
title_list
:
title
=
i
.
text
news_url
=
i
.
attrs
[
"href"
]
output_result
+=
title
+
"
\n
"
+
news_url
+
"
\n\n
"
if
title_list
.
index
(
i
)
==
2
:
break
return
output_result
#텔레그램 관련 코드
token
=
"1721885449:AAHDGMbjSJfhXxML6nfSCpfiU7SghpL_vOE"
id
=
"1657858421"
bot
=
telegram
.
Bot
(
token
)
info_message
=
'''진세 진세, 오늘 코로나 확진자 수 몇명인지 알아?? 완전 무섭다,,, (#몇명인데?)'''
bot
.
sendMessage
(
chat_id
=
id
,
text
=
info_message
)
news_message
=
'''관련 뉴스도 엄청 많이 보도되고 있더라,,, (#뉴스)'''
worried_message
=
'''진세도 꼭 마스크 끼고 손 씻고 주의해!! (#웅웅 너도)'''
react01_message
=
'''오키 ㅎㅎ'''
updater
=
Updater
(
token
=
token
,
use_context
=
True
)
dispatcher
=
updater
.
dispatcher
updater
.
start_polling
()
# 챗봇 응답
def
handler
(
update
,
context
):
user_text
=
update
.
message
.
text
# 사용자가 보낸 메세지를 user_text 변수에 저장합니다.
# 오늘 확진자 수 답장
if
(
user_text
==
"몇명인데?"
):
covid_num
=
covid_num_crawling
()
bot
.
send_message
(
chat_id
=
id
,
text
=
"오늘 확진자 수 : {} 명"
.
format
(
covid_num
))
bot
.
sendMessage
(
chat_id
=
id
,
text
=
news_message
)
# 코로나 관련 뉴스 답장
elif
(
user_text
==
"뉴스"
):
covid_news
=
covid_news_crawling
()
bot
.
send_message
(
chat_id
=
id
,
text
=
covid_news
)
bot
.
sendMessage
(
chat_id
=
id
,
text
=
worried_message
)
elif
(
user_text
==
"웅웅 너도"
):
bot
.
sendMessage
(
chat_id
=
id
,
text
=
react01_message
)
echo_handler
=
MessageHandler
(
Filters
.
text
,
handler
)
dispatcher
.
add_handler
(
echo_handler
)
Please
register
or
login
to post a comment