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/main.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
188 lines (160 sloc)
7.77 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
#------------imports--------------------- | |
import discord | |
from discord.ext import commands | |
import youtube_dl | |
from wit import Wit | |
from weather import get_weather | |
import requests | |
from teamInfo import teamInformation | |
from youtubeSearch import get_vid_link | |
from youtubeSearch import lenght_song | |
import time | |
#---------------------------------------- | |
city='coventry' | |
discord_token ='NTA2OTAwODQxMzAzNjM4MDE3.DrpAQw.9LkZsCRlA9DZXll0Us65hdgxLN0' | |
players_instances={} | |
def format_message(message): | |
'''extract any unwanted characters the user may have entered''' | |
for word in message: | |
word.replace('?','').replace('/',''),replace(';','').replace(':',''),replace('*','') | |
return message | |
def get_wit_intend(user_input): | |
'''This function takes a string as input and returns two values: intend and value on which it applies''' | |
#--- This code was inspired from | |
wit_url = 'https://api.wit.ai/message' | |
wit_access_tokken = 'SLMTP2DRRNT242SZJY4MNFMUV3CDAPAB' | |
header={'Authorization':'Bearer {}'.format(wit_access_tokken)} | |
query={'q':str(user_input)} | |
wit_resp=requests.get(wit_url,headers=header,params=query) | |
wit_resp=wit_resp.json() | |
user_intent=wit_resp['entities']['intent'][0]['value'] | |
return user_intent | |
#----The following code (news.py) is a modified version ofthe code mohamed adnan(teammate) wrote --------------------------- | |
def display_news(source): | |
sources = ['bbc-news', 'bbc-sport', 'fox-news', 'ign', 'rbc'] | |
summary=[] | |
if source in sources: | |
url_link = 'https://newsapi.org/v2/top-headlines?sources={}&apiKey=b2a284c2f1cc47a0b0abd899591feb35'.format(source) | |
response = requests.get(url_link) | |
s=0 | |
info = response.json() | |
while s <10: | |
title = info['articles'][s]['title'] | |
description = info['articles'][s]['description'] | |
article_url = info['articles'][s]['url'] | |
summary.append( [title,description,article_url] ) | |
s=s+1 | |
return summary | |
else: | |
return False | |
#------------------------------END OF MODIFIED VERSION OF news.py------------------------------------------------ | |
TESS=discord.Client() | |
@TESS.event | |
async def on_ready(): | |
print('TESS online ') | |
@TESS.event | |
async def on_message(message): | |
if message.author ==TESS.user: | |
return | |
if ((message.content[0])=='!' and ('play' in message.content)): #--checking if an input is a command | |
channel= message.author.voice.voice_channel #--- establishing an instance of TESS in the voice channel the user is in | |
server=message.server | |
if TESS.is_voice_connected(server) ==False: #---checking if we already have a voice connection to the voice client | |
await TESS.join_voice_channel(channel) | |
TESS_voice=TESS.voice_client_in(server) | |
print('joined voice channel') | |
message.content=message.content.strip('!') | |
message.content=message.content.strip('play') | |
first_link=get_vid_link(message.content) | |
song_lenght=int(lenght_song(first_link)) | |
vid='https://youtube.com{}'.format(first_link) | |
song_player=await TESS_voice.create_ytdl_player(vid) | |
players_instances[server.id]=song_player | |
song_player.start() | |
print(players_instances) | |
else: | |
TESS_voice=TESS.voice_client_in(server) | |
message.content=message.content.strip('!') | |
message.content=message.content.strip('play') | |
first_link=get_vid_link(message.content) | |
song_lenght=int(lenght_song(first_link)) | |
vid='https://youtube.com{}'.format(first_link) | |
song_player=await TESS_voice.create_ytdl_player(vid) | |
players_instances[server.id]=song_player | |
song_player.start() | |
elif (message.content[0]=='!') and ('pause' in message.content): | |
if len(players_instances)>=0: | |
id=message.server.id | |
TESS_voice=TESS.voice_client_in(server) | |
players_instances[id].pause() | |
elif (message.content[0]=='!') and ('resume' in message.content): | |
if len(players_instances)>=0: | |
id=message.server.id | |
TESS_voice=TESS.voice_client_in(server) | |
players_instances[id].resume() | |
elif (message.content[0]=='!') and ('stop' in message.content): | |
if len(players_instances)>=0: | |
id=message.server.id | |
TESS_voice=TESS.voice_client_in(server) | |
players_instances[id].stop() | |
TESS_voice.disconnect() | |
print('TESS left the Voice client') | |
else: | |
intent=get_wit_intend(message.content) | |
if intent=='find_weather': | |
weather_info = get_weather(city) | |
temp = weather_info[1] | |
overview = weather_info[0] | |
description = weather_info[2] | |
wind_speed = weather_info[3] | |
location = weather_info[4] | |
clouds=':cloud: ' | |
temperatur_emoji=':thermometer: ' | |
await TESS.send_message(message.channel,'It is currently {0} celsius {5} with {1} {4} and wind speed of {2} meters per seconds in {3} '.format(temp,description,wind_speed, location,clouds,temperatur_emoji)) | |
elif intent== 'find_team': | |
await TESS.send_typing(message.channel) | |
msg=format(message.content).split() | |
if 'bayern' in msg: | |
teamInfo=teamInformation(5) | |
name=teamInfo[0] | |
date_created=teamInfo[1] | |
colors=teamInfo[2] | |
color_emojis=':large_blue_circle: ' + ':red_circle: ' +':white_circle: ' | |
reply='{0} is a football club that was created in {1} in Germany. Their kit colors are\n {2} {3}'.format(name,date_created,colors,color_emojis) | |
await TESS.send_message(message.channel,reply,tts=True) | |
else: | |
await TESS.send_typing(message.channel) | |
time.sleep(2) | |
await TESS.send_message(message.channel,'i only know about Bayern so far. i will be able to find data on your team in later versions of me :confused: ',tts=True) | |
elif intent=='greetings': | |
await TESS.send_typing(message.channel) | |
time.sleep(2) | |
await TESS.send_message(message.channel,'Hi :wave: ',tts=True) | |
elif intent=='play_song': | |
channel=message.author.voice.voice_channel | |
server=message.server | |
await TESS.join_voice_channel(channel) | |
TESS_voice=TESS.voice_client_in(server) | |
print('joined voice channel') | |
message.content=message.content.strip('!') | |
message.content=message.content.strip('play') | |
first_link=get_vid_link(message.content) | |
#song_lenght=int(lenght_song(first_link)) | |
vid='https://youtube.com{}'.format(first_link) | |
song_player=await TESS_voice.create_ytdl_player(vid) | |
song_player.start() | |
elif intent =='news': | |
await TESS.send_typing(message.channel) | |
news=display_news(message.content) | |
if news==False: | |
await TESS.send_message(message.channel,'sorry enter one of the following sources :\n bbc-news, bbc-sport, fox-news, ign, rbc',tts=True) | |
else: | |
for item in news: | |
for in_item in item: | |
await TESS.send_message(message.channel,in_item,tts=True) | |
await TESS.send_message(message.channel,':newspaper:',tts=True) | |
else: | |
await TESS.send_typing(message.channel) | |
time.sleep(2) | |
await TESS.send_message(message.channel, 'sorry I do not understand what you mean. ',tts=True) | |
TESS.run(discord_token) | |