wlstp8473

weather_info

1 +import telegram
2 +from telegram.ext import Updater
3 +from telegram.ext import MessageHandler, Filters
4 +from bs4 import BeautifulSoup
5 +from selenium import webdriver
6 +import urllib.request as req
7 +import os
8 +from selenium.webdriver.support.ui import WebDriverWait
9 +from selenium.webdriver.support import expected_conditions as EC
10 +from selenium.webdriver.common.by import By
11 +
12 +from pprint import pprint
13 +import requests
14 +
15 +html = requests.get('https://search.naver.com/search.naver?query=날씨')
16 +#pprint(html.text)
17 +
18 +
19 +def weather_info_crawling():
20 + soup = BeautifulSoup(html.text, 'html.parser')
21 + data1 = soup.find('div', {'class': 'weather_box'})
22 +
23 + find_address = data1.find('span', {'class':'btn_select'}).text
24 + find_address_info = ('현재 위치: '+find_address + "\n")
25 +
26 + find_currenttemp = data1.find('span',{'class': 'todaytemp'}).text
27 + find_currenttemp_info=('현재 온도: '+find_currenttemp+'℃'+ "\n")
28 +
29 + data2 = data1.findAll('dd')
30 + find_dust = data2[0].find('span', {'class':'num'}).text
31 + find_dust_info=('현재 미세먼지: '+find_dust + "\n")
32 + result = find_address_info + ' ' + find_currenttemp_info + ' ' + find_dust_info
33 + return result
34 +
35 +
36 +def find_currenttemp_num():
37 + soup = BeautifulSoup(html.text, 'html.parser')
38 + data1 = soup.find('div', {'class': 'weather_box'})
39 +
40 + find_currenttemp = data1.find('span',{'class': 'todaytemp'}).text
41 + find_currenttemp_num = int(find_currenttemp)
42 + result = find_currenttemp_num
43 + return result
44 +
45 +#################################################################3
46 +#텔레그램 관련 코드
47 +token = "1721885449:AAHDGMbjSJfhXxML6nfSCpfiU7SghpL_vOE"
48 +id = "1657858421"
49 +bot = telegram.Bot(token)
50 +
51 +
52 +info_message = '''기능: (#오늘 날씨)''' ########기능 리스트 ##############33
53 +bot.sendMessage(chat_id=id, text=info_message)
54 +
55 +react_weather = '''오늘 날씨 봐봐'''
56 +
57 +
58 +updater = Updater(token=token, use_context=True)
59 +dispatcher = updater.dispatcher
60 +updater.start_polling()
61 +
62 +
63 +# 챗봇 응답
64 +def handler(update, context):
65 + user_text = update.message.text # 사용자가 보낸 메세지를 user_text 변수에 저장합니다.
66 +
67 + # 오늘 확진자 수 답장
68 + if ("오늘 날씨" in user_text):
69 + weather_info = weather_info_crawling()
70 + bot.send_message(chat_id=id, text= weather_info)
71 + bot.sendMessage(chat_id=id, text=react_weather)
72 + #bot.send_photo(chat_id=id, photo=open(r'C:\Users\user\Documents\project_opensw-chatbot\telegram_Messenger_Chatbot\img\weather_cloth_info.jpg', 'rb')) ###절대 경로니까 수정하세요!!!!!!
73 + if (find_currenttemp_num()>=27):
74 + bot.send_message(chat_id=id, text="와 미친,, 27도가 넘는데? 낼 무조건 시원하게 얇고 통풍 잘 되는 거 입어, 쪄 죽겠다 ㅋㅋㅋㅋ")
75 + elif (find_currenttemp_num()>=23):
76 + bot.send_message(chat_id=id, text="날씨가 23도 이상이네,,, ㅜㅜ 반팔에 반바지 정도 입으면 충분할 듯!")
77 + elif (find_currenttemp_num()>=20):
78 + bot.send_message(chat_id=id, text="20도 이상이면 밤엔 많이 쌀쌀할 듯 ㅠㅠ,, 진세 감기 조심해!")
79 + elif (find_currenttemp_num()>=15):
80 + bot.send_message(chat_id=id, text="헐 15도 이상이면 가을 날씨네,,,따뜻하게 입고가!!")
81 + else:
82 + update.message.reply_text("꽁꽁 싸매고 나가")
83 + ########################################################## 이어서 작업
84 +
85 + else:
86 + update.message.reply_text("ㅎㅎ")
87 +
88 +echo_handler = MessageHandler(Filters.text, handler)
89 +dispatcher.add_handler(echo_handler)
90 +
91 +
92 +
93 +