diff options
-rw-r--r-- | README.md | 27 | ||||
-rw-r--r-- | wee_slack.py | 51 |
2 files changed, 77 insertions, 1 deletions
@@ -88,10 +88,35 @@ weechat **NOTE:** If weechat is already running, the script can be loaded using ``/python load python/autoload/wee_slack.py`` #### 4. Add your Slack API key(s) + +Log in to Slack: + +``` +/slack register +``` + +This command prints a link you should open in your browser to authorize WeeChat +with Slack. Once you've accomplished this, copy the "code" portion of the URL in +the browser and pass it to this command: + +``` +/slack register [YOUR_SLACK_TOKEN] +``` + +Your Slack team is now added, and you can complete setup by restarting the +wee-slack plugin. + +``` +/python reload slack +``` + +Alternatively, you can click the "Request token" button at the +[Slack legacy token page](https://api.slack.com/custom-integrations/legacy-tokens), +and paste it directly into your settings: + ``` /set plugins.var.python.slack.slack_api_token [YOUR_SLACK_TOKEN] ``` -^^ (find this at https://api.slack.com/custom-integrations/legacy-tokens using the "Request token" button) If you don't want to store your API token in plaintext you can use the secure features of weechat: diff --git a/wee_slack.py b/wee_slack.py index c452b1b..651e995 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -6,6 +6,7 @@ from collections import OrderedDict from functools import wraps from itertools import islice +import textwrap import time import json import pickle @@ -3004,6 +3005,52 @@ def me_command_cb(data, current_buffer, args): return w.WEECHAT_RC_OK_EAT +def command_register(data, current_buffer, args): + CLIENT_ID = "2468770254.51917335286" + CLIENT_SECRET = "dcb7fe380a000cba0cca3169a5fe8d70" # Not really a secret. + if args == 'register': + message = textwrap.dedent(""" + #### Retrieving a Slack token via OAUTH #### + + 1) Paste this into a browser: https://slack.com/oauth/authorize?client_id=2468770254.51917335286&scope=client + 2) Select the team you wish to access from wee-slack in your browser. + 3) Click "Authorize" in the browser **IMPORTANT: the redirect will fail, this is expected** + 4) Copy the "code" portion of the URL to your clipboard + 5) Return to weechat and run `/slack register [code]` + """) + w.prnt("", message) + return + + try: + _, oauth_code = args.split() + except ValueError: + w.prnt("", + "ERROR: wrong number of arguments given for register command") + return + + uri = ( + "https://slack.com/api/oauth.access?" + "client_id={}&client_secret={}&code={}" + ).format(CLIENT_ID, CLIENT_SECRET, oauth_code) + ret = urllib.urlopen(uri).read() + d = json.loads(ret) + if not d["ok"]: + w.prnt("", + "ERROR: Couldn't get Slack OAuth token: {}".format(d['error'])) + return + + if config.is_default('slack_api_token'): + w.config_set_plugin('slack_api_token', d['access_token']) + else: + # Add new token to existing set, joined by comma. + tok = config.get_string('slack_api_token') + w.config_set_plugin('slack_api_token', + ','.join([tok, d['access_token']])) + + w.prnt("", "Success! Added team \"%s\"" % (d['team_name'],)) + w.prnt("", "Please reload wee-slack with: /script reload slack") + + @slack_buffer_or_ignore @utf8_decode def msg_command_cb(data, current_buffer, args): @@ -3651,6 +3698,10 @@ class PluginConfig(object): def get_int(self, key): return int(w.config_get_plugin(key)) + def is_default(self, key): + default = self.default_settings.get(key).default + return w.config_get_plugin(key) == default + get_debug_level = get_int get_group_name_prefix = get_string get_map_underline_to = get_string |