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?
SmartBot/SmartBOT.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
172 lines (151 sloc)
8.55 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 #This is the discord.py library used to access discord | |
from discord.ext import commands | |
client = commands.Bot(command_prefix = '.') | |
@client.event | |
async def on_ready(): | |
print("Bot Ready.") #check if the bot is ready | |
QuestionCount = 0 | |
PhoneBudget = '' | |
PhoneSize = '' | |
PhoneCamera = '' | |
PhoneBattery = '' | |
PhoneBrand = '' | |
WelcomeMsgResponses = ['yes','ok','sure','yep'] #list of expected responses for the welcome message | |
SizeLargeResponses = ['large','big'] | |
SizeSmallResponses = ['compact','small'] | |
PhoneCameraPositive = ['yes','always','often'] | |
PhoneCameraNegative = ['no', 'not really','sometimes','nope'] | |
PhoneBrandResponse = ['apple','samsung','google','oneplus','any'] | |
#phone name and specification details are stored in seperate list | |
#the index number matches the name of the phones with their specifications from the other lists | |
PhoneNameList = ['pixel 3a','s20','iphone 8','oneplus nord'] | |
PhonePriceList = ['200', '700' , '250' , '380'] | |
PhoneSizeList = ['small', 'large' , 'small', 'large'] | |
PhoneCameraList = ['regular','pro','pro','regular'] #eligibility of professional camera features | |
PhoneBatteryList = ['small','large','small','large'] | |
PhoneBrandList = ['google' , 'samsung','apple','oneplus'] | |
#the index of the phone names corrospond to Amazon links list | |
PhoneAmzonLinkList = ['''https://www.amazon.co.uk/Google-Pixel-Just-Black-64GB/dp/B07RL859ZT/ref=sr_1_3?dchild=1&keywords=pixel+3a&qid=1606492712&sr=8-3''','''https://www.amazon.co.uk/Samsung-Galaxy-Mobile-Phone-Smartphone-Cosmic-Grey/dp/B084GQ3KYM/ref=sr_1_4?dchild=1&keywords=s20&qid=1606492681&sr=8-4''','''https://www.amazon.co.uk/Apple-iPhone-Sim-Free-Smartphone-Refurbished-Gold/dp/B0784GTZJS/ref=sr_1_4?dchild=1&keywords=iphone+8&qid=1606491722&sr=8-4''','''https://www.amazon.co.uk/OnePlus-SIM-Free-Smartphone-Camera-Warranty/dp/B08BPJRK8Z/ref=sr_1_3?dchild=1&keywords=oneplus+nord&qid=1606492267&sr=8-3'''] | |
@client.event | |
async def on_message(message): #trigerred when bot receives an message | |
global QuestionCount #current question number | |
#global variables used to store the answers from the user | |
global PhoneBudget | |
global PhoneSize | |
global PhoneCamera | |
global PhoneBattery | |
global PhoneBrand | |
if message.author == client.user: #make sure that the bot doesn't respond to itself | |
return | |
if QuestionCount==0: | |
await message.channel.send('Welcome! I am Mr SmartBOT here. You have come to the right place to choose your next ideal smartphone! Shall we begin?') | |
QuestionCount = QuestionCount + 1 | |
elif QuestionCount == 1: | |
if message.content.lower() in WelcomeMsgResponses: | |
await message.channel.send('Great! So what is the maximum amount of money you would like to spend on your next smartphone?') | |
QuestionCount = QuestionCount + 1 | |
else: | |
await message.channel.send('''Was that a no? Let's try that again.''') | |
QuestionCount = 0 | |
await on_message(message) | |
elif QuestionCount == 2: | |
try: | |
if int(message.content)>300: | |
await message.channel.send('Do you prefer a large phone or do you prefer something compact?') | |
PhoneBudget = message.content | |
QuestionCount = 3 | |
else: | |
await message.channel.send('''I am sorry that's too low for the phones on my recommondation list. See if you can go a little higher.''') | |
QuestionCount = 1 | |
message.content = 'yes' | |
await on_message(message) | |
except: | |
await message.channel.send('''That wasn't a number you entered. Let's try that again.''') | |
QuestionCount = 1 | |
message.content = 'yes' | |
await on_message(message) | |
elif QuestionCount == 3: | |
if message.content.lower() in SizeLargeResponses or message.content.lower() in SizeSmallResponses: | |
await message.channel.send('Do you use your phone for professional photography?') | |
PhoneSize = message.content | |
QuestionCount = 4 | |
else: | |
await message.channel.send(''' Sorry I didn't understand that. Can you try again please. One word replies will work best with me :) ''') | |
QuestionCount = 2 | |
message.content = PhoneBudget | |
await on_message(message) | |
elif QuestionCount == 4: | |
if message.content.lower() in PhoneCameraPositive or message.content.lower() in PhoneCameraNegative: | |
await message.channel.send('''For approximately how many hours do you daily use your phone for? (4 hours of screen-on time is the average)''') | |
PhoneCamera = message.content | |
QuestionCount = 5 | |
else: | |
await message.channel.send(''' Sorry I didn't understand that. Can you try again please. One word replies will work best with me :) ''') | |
QuestionCount = 3 | |
message.content = PhoneSize | |
await on_message(message) | |
elif QuestionCount == 5: | |
try: | |
if int(message.content)<10: | |
await message.channel.send('Which of the following brand do you prefer Apple,Samsung, Google,OnePlus or do you prefer any brand?') | |
PhoneBattery = message.content | |
QuestionCount = 6 | |
else: | |
await message.channel.send('''That seems like an unrealistic answer. Let's try that again.''') | |
QuestionCount = 4 | |
message.content = PhoneCamera | |
await on_message(message) | |
except: | |
await message.channel.send('''That wasn't a number you entered. Let's try that again''') | |
QuestionCount = 4 | |
message.content = PhoneCamera | |
await on_message(message) | |
elif QuestionCount == 6: | |
if message.content.lower() in PhoneBrandResponse: | |
PhoneBrand = message.content | |
await message.channel.send('''Thanks! That's all the info I need.''') | |
QuestionCount = 7 | |
message.content = 'Done' | |
await on_message(message) | |
else: | |
await message.channel.send(''' Sorry I didn't understand that. Can you try again please? One word replies will work best with me :) ''') | |
QuestionCount = 5 | |
message.content = PhoneBattery | |
await on_message(message) | |
elif QuestionCount == 7: | |
HighestVote = 0 | |
for i in range(0,4): #cycle through the user answer and compare it with phone specification from the lists | |
Phone = PhoneNameList[i] | |
vars()[Phone] = 0 #var() dictionary is used to convert string to a variable name | |
#while cycling through phone lists, whenever the user's aswers is matched with the spec, a point is given to that phone name | |
if int(PhoneBudget) > int(PhonePriceList[i]): | |
vars()[Phone] = vars()[Phone] + 1 | |
if PhoneSize in SizeLargeResponses and PhoneSizeList[i] == 'large': | |
vars()[Phone] = vars()[Phone] + 1 | |
if PhoneSize in SizeLargeResponses and PhoneSizeList[i] == 'small': | |
vars()[Phone] = vars()[Phone] + 1 | |
if int(PhoneBattery) <= 4 : | |
vars()[Phone] = vars()[Phone] + 1 | |
if int(PhoneBattery) >= 4 : | |
if 'large' == PhoneBatteryList[i]: | |
vars()[Phone] = vars()[Phone] + 1 | |
if PhoneCamera in PhoneCameraPositive and PhoneCameraList[i] == 'pro': | |
vars()[Phone] = vars()[Phone] + 1 | |
if PhoneCamera in PhoneCameraNegative: | |
vars()[Phone] = vars()[Phone] + 1 | |
if PhoneBrand in PhoneBrandList: | |
if PhoneBrand == 'any': | |
vars()[Phone] = vars()[Phone] + 1 | |
elif PhoneBrandList[i] == PhoneBrand: | |
vars()[Phone] = vars()[Phone] + 10 | |
for x in range(0,3): | |
FinalPhoneIndex = 0 | |
#compare the scores given and output the phone with the highest score | |
if vars()[PhoneNameList[x]] > vars()[PhoneNameList[x + 1]]: | |
FinalPhoneIndex = x | |
else: | |
FinalPhoneIndex = x + 1 | |
await message.channel.send("Here is the recommended phone for you!") | |
await message.channel.send(PhoneAmzonLinkList[FinalPhoneIndex]) | |
QuestionCount = 0 | |
client.run('NzgwODc2MDg2NzExNTUwMDEz.X71dhw.opqQ5muiGjKydgThgPthqXyq1Ss')#discord bot token - used to access the discrod API |