How to post to hipchat from python

不问归期 提交于 2019-12-03 22:07:06

In case it helps, here's a working V2 API example. I did find the V2 API to be a bit more sensitive about getting the form of the request exactly right. But, it might be more forward-looking to conform to the V2 API (though the original question seemed to pertain to V1).

#!/usr/bin/env python
import json
from urllib2 import Request, urlopen

V2TOKEN = '--V2 API token goes here--'
ROOMID = --room-id-nr-goes-here--

# API V2, send message to room:
url = 'https://api.hipchat.com/v2/room/%d/notification' % ROOMID
message = "It's a<br><em>trap!</em>"
headers = {
    "content-type": "application/json",
    "authorization": "Bearer %s" % V2TOKEN}
datastr = json.dumps({
    'message': message,
    'color': 'yellow',
    'message_format': 'html',
    'notify': False})
request = Request(url, headers=headers, data=datastr)
uo = urlopen(request)
rawresponse = ''.join(uo)
uo.close()
assert uo.code == 204

Another basic example using requests:

import requests, json

amessage = 'Hello World!'
room = 'https://api.hipchat.com/v2/room/18REPLACE35/notification'
headers = {'Authorization':'Bearer UGetYourOwnAuthKey', 'Content-type':'application/json'}
requests.post(url = room, data = json.dumps({'message':amessage}), headers = headers)

As Ianzz said, try including it in the URL query string. Although clunky (you probably want to hash it!), it definitely works.

The other strange quirk is the tokens that you get through Hipchat; I had no end of problems earlier this evening using my own personal token; it seemed to correspond to v2 beta of the API. If you go in through Group Admin and get a token from there, it may help.

Old question is old.

Here's an official list of libs which use the HipChat API v2 interface https://www.hipchat.com/docs/apiv2/libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!