Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
mqttwarn functions for slack whereis slash-command
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import time
import urllib2
# --- slack_whereis
def _isloc(s):
data = json.loads(s)
if type(data) == dict:
if '_type' in data and data['_type'] == 'location':
if 'lat' in data and 'lon' in data:
return data
return None
def slack_whereis_filter(topic, payload):
''' return True if this message should be suppressed '''
data = _isloc(payload)
if data is None:
return True
return False
def slack_whereis(topic, data, srv=None):
geo = "?"
data = _isloc(data['payload'])
if data is not None:
lat = data['lat']
lon = data['lon']
try:
jsondata = json.load(urllib2.urlopen('http://maps.googleapis.com/maps/api/geocode/json?latlng='+str(lat)+','+str(lon)+'&sensor=true'))
geo = jsondata['results'][0]['formatted_address']
except:
geo = 'reverse lookup error'
return dict(geo=geo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment