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?
ChatBot/mainBot.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
387 lines (367 sloc)
21.4 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 | |
from discord.ext import commands | |
import csv | |
import requests | |
from selenium import webdriver | |
from dictionary import definition, nat | |
from azure.cognitiveservices.search.imagesearch import ImageSearchAPI | |
from msrest.authentication import CognitiveServicesCredentials | |
from selenium.webdriver.support.ui import Select | |
import time | |
browser = webdriver.Chrome('C:/Users/chadw/OneDrive/Desktop/chromedriver') #Launches an instance of chrome the bot can use | |
bot = commands.Bot(command_prefix='!', description='A bot to help the user choose a car.') #Defines bot for future reference | |
@bot.event | |
async def on_ready(): | |
print('Logged in as') | |
print(bot.user.name) | |
print(bot.user.id) | |
activity = discord.Activity(name='Robot TV', type=discord.ActivityType.watching) | |
await bot.change_presence(activity=activity) | |
await main() | |
### Initial mechanicCall function was wrote by Roshan but I intergrated it to the main discord bot. | |
@bot.event | |
async def mechanicCall(message): | |
browser.get('https://docs.google.com/forms/d/e/1FAIpQLSfwm-zzPi8Cv8jwsQ9G8whWVL6uEPH6UPBdfv5FsuXLOpdTgw/viewform') | |
name = browser.find_element_by_name('entry.903722920') | |
await message.channel.send("What is your name?") | |
@bot.event | |
async def on_message(message): | |
if message.author == bot.user: | |
return | |
else: | |
userName = message.content.lower() | |
name.send_keys(userName) | |
email = browser.find_element_by_name('entry.181354507') | |
await message.channel.send("What is your email?") | |
@bot.event | |
async def on_message(message): | |
if message.author == bot.user: | |
return | |
else: | |
userEmail = message.content.lower() | |
email.send_keys(userEmail) | |
number = browser.find_element_by_name('entry.1384446626') | |
await message.channel.send("What is your phone number?") | |
@bot.event | |
async def on_message(message): | |
if message.author == bot.user: | |
return | |
else: | |
userPhonenumber = message.content.lower() | |
number.send_keys(userPhonenumber) | |
postcode = browser.find_element_by_name('entry.59934193') | |
await message.channel.send("What is your postcode?") | |
@bot.event | |
async def on_message(message): | |
if message.author == bot.user: | |
return | |
else: | |
userPostcode = message.content.lower() | |
postcode.send_keys(userPostcode) | |
reg = browser.find_element_by_name('entry.1243460365') | |
await message.channel.send("What is your numberplate?") | |
@bot.event | |
async def on_message(message): | |
if message.author == bot.user: | |
return | |
else: | |
userNumberplate = message.content.lower() | |
reg.send_keys(userNumberplate) | |
Problem = browser.find_element_by_name("entry.1747051028") | |
await message.channel.send("What is your problem?") | |
@bot.event | |
async def on_message(message): | |
if message.author == bot.user: | |
return | |
else: | |
userProblem = message.content.lower() | |
Problem.send_keys(userProblem) | |
submit = browser.find_element_by_xpath( | |
'/html/body/div/div[2]/form/div/div/div[3]/div[1]/div[1]/div/span/span') | |
submit.click() | |
await message.channel.send( | |
"One of the mechanics will call or email you shortly regarding your problem.") | |
await main() | |
###End of code by Roshan that I integrated to the main discord bot | |
###Code that I wrote that utilizes selenium to enter data into a website to get a quote for selling your car | |
@bot.event | |
async def carQuote(message): | |
browser.get('https://www.webuyanycar.com/') | |
vehicleref = browser.find_element_by_id('vehicleReg') | |
await message.channel.send("What is your registration?") | |
@bot.event | |
async def on_message(message): | |
if message.author == bot.user: | |
return | |
else: | |
reg = message.content.lower() | |
vehicleref.send_keys(reg) | |
vehicleref.submit() | |
time.sleep(3) | |
mileage = browser.find_element_by_id('Mileage') | |
await message.channel.send("What is the mileage") | |
@bot.event | |
async def on_message(message): | |
if message.author == bot.user: | |
return | |
else: | |
miles = message.content.lower() | |
mileage.send_keys(miles) | |
await message.channel.send("How many owners has the car had?") | |
@bot.event | |
async def on_message(message): | |
if message.author == bot.user: | |
return | |
else: | |
owners = message.content.lower() | |
select = Select(browser.find_element_by_id('PreviousOwners')) | |
owners = str(owners) | |
select.select_by_visible_text(owners) | |
select = Select(browser.find_element_by_id('ServiceHistoryId')) | |
await message.channel.send("Please enter what service history your car has. Options are either; Full, Some, None or First not due.") | |
@bot.event | |
async def on_message(message): | |
if message.author == bot.user: | |
return | |
else: | |
serviceHistory = message.content | |
select.select_by_visible_text(serviceHistory) | |
browser.find_element_by_xpath('//button[normalize-space()="Continue"]').click() | |
time.sleep(3) | |
customername = browser.find_element_by_id('CustomerName') | |
await message.channel.send("What is your name?") | |
@bot.event | |
async def on_message(message): | |
if message.author == bot.user: | |
return | |
else: | |
name = message.content.lower() | |
customername.send_keys(name) | |
customeremail = browser.find_element_by_id('EmailAddress') | |
await message.channel.send("What is your email?") | |
@bot.event | |
async def on_message(message): | |
if message.author == bot.user: | |
return | |
else: | |
email = message.content.lower() | |
customeremail.send_keys(email) | |
customerpostcode = browser.find_element_by_id('Postcode') | |
await message.channel.send("What is your postcode?") | |
@bot.event | |
async def on_message(message): | |
if message.author == bot.user: | |
return | |
else: | |
postcode = message.content.lower() | |
customerpostcode.send_keys(postcode) | |
browser.find_element_by_xpath('//button[normalize-space()="Get my valuation"]').click() | |
time.sleep(3) | |
await message.channel.send("Our valuation from webuyanycar.com has been emailed to you") | |
await main() | |
###Code that I edited that uses the bing image search api example to search the internet for an image and returns it through the discord bot | |
def imageSearch(): | |
@bot.event | |
async def on_message(message): | |
if message.author == bot.user: | |
return | |
else: | |
subscription_key = '' | |
search_term = message.content | |
client = ImageSearchAPI(CognitiveServicesCredentials(subscription_key)) | |
image_results = client.images.search(query=search_term) | |
if image_results.value: | |
first_image_result = image_results.value[0] | |
await message.channel.send(format(first_image_result.content_url)) | |
await main() | |
else: | |
await message.channel.send("Couldn't find image results!") | |
### Code that Roshan wrote that communicates with the user and chooses them a car through searching a csv file | |
### I helped with a couple errors and helped figure out searching the database with Roshan | |
### Roshan wrote the code initially in python to run in terminal and I then integrated it within the discord bot. | |
@bot.event | |
async def deduce2(c, message): | |
n=definition(c) | |
with open('C:/Users/chadw/PycharmProjects/Carbot/2019.csv', 'r') as csv_file: | |
csv_reader = csv.reader(csv_file) | |
x=" " | |
await message.channel.send("Ive had a look for you and I think " + n + " is best for you") | |
await message.channel.send("These are some companies with " +n) | |
for line in csv_reader: | |
if line[1]==x: | |
pass | |
elif n in line[3] and int(line[4])>35000: | |
await message.channel.send(line[1]) | |
x=(line[1]) | |
await message.channel.send("Which company shall I select?") | |
@bot.event | |
async def on_message(message): | |
if message.author == bot.user: | |
return | |
else: | |
o = message.content.lower() | |
await message.channel.send("Alright " + o + " it is.") | |
await message.channel.send("Reselect Company?(Y/N)") | |
@bot.event | |
async def on_message(message): | |
if message.author == bot.user: | |
return | |
else: | |
b = message.content.lower() | |
if "reselect" in b: | |
await deduce(f) | |
else: | |
await message.channel.send("Here are some models in your category") | |
with open('C:/Users/chadw/PycharmProjects/Carbot/2019.csv', 'r') as csv_file: | |
csv_reader = csv.reader(csv_file) | |
for line in csv_reader: | |
if line[1] == o and n in line[3]: | |
await message.channel.send(line[2]) | |
await message.channel.send("What model do you want?") | |
@bot.event | |
async def on_message(message): | |
if message.author == bot.user: | |
return | |
else: | |
P = message.content.lower() | |
await message.channel.send(P + "Noted") | |
await message.channel.send("-------------") | |
await message.channel.send("Car Finalized") | |
await message.channel.send("Company : " + o) | |
await message.channel.send("Model : " + P) | |
search_term = o + " " + P | |
subscription_key = '10b1d84d5a364c97ab7deb697231de4c' | |
search_term = message.content | |
client = ImageSearchAPI(CognitiveServicesCredentials(subscription_key)) | |
image_results = client.images.search(query=search_term) | |
if image_results.value: | |
first_image_result = image_results.value[0] | |
await message.channel.send(format(first_image_result.content_url)) | |
await main() | |
@bot.event | |
async def deduce(c, message): | |
n=definition(c) | |
with open('C:/Users/chadw/PycharmProjects/Carbot/2019.csv', 'r') as csv_file: | |
csv_reader = csv.reader(csv_file) | |
x=" " | |
await message.channel.send("According to my bot calculations " + n + " is best for you") | |
await message.channel.send("These are some companies with " + n +" along with affordable prices. Additionally, I only searched diesel vehicle since you are low on budget") | |
for line in csv_reader: | |
if line[1]==x: | |
pass | |
elif n in line[3] and int(line[4])<28000: | |
if "diesel" in line[5]: | |
await message.channel.send(line[1]) | |
x=(line[1]) | |
else: | |
pass | |
await message.channel.send("Do you want me to look for petrol instead?") | |
@bot.event | |
async def on_message(message): | |
if message.author == bot.user: | |
return | |
else: | |
j = message.content.lower() | |
if "yes" in j: | |
n = definition(c) | |
with open('C:/Users/chadw/PycharmProjects/Carbot/2019.csv', 'r') as csv_file: | |
csv_reader = csv.reader(csv_file) | |
x = " " | |
for line in csv_reader: | |
if line[1] == x: | |
pass | |
elif n in line[3] and int(line[4]) < 28000: | |
if "petrol" in line[5]: | |
await message.channel.send(line[1]) | |
x = (line[1]) | |
else: | |
pass | |
await message.channel.send("Which company shall I select?") | |
@bot.event | |
async def on_message(message, n): | |
if message.author == bot.user: | |
return | |
else: | |
o = message.content.lower() | |
await message.channel.send("Alright " + o + " it is.") | |
with open('C:/Users/chadw/PycharmProjects/Carbot/2019.csv', 'r') as csv_file: | |
csv_reader = csv.reader(csv_file) | |
for line in csv_reader: | |
if line[1] == o and int(line[4]) < 28000 and n in line[3]: | |
await message.channel.send(line[2]) | |
await message.channel.send("Reselect Company?(Y/N)") | |
@bot.event | |
async def on_message(message): | |
if message.author == bot.user: | |
return | |
else: | |
b = message.content.lower() | |
if "reselect" in b: | |
await deduce(f) | |
else: | |
await message.channel.send("What model do you want") | |
@bot.event | |
async def on_message(message): | |
if message.author == bot.user: | |
return | |
else: | |
P = message.content.lower() | |
await message.channel.send(P + (' Noted')) | |
await message.channel.send("-------------") | |
await message.channel.send("Car Finalized") | |
await message.channel.send("Company : " + o) | |
await message.channel.send("Model : " + P) | |
search_term = o + " " + P | |
subscription_key = '10b1d84d5a364c97ab7deb697231de4c' | |
search_term = message.content | |
client = ImageSearchAPI(CognitiveServicesCredentials(subscription_key)) | |
image_results = client.images.search(query=search_term) | |
if image_results.value: | |
first_image_result = image_results.value[0] | |
await message.channel.send(format(first_image_result.content_url)) | |
await main() | |
### End of code wrote by Roshan | |
### Code that I wrote that handles the conversation between the bot and user, Roshan found and initially coded the ai bot in pyhton terminal and I just brought it into the discord bot. | |
@bot.event | |
async def main(): | |
@bot.event | |
async def on_message(message): | |
if message.author == bot.user: | |
return | |
else: | |
if message.content.lower() == "hi" or message.content.lower() == "hey" or message.content.lower() == "hello": | |
await message.channel.send("Hi! There are a few things i can do, have a general chat with me and if you decide to either; search for a car, search the internet for images, get a quote from a WeBuyAnyCar or organise a call form the mechanic. Just type Image search, Car Quote, Mechanic Call or Car Search at any time.") | |
elif message.content.lower() == "image search": | |
await message.channel.send("Okay so you want to search the internet for an image.") | |
await message.channel.send("What are you searching for?") | |
imageSearch() | |
elif message.content.lower() == "car quote": | |
await carQuote(message) | |
elif message.content.lower() == "car search": | |
await message.channel.send("Hi so you are looking for a car? Tell me more about your budget and preference") | |
@bot.event | |
async def on_message(message): | |
if message.author == bot.user: | |
return | |
else: | |
e = message.content.lower() | |
f = nat(e) | |
if "low budget" in e or 'affordable' in e or 'low range' in e or 'cheap' in e or 'below' in e: | |
await deduce(f, message) | |
else: | |
await deduce2(f, message) | |
else: | |
### Wrote by Roshan, I changed x from an input to the message content from discord and instead of printing the response I sent it through the discord channel. | |
x = message.content | |
url = "https://acobot-brainshop-ai-v1.p.rapidapi.com/get" | |
querystring = {"bid": "178", "key": "", "uid": "mashape", "msg": x} | |
headers = { | |
'x-rapidapi-host': "acobot-brainshop-ai-v1.p.rapidapi.com", | |
'x-rapidapi-key': "" | |
} | |
response = requests.request("GET", url, headers=headers, params=querystring) | |
z = len(response.text) | |
y = response.text[8:z - 2] | |
await message.channel.send(y) | |
### End of code wrote by Roshan | |
bot.run('') |