Skip to content

Instantly share code, notes, and snippets.

@vicky002
Created July 23, 2017 05:56
Show Gist options
  • Save vicky002/7758de03d6c1f5e1a4f61605a4d61044 to your computer and use it in GitHub Desktop.
Save vicky002/7758de03d6c1f5e1a4f61605a4d61044 to your computer and use it in GitHub Desktop.
Slack bot using Wolframalpha
__author__ = 'vikesh' # this is the author name
import os # This module provides a portable way of using operating system dependent functionality.
import wolframalpha
# I have used Wolframalpha module, written in python to work with Wolframa API.
# Read more about it here : https://pypi.python.org/pypi/wolframalpha
from flask import Flask, request, Response, redirect
# Modules from Flask
try:
import config # Import config.py file
wol_id = config.wolframalpha['app_id'] # assign APP_ID from config.py to wol_id
except:
wol_id = os.environ.get('APP_ID') # load envoironment variable
if not wol_id: # if wol_id is not present display error and exit
import sys
print 'No config.py found exisiting...'
sys.exit(0)
app = Flask(__name__) # initiate flask app
client = wolframalpha.Client(wol_id) # this is from Wolframa moudule. Initiate Client using Wol_id
@app.route('/thel',methods=['post'])
def thel():
'''
:Example:
/thel current weather in mumbai?
'''
'''
this is about routing of the URL in flask.
If you want to change the command from `/thel` to something else. you have to change the app.route('/thel') to
your own command. Also change the function name.
'''
text = request.values.get('text') # Get Query
try:
res = client.query(text) # Get result from Wolframalpha API.
except UnicodeEncodeError: # if coudln't get the response show error.
return Response(('Sorry I did\'t get you. Would you please simplify your query?'
'%s is not valid input.' % text),
content_type='text\plain; charset=utf-8')
resp_qs = ['Hi Top Answer for "%s"\n' % text] # if Query is successful show result.
resp_qs.extend(next(res.results).text) # iterate the result
return Response(''.join(resp_qs),
content_type='text/plain; chatset=utf-8') # return response.
@app.route('/')
def hello(): # if someone tries to open the link directy redirect him somewhere :p
return redirect('https://github.com/vicky002/slack-TheL')
if __name__ == '__main__':
port = int(os.environ.get('PORT',5000)) # run your app on local
app.run(host='0.0.0.0', port=port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment