Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ChatBotGroup/GBot.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
210 lines (170 sloc)
6.27 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import discord | |
import urllib.request | |
import urllib.parse | |
import re | |
import asyncio | |
from datetime import datetime | |
TOKEN = 'NTA0NzgyMjc4MjYyNzg0MDAy.Dspoqg.pLuBwseJIhILqFByxueoNQO5Isg' | |
client = discord.Client() | |
weatherList = ["cold", "hot", "weather", "typhoon", "forecast", "wind", "temperature"] | |
yes = ["yes"] | |
no = ["no"] | |
listTime = ["time", "day", "month", "year", "minute", "hour", "date"] | |
day = ["day"] | |
time = ["time"] | |
both = ["both"] | |
neither = ["neither"] | |
class gV(): | |
flagTimeQ = 0 | |
flagWeather = 0 | |
flagWeatherA = 0 | |
flagRandom = 0 | |
def timeFunction(text): | |
now = datetime.now() | |
currentYear = now.year | |
currentMonth = now.month | |
currentDay = now.day | |
currentHour = now.hour | |
currentMin = now.minute | |
if currentMonth == 1: | |
currentMonth = "January" | |
elif currentMonth == 2: | |
currentMonth = "February" | |
elif currentMonth == 3: | |
currentMonth = "March" | |
elif currentMonth == 4: | |
currentMonth = "April" | |
elif currentMonth == 5: | |
currentMonth = "May" | |
elif currentMonth == 6: | |
currentMonth = "June" | |
elif currentMonth == 7: | |
currentMonth = "July" | |
elif currentMonth == 8: | |
currentMonth = "August" | |
elif currentMonth == 9: | |
currentMonth = "September" | |
elif currentMonth == 10: | |
currentMonth = "October" | |
elif currentMonth == 11: | |
currentMonth = "November" | |
elif currentMonth == 12: | |
currentMonth = "December" | |
if currentHour <= 12: | |
hour = " pm " | |
else: | |
currentHour = currentHour - 12 | |
hour = " am " | |
if currentMin == 1: | |
min = " minute " | |
else: | |
min = " minutes " | |
try: | |
if gV.flagTimeQ == 1 and day[0] in text: | |
gV.flagTimeQ = 0 | |
return "Today is " + currentMonth + " " + str(currentDay) + ", " + str(currentYear) + "." | |
elif gV.flagTimeQ == 1 and time[0] in text: | |
gV.flagTimeQ = 0 | |
return "It's " + str(currentHour) + hour + "and " + str(currentMin) + min + "." | |
elif gV.flagTimeQ == 1 and both[0] in text: | |
gV.flagTimeQ = 0 | |
return "Today is " + currentMonth + " " + str(currentDay) + ", " + str(currentYear) + " and it's " + \ | |
str(currentHour) + hour + "and " + str(currentMin) + min + "." | |
elif gV.flagTimeQ == 1 and neither[0] in text: | |
gV.flagTimeQ = 0 | |
return "What do you want to talk about then?" | |
else: | |
return "I can't understand you." | |
except: | |
return "Something went wrong." | |
def weatherForecast(city): | |
try: | |
url = 'https://www.weather-forecast.com/locations/' + city + '/forecasts/latest' | |
headers = {} | |
headers['User-Agent'] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " \ | |
"Chrome/125.190.23.18 Safari/537.36" | |
# the 2 next lines were based on https://docs.python.org/3.4/howto/urllib2.html | |
req = urllib.request.Request(url, headers=headers) | |
resp = urllib.request.urlopen(req) | |
respData = resp.read() | |
temperature = re.findall(r'class="temp">\S+<', str(respData)) | |
weather_text = re.findall(r'"b-metar-table__weather-text">\D+<', str(respData)) | |
n = 0 | |
while n < 4: | |
t = temperature[n] | |
wt = weather_text[n] | |
if "Dry" in wt: | |
weather = "Dry" | |
elif "clowdy" or "Clowdy" in wt: | |
weather = "Clowdy" | |
elif "snow" in wt: | |
weather = "snowy" | |
elif "rain" or "Rain" in wt: | |
weather = "Rainy" | |
elif "Fog" or "fog" in wt: | |
weather = "Foggy" | |
elif "No report." in wt: | |
weather = "Something..." | |
if n == 0: | |
sentence0 = "The temperature and weather in different spots of " + city + " today will be: " | |
sentence1 = t[13:len(t) - 21] + "ºC, and " + weather | |
if n == 1: | |
sentence2 = t[13:len(t) - 21] + "ºC, and " + weather | |
elif n == 2: | |
sentence3 = t[13:len(t) - 21] + "ºC, and " + weather | |
elif n == 3: | |
sentence4 = t[13:len(t) - 21] + "ºC, and " + weather | |
return sentence0 + "\n" + sentence1 + "\n" + sentence2 + "\n" + sentence3 + "\n" + sentence4 | |
n = n + 1 | |
except Exception as error: | |
return "Sorry, but im afraid I don't know that city." | |
def weatherAnswer(text): | |
try: | |
if any(word in text for word in weatherList): | |
gV.flagWeather = 1 | |
return "Do you want to talk about the weather?" | |
elif gV.flagWeather == 1 and gV.flagWeatherA == 1: | |
gV.flagWeather = 0 | |
gV.flagWeatherA = 0 | |
return weatherForecast(text) | |
elif any(word in text for word in yes) and gV.flagWeather == 1: | |
gV.flagWeatherA = 1 | |
return "You want the weather forecast of which city?" | |
elif any(word in text for word in no) and gV.flagWeather == 1: | |
gV.flagWeather = 0 | |
return "Ok then." | |
else: | |
return "I didn't understand." | |
except: | |
return "Something went wrong." | |
@client.event | |
@asyncio.coroutine | |
def on_message(message): | |
message.content = message.content.lower() | |
if message.author == client.user: | |
return | |
else: | |
try: | |
if any(word in message.content for word in weatherList): | |
gV.flagWeather = 1 | |
botmsg = "Do you want to talk about the weather?" | |
elif gV.flagWeather == 1: | |
botmsg = weatherAnswer(message.content) | |
elif any(word in message.content for word in listTime) and gV.flagTimeQ == 0: | |
gV.flagTimeQ = 1 | |
botmsg = "Do you want to know what day it is, what time it is, both or neither?" | |
elif gV.flagTimeQ == 1: | |
botmsg = timeFunction(message.content) | |
else: | |
botmsg = "Something went wrong." | |
except: | |
botmsg = "Sorry, but I didn't understand." | |
yield from client.send_message(message.channel, botmsg) | |
@client.event | |
@asyncio.coroutine | |
def on_ready(): | |
print('Logged in as') | |
print(client.user.name) | |
print(client.user.id) | |
print('------') | |
client.run(TOKEN) |