(This is Part 2 of a 3 part series. See Part 1 and Part 3 for the whole story.)
In Part 1, we got Slack up and running with a Slash Command that will send an HTTP POST to a specified URL endpoint. In this part, I'll show you how to set up a basic Google App Engine web server in Python to respond to this HTTP POST and format a request for SAP Gateway. From Gateway, we'll output the data that the request asks for and send it back to Slack. I will not be exhaustive of all the features of App Engine - this is an SAP blog, after all - but I'll provide sample code, links to how-tos, and some tricks I learned along the way. The amazing thing is that a super basic implementation is only about 40 lines of Python code!
Now you're ready to code! The easiest way to set up a project for App Engine is do the play-at-home 5-minute version. This will get you a project set up, the right deployment tools installed, and a project folder ready to go. Try it out, test it a few times.
Once you're comfortable with how that works, you can simply replace the code files with code I'll provide below. Note that there are several places in the code where I've put some angle brackets with comments - this is where you'll need to fill in your own solution details. My meager programmer salary won't cover a giant hosting bill because everyone copies my domain/settings and sends all their messages through my server.
First, replace the contents of your app.yaml file with this code:
application: <your-google-app-id>
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: main.app
Very straightforward, not much to comment on here. Just remember to replace the app-id section at the top.
Next, create a file called main.py (or replace the contents of the existing one) with this code:
import webapp2
import json
from google.appengine.api import urlfetch
class SlackDemo(webapp2.RequestHandler):
def post(self):
sap_url = '<your-sap-gateway>/ZSLACK_DEMO_SRV/RfcDestinationSet'
json_suffix = '?$format=json'
authorization = 'Basic <your-basic-credentials>'
slack_token = '<your-slack-token>'
request_token = self.request.get('token')
if slack_token != request_token:
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Invalid token.')
return
text = self.request.get('text')
details = {}
if text.find('shout') > -1:
details['response_type'] = 'in_channel'
response_text = ''
if text.find('test') > -1:
rfc_destination = text.split()[-1]
request_url = sap_url + "('" + rfc_destination + "')" + json_suffix
headers = {}
headers['Authorization'] = authorization
response_tmp = urlfetch.fetch(url=request_url,
headers=headers,
method=urlfetch.GET)
response_info = json.loads(response_tmp.content)
response_text += 'Sensor sweep indicates the following:\n'
response_text += response_info['d']['Destination'] + ' - '
response_text += response_info['d']['ConnectionStatus'] + ' - '
response_text += str(response_info['d']['ConnectionTime']) + ' ms response'
else:
response_text += "I'm sorry, Captain, but my neural nets can't process your command."
details['text'] = response_text
json_response = json.dumps(details)
self.response.headers['Content-Type'] = 'application/json'
self.response.write(json_response)
app = webapp2.WSGIApplication([
('/slackdemo', SlackDemo),
], debug=True)
I'll do a little explaining here.
Build the project and deploy it to the web site you're using. Now we're ready to create the Gateway service that will do the simple RFC test that Commander Data did in part 1.
Off to part 3!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
10 | |
9 | |
7 | |
6 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 |