Skip to content

Instantly share code, notes, and snippets.

@trygveaa
Created February 15, 2019 15:16
Show Gist options
  • Save trygveaa/1451e7f8dfbfb950a710fc176fc5e50a to your computer and use it in GitHub Desktop.
Save trygveaa/1451e7f8dfbfb950a710fc176fc5e50a to your computer and use it in GitHub Desktop.
#!/usr/bin/python2
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from websocket import create_connection
import json
import urllib2
try:
import weechat
except:
weechat = None
TOKEN='INSERT_TOKEN_HERE'
SCRIPT_NAME = "slack-test"
SCRIPT_AUTHOR = "trygveaa"
SCRIPT_VERSION = "0.1"
SCRIPT_LICENSE = "MIT"
SCRIPT_DESC = "Simple test of Slack connection"
slack_url = 'https://api.slack.com/api/rtm.connect?token=' + TOKEN
def connect_ws(rtm):
url = json.loads(rtm).get('url')
if url:
ws = create_connection(url)
response = ws.recv()
ws.close()
return response
else:
return 'missing url, got: ' + rtm
def connect_urllib2():
rtm = urllib2.urlopen(slack_url).read()
return connect_ws(rtm)
def connect_weechat():
weechat.hook_process_hashtable('url:' + slack_url, {}, 60000, 'callback_connect', '')
def callback_connect(data, command, return_code, out, err):
if return_code == 0 and not err:
weechat.prnt('', 'connect with weechat: ' + connect_ws(out))
else:
weechat.prnt('', 'connect with weechat failed: {} - {} - {}'.format(return_code, out, err))
return weechat.WEECHAT_RC_OK
if __name__ == "__main__":
if weechat:
weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, '', '')
weechat.prnt('', 'connect with urllib2: ' + connect_urllib2())
connect_weechat()
else:
print('connect with urllib2: ' + connect_urllib2())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment