json - Listen for an event on python and parse though the data -
i designer, , have been given potentially impossible task learn python, right now, boss.
currently have python script has connected websocket between slack , our server retrieves json data sent user bot on slack.
here code:
from django.core.management.base import basecommand websocket import create_connection import requests import json import sys class command(basecommand): def handle(self, *args, **kwargs): url = (json.loads(requests.get("https://slack.com/api/rtm.start?token=xxxxxxx").content))['url'] s = create_connection(url) s.sock.setblocking(0) while true: try: print s.recv() #self.stdout.write("{}\n").format(s.recv()) except (keyboardinterrupt, systemexit): sys.exit() raise except: continue c = command() c.handle() here example of json data retrieving:
{ "type":"message", "message": { "type":"message", "user":"u045xdv70", "text":"<#c08sy155g> <http://seedinvest.com>", "attachments":[ {"title":"seedinvest", "title_link":"http://seedinvest.com", "text":"seedinvest equity crowdfunding platform connects accredited investors vetted startups , small businesses seeking funding.", "fallback":"seedinvest", "thumb_url":"https://d2v4u62fw1nf2w.cloudfront.net/app/img/website/logo_square_200.png", "from_url":"http://seedinvest.com", "thumb_width":200, "thumb_height":200, "id":1} ], "ts":"1443638877.000008" }, "subtype":"message_changed", "hidden":true, "channel":"d0bbf1jjg", "event_ts":"1443638878.008753", "ts":"1443638878.000009" } now, next task to:
- listen "message" event
- when "message" event comes in "attachment"
- parse message looking channel markers, example:<#c08sy155g>
- then re-post "attachment" channel.
i getting lot of direct slack api, however, writing in python whole other animal, , not sure started, or in section attempt start writing something.
any sort of direction appreciated.
so direction, when import json object, can use other dictionary object. looking "message" ["type"] , check values.
Comments
Post a Comment