2

Im having some issue trying to get ackknowalgemtns working with python.

thus far my code posts the message and gets the JSON sting in return but now i need a way to get the receipt out of the string

import requests
app_token = 'app token'
user_token = 'user token'
title = 'TEST MESSGAE'
text = 'text for polling'
params = {
            'token': app_token,
            'user': user_token,
            'title': title,
            'message': text,
            'retry': 30, 
            'expire': 180,
            'priority': 2,
            'sound': 'siren',
        }
msg = requests.post('https://api.pushover.net/1/messages.json', data=params)
print "POSTed message"
print msg.json()

output is

POSTed message
{u'status': 1, u'receipt': u'riD7NEukZsiDQpjhiNppphYnpue8uK', u'request': u'ef5f42d568e966cbf3d2b28c6579397c'}

The parameter is a case-sensitive, 30 character string containing the character set [A-Za-z0-9]. would this be a REGEX job ?

shaggs
  • 600
  • 7
  • 27
  • Why don't use `json` module to load the json data as a `dict`? Then you can do something like `d['request']` or `d['receipt']`. And you'll get `'ef5f42d568e966cbf3d2b28c6579397c'` or `'riD7NEukZsiDQpjhiNppphYnpue8uK'`. – Remi Guan Oct 17 '15 at 23:48
  • Ive never used json like that before so example would be great – shaggs Oct 17 '15 at 23:50
  • Okay, please wait :) – Remi Guan Oct 17 '15 at 23:51
  • Have you considered using a python library that already implements this? See [pushnotify](https://pypi.python.org/pypi/pushnotify), [pushover](https://github.com/Wyattjoh/pushover), or [py_pushover](https://github.com/KronosKoderS/py_pushover). *Note: I'm the creator of `py_pushover`.* – James Mertz Nov 09 '15 at 22:38
  • I had but if you ook at the amount of code and functions i would be the calling to do one task seems very over kill. I now have it up and running using the json below. Ive also made a dict for user key to user name for acknowledges. The issue is that the pushover api documentation is not easy to understand if you have never used json before – shaggs Nov 09 '15 at 23:02

1 Answers1

3

Because it's json type, so you can just use json module to load the data as a dict. For example if you have some json data like this:

'{"message":"Hello there, wayfaring stranger"}'

Then you can use json.loads() function to load it as a dict like this:

>>> a = '{"message":"Hello there, wayfaring stranger"}'
>>> import json
>>> d = json.loads(a)
>>> d.items()
dict_items([('message', 'Hello there, wayfaring stranger')])
>>> 

As you can see, a is string, but d is a dict. And now, 'Hello there, wayfaring stranger' is the value of key 'message'.


But msg.json() can auto covert the json data to dict, so you don't need json module here. So if you want to get 'ef5f42d568e966cbf3d2b28c6579397c', you can simply do something like this:

json_data = msg.json()
print json_data['request']
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
  • thanks works a treat. I had looked at that link and that's how i got the output as pushovers documentation for python is very limited. – shaggs Oct 18 '15 at 00:03
  • @shaggs i'm happy to hear that :) – Remi Guan Oct 18 '15 at 00:09
  • now to workout _`https://api.pushover.net/1/receipts/(your receipt).json?token=(your app token)`_ and poll till I get reply – shaggs Oct 18 '15 at 00:16