Skip to Content

(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!

Setup

  • You’ll need a Google account (if you have a Gmail address, you’re good to go). I like using an IDE like Eclipse with PyDev installed, but if you are a complete notepad.exe ninja then go for it.
  • You’ll need to secure a domain name for yourself, or have rights to one. Google, again, has an easy way to do this.
  • You’ll also need to get SSL set up for that domain, which you can do for 90 days free at Comodo.
  • Once you have the cert, you can apply it to your Google Domain like this.

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.

  • We’ll set up ZSLACK_DEMO_SRV in the next post, part 3.
  • To use Basic authentication, you’ll need to take some credentials with access to your SAP Gateway and turn them into base64 encoded characters. One easy way is to bring up the Chrome javascript console (ctrl-shift-j), type “btoa(‘USERNAME:PASSWORD’)”, and take the resulting string. Obviously use a real user and password here.
  • Take the slack_token value from the screen where you set up your slack slash command in part 1.
  • The app configuration at the bottom will make it so that you should configure slack to send its commands to https://<your-domain>/slackdemo. Change that to whatever you like.
  • We treat the ‘shout’ text as a command to send the result of the command to the whole chat window. Otherwise the command will respond only to the person who sends the command and others won’t see it.
  • We look for the word ‘test’ as the key to actually invoke our functionality. If we don’t find that, Commander Data will respond with his polite apology.
  • We look for the name of the RFC by splitting the command up into words and then just taking the last word. Python has this nice little syntax for lists where index [-1] is the last element of the list. text.split()[-1] does this for us.

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!

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply