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?
totumchatbot/TOTUM_Chatbot.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
364 lines (251 sloc)
11.5 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 nltk | |
from nltk.stem import PorterStemmer | |
from dotenv import load_dotenv | |
import os | |
import random | |
from nltk.stem.wordnet import WordNetLemmatizer | |
import requests | |
import json | |
from googlemaps import convert | |
lem = WordNetLemmatizer() | |
ps = PorterStemmer() | |
message321 =[] | |
message1 = [] | |
load_dotenv() | |
token = 'NjMxNDA0MzQ1NDEzMzM3MDk0.Xa7ZiQ.YR926EsZ1n3idzCvutA13MRKRz4' | |
client = discord.Client() | |
async def getWeather(city, message): | |
getInfo = requests.get('https://api.openweathermap.org/data/2.5/weather?q=' + city + ',uk' + '&appid=bd9b2518321605c17d1b7fc13d1a4034') | |
if getInfo.status_code != 200: | |
await message.channel.send("I don't think that's a city in the U.K.") | |
return | |
weather = getInfo.json()["weather"] | |
temp = getInfo.json()["main"] | |
tempKelvin = temp["temp"] | |
tempCelsius = round(tempKelvin - 273.15, 2) | |
for i in weather: | |
weatherMain = i["main"] | |
await message.channel.send("The weather is " + weatherMain + "y" + " with a temperature of " + str(tempCelsius)) | |
if random.choice(range(9)) == 0: | |
await message.channel.send("Good merning, nice day fer fishing, ain't it? huh huh") | |
async def getMarvel(characterName, message): | |
response = requests.get("http://gateway.marvel.com/v1/public/characters?name="+ characterName + "&ts=1&apikey=7c60178bd6e1196ab697f86dc654aa96&hash=37c7befca5d244893ffe8bcd8890cb5d") | |
if response.json()["etag"] == "79ef3436d0dc139b17693635b99776556e29f495": | |
await message.channel.send("Don't think that's a real character...") | |
return | |
response = response.json()["data"]["results"] | |
for i in response: | |
description = i["description"] | |
if description != "": | |
await message.channel.send("A brief description of " + characterName + " is: " + description) | |
else: | |
await message.channel.send("I'm sorry but I don't know enough about this character. Could you please inform me of them?") | |
@client.event | |
async def on_ready(): | |
print(f'{client.user} has connected to Discord!') | |
@client.event | |
async def on_member_join(member): | |
await member.create_dm() | |
await member.dm_channel.send( | |
f'Hi {member.name}, welcome to my Discord server!' | |
) | |
@client.event | |
async def on_message(message): | |
if message.author == client.user: | |
return | |
greetingsIN = [ | |
'hello', | |
'hey', | |
'yo', | |
'greet', | |
'hi', | |
'sup', | |
] | |
greetingsOUT = [ | |
'Hello!', | |
'Hey!', | |
'Yo!', | |
'Greetings!', | |
'Hi!', | |
'Hiya!', | |
"'Sup!", | |
] | |
message123 = nltk.word_tokenize(message.content) | |
for w in message123: | |
message1.append(w.lower()) | |
print(message1) | |
for w in message1: | |
message321.append(ps.stem(w)) | |
print(message321) | |
if 'bitch' in message321: | |
await message.channel.send("Hey, let's not use foul language on the server. Okay?") | |
message321.clear() | |
message1.clear() | |
return | |
if 'shit' in message321: | |
await message.channel.send("Language.") | |
message321.clear() | |
message1.clear() | |
return | |
if 'fuck' in message321: | |
await message.channel.send("Come on, watch your langauge.") | |
message321.clear() | |
message1.clear() | |
return | |
for i in greetingsIN: | |
if i in message321: | |
response = random.choice(greetingsOUT) | |
await message.channel.send(response) | |
if 'how' in message321: | |
if 'you' in message321: | |
await message.channel.send("I'm good and you?") | |
if 'what' in message321: | |
if 'name' in message321: | |
await message.channel.send("My name's Totum. What's yours?") | |
if 'weather' in message321: | |
if 'is' in message321: | |
if 'in' in message321: | |
for i in range(len(message1)): | |
if message1[i] == "in": | |
message1.append("") | |
if message1[i + 1] == "": | |
await message.channel.send("Enter a city name, please.") | |
message321.clear() | |
message1.clear() | |
return | |
else: | |
city = message1[i + 1] | |
print(city) | |
await getWeather(city, message) | |
if "who" in message321: | |
if "is" in message321: | |
for i in range(len(message1)): | |
if message1[i] == "is": | |
message1.append("") | |
if message1[i + 1] == "": | |
await message.channel.send("I only know fictional characters, most of which are from the Marvel universe. Ask about one of those charcters and I'll see what I can tell you about them.") | |
message321.clear() | |
message1.clear() | |
return | |
else: | |
characterName = message1[i + 1] | |
print(characterName) | |
await getMarvel(characterName, message) | |
if 'comic' in message321: | |
if 'store' in message321: | |
await message.channel.send("What's your postcode?") | |
message1.clear() | |
message321.clear() | |
await postcode(message) | |
message1.clear() | |
message321.clear() | |
async def postcode(message): | |
def check(m): | |
return "cv" in m.content.lower() and m.channel == message.channel and m.author == message.author | |
newMessage = await client.wait_for("message", check=check) | |
newMessage = newMessage.content | |
postcode1 = newMessage | |
await google(message, postcode1) | |
def longitude(message, postcode1): | |
postcodem = postcode1 | |
getInfo = requests.get('http://api.postcodes.io/postcodes/' + postcodem) | |
longitude = getInfo.json()["result"]["longitude"] | |
print(longitude) | |
return longitude | |
def latitude(message, postcode1): | |
postcodem = postcode1 | |
getInfo = requests.get('http://api.postcodes.io/postcodes/' + postcodem) | |
latitude = getInfo.json()["result"]["latitude"] | |
print(latitude) | |
return latitude | |
async def google(message, postcode1): | |
long = str(longitude(message, postcode1)) | |
lat = str(latitude(message, postcode1)) | |
getInfo = requests.get('https://maps.googleapis.com/maps/api/place/textsearch/json?query=comic+book+stores&location=' + long + ',' + lat + '&radius=10000&key=AIzaSyB6frkOwZL20nQAzj0nCRHFUkEFVJ0XzC8') | |
find = getInfo.json()["results"] | |
name = [] | |
openHour = [] | |
for i in find: | |
name.append(i["name"]) | |
for i in find: | |
openHour.append(i["opening_hours"]) | |
if len(openHour) >= 9: | |
break | |
if openHour[0]["open_now"] == True: | |
await message.channel.send("A) " + name[0] + " is nearby and it's open, right now.") | |
else: | |
await message.channel.send("A) " + name[0] + " is closed, at the moment.") | |
if openHour[1]["open_now"] == True: | |
await message.channel.send("B) " + name[1] + " is close too. And it's open.") | |
else: | |
await message.channel.send("B) " + name[1] + " is closed.") | |
if openHour[1]["open_now"] == True: | |
await message.channel.send("C) " + name[2] + " is open and it's not too far away.") | |
else: | |
await message.channel.send("C) " + name[2] + " is fairly close by but it's closed, right now.") | |
### everything from here is from daniel daria gustavo and swas | |
def check(m): | |
return "" in m.content.lower() and m.channel == message.channel and m.author == message.author | |
await message.channel.send("Would you like the directions to one of the stores?") | |
newMessage = await client.wait_for("message", check=check) | |
newMessage = newMessage.content | |
newMessage = newMessage.lower() | |
if newMessage == "yes": | |
def check(m): | |
return "" in m.content.lower() and m.channel == message.channel and m.author == message.author | |
await message.channel.send("Which one?") | |
await message.channel.send("Option A, B or C?") | |
newMessage = await client.wait_for("message", check=check) | |
newMessage = newMessage.content | |
newMessage = newMessage.lower() | |
if newMessage == "a": | |
def check(m): | |
return "" in m.content.lower() and m.channel == message.channel and m.author == message.author | |
await message.channel.send("How would you like to get there?") | |
await message.channel.send("Driving, cycling, walking or transit?") | |
newMessage = await client.wait_for("message", check=check) | |
newMessage = newMessage.content | |
newMessage = newMessage.lower() | |
await comicStores(postcode1,name[0], newMessage, message) | |
elif newMessage == "b": | |
def check(m): | |
return "" in m.content.lower() and m.channel == message.channel and m.author == message.author | |
await message.channel.send("How would you like to get there?") | |
await message.channel.send("Driving, cycling, walking or transit?") | |
newMessage = await client.wait_for("message", check=check) | |
newMessage = newMessage.content | |
newMessage = newMessage.lower() | |
await comicStores(postcode1, name[1], newMessage, message) | |
elif newMessage == "c": | |
def check(m): | |
return "" in m.content.lower() and m.channel == message.channel and m.author == message.author | |
await message.channel.send("How would you like to get there?") | |
await message.channel.send("Driving, cycling, walking or transit?") | |
newMessage = await client.wait_for("message", check=check) | |
newMessage = newMessage.content | |
newMessage = newMessage.lower() | |
await comicStores(postcode1, name[2], newMessage, message) | |
else: | |
await message.channel.send("Not an option.") | |
elif newMessage == "no": | |
await message.channel.send("Okay, have a good day.") | |
else: | |
await message.channel.send("Not an option.") | |
async def comicStores(postcode1, destination, mode, message): | |
directionsMain = requests.get("https://maps.googleapis.com/maps/api/directions/json?origin=" + postcode1 + "&destination=" + destination + "&mode=" + mode + "&key=AIzaSyB6frkOwZL20nQAzj0nCRHFUkEFVJ0XzC8") | |
print(json.dumps(directionsMain.json(), sort_keys=True, indent=4)) | |
directionsRoutes = directionsMain.json()["routes"] | |
for i in directionsRoutes: | |
directionsLegs = i["legs"] | |
for i in directionsLegs: | |
directionSteps = i["steps"] | |
direc = [] | |
for i in directionSteps: | |
direc.append(i["html_instructions"]) | |
fullDirection = "" | |
for i in direc: | |
fullDirection = fullDirection + "\n" + i | |
await message.channel.send(fullDirection) | |
await message.channel.send("You have arrived at your destination.") | |
client.run(token) | |