#!/usr/bin/python #-------------------------------------------------------------------------------------------------- #-- NAME #-------------------------------------------------------------------------------------------------- # Program : NAME # To Complie : n/a # # Purpose : # # Called By : # Calls : # # Author : Rusty Myers # Based Upon : # # Note : Point Slack to URL where this is hosted. Update token in script. Returns json of data. # : test with - curl -X POST -F 'text=man' -F 'token=dtbzweAwGhicKXGQeARRol8u' http://127.0.0.1:5000/manbot # # Revisions : # %Y-%m-%d Initial Version # # Version : 1.0 #-------------------------------------------------------------------------------------------------- import urllib2 import re import html2text from BeautifulSoup import BeautifulSoup import json import calendar import time from flask import Flask, request, jsonify app = Flask(__name__) app.debug = True def check_token(service, token): if "man" in service: if "dtbzweAwGhicKXGQeARRol8u" in token: return True else: return False def manbot(command): """docstring for manbot""" # print manpage[:1] # print command response = urllib2.urlopen('http://www.manpagez.com/man/alpha.php?' + command[:1]) html = response.read() # print html manpage = re.search('[0-9]{1}\/' + command +'\/', html) # print manpage.group(0) manpageresponse = urllib2.urlopen('http://www.manpagez.com/man/' +manpage.group(0)) manhtml = manpageresponse.read() soup = BeautifulSoup(manhtml) souptext = soup.find('div', {'id' : 'content'}).getText() #.replace('\n','\n\n') # souptext = "test" epochnow = calendar.timegm(time.gmtime()) message = jsonify({"text": "Man Page:", "response_type": "in_channel", "attachments":[ {"text": souptext, "footer": "spiderweb API", "ts": epochnow, "color": "#36a64f" }] }) #print message return message @app.route('/') def hello_world(): return '

Welcome to the ManBot service.

' @app.route('/manbot', methods=['GET','POST']) def slackmanbot(): token = request.form.get('token', '') #print token command = request.form.get('text', '') #print command if check_token("man", token): return manbot(command) else: return "Wrong Token" #return manbot("ls") if __name__ == '__main__': app.run(host='0.0.0.0', debug=True)