Created
May 15, 2017 03:00
-
-
Save crschmidt/e52304bb790eb1f5f0f85b11ce1df6f4 to your computer and use it in GitHub Desktop.
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 asyncio | |
import os.path | |
import sys | |
import json | |
try: | |
import apiai | |
except ImportError: | |
sys.path.append( | |
os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir) | |
) | |
import apiai | |
CLIENT_ACCESS_TOKEN = 'APIAITOKEN' | |
def question(message): | |
ai = apiai.ApiAI(CLIENT_ACCESS_TOKEN) | |
request = ai.text_request() | |
request.session_id = "discordbot" | |
request.query = message | |
response = request.getresponse() | |
response = response.read() | |
response = json.loads(response.decode("utf-8")) | |
text = [] | |
if 'result' in response: | |
if 'fulfillment' in response['result']: | |
for i in response['result']['fulfillment']['messages']: | |
if 'platform' in i: continue | |
text.append(i['speech']) | |
return "".join(text) | |
client = discord.Client() | |
@client.event | |
async def on_ready(): | |
print('Logged in as') | |
print(client.user.name) | |
print(client.user.id) | |
print('------') | |
@client.event | |
async def on_message(message): | |
if message.content.startswith("!q"): | |
q = message.content.replace("!q ", "") | |
text = question(q) | |
await client.send_message(message.channel, text) | |
client.run('DISCORDKEY') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment