Coder Social home page Coder Social logo

theckman / slackmech Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 1.0 1.75 MB

slackmech provides help for mechanizing request to the Slack Web/Electron UI API

License: MIT License

Go 83.35% HTML 16.40% Makefile 0.25%
golang slack slack-api golang-library mechanize

slackmech's Introduction

slackmech

License GoDoc Page Latest Git Tag TravisCI master Branch Build Status Go Cover Test Coverage Go Report Card

Package slackmech is a package for mechanizing requests to the Slack API. It helps imitate a web session with Slack, to present you with a session API token. This token has different API access, including undocumented behaviors, to allow special features of the Web/Electron UI. This package will also abstract away some of those API calls, making it easier to consume them.

As of now, the features of this project are focused towards a bot for moderating Open Source Slack communities. Things will be added as they are needed to support that effort, or as contributions from others are made.

This package is still under active development.

Compatibility Notice

The behaviors and functions relied on by this package are largely undocumented, and the usage of them fall outside of any compatibility guarantees provided by Slack. While the functionality provided by this package is necessary, it's reasonable to assume it may break unexpectedly in the future.

As such, at the time of writing, it seems unlikely that this package will ever be able to see a 1.0.0 release. However, efforts will be made to retain API compatibility as long as possible. The package will have releases, using git tags, following the Semantic Version v2.0.0 spec.

License

This source code of this package is released under the MIT License. Please see the LICENSE for the full content of the license.

Usage

To be a respectful user of the Slack APIs, be sure to use an *http.Client with a cookiejar set, preferably one that makes use of persistence. In our example, we'll use the standard cookiejar implementation.

Here we'll create a cookiekar, create an HTTP client, create a new slackmech client, log in, and then use the Session Token to create a new API client:

import (
	"log"
	"net/http"
	"net/http/cookiejar"
	"os"

	"github.com/nlopes/slack"
	"github.com/theckman/slackmech"

	"golang.org/x/net/publicsuffix"
)

func main() {
	email := os.Getenv("SLACK_EMAIL")
	password := os.Getenv("SLACK_PASSWORD")
	subdomain := os.Getenv("SLACK_SUBDOMAIN")

	cj, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
	if err != nil {
		log.Fatal(err)
	}

	// new http client with cookiejar set
	client := &http.Client{
		Jar: cj,
	}

	// don't follow redirects -- needed by slackmech
	client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
		return http.ErrUseLastResponse
	}

	// new slackmech client
	sm, err := slackmech.New(client, subdomain)
	if err != nil {
		log.Fatal(err)
	}

	// log in and get a new session
	err = sm.StartSession(email, password)
	if err != nil {
		log.Fatal(err)
	}

	// use the slackmech SessionToken to build a new Slack API client
	// also pass in the HTTP client used to log in to continue using cookies
	api := slack.New(sm.SessionToken(), slack.OptionHTTPClient(client))
}

slackmech's People

Contributors

theckman avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

akupila

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.