You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
9.7 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. # Go-NEB
  2. Go-NEB is a [Matrix](https://matrix.org) bot written in Go. It is the successor to [Matrix-NEB](https://github.com/matrix-org/Matrix-NEB), the original Matrix bot written in Python.
  3. # Table of Contents
  4. * [Quick Start](#quick-start)
  5. * [Features](#features)
  6. * [Installing](#installing)
  7. * [Running](#running)
  8. * [Configuration file](#configuration-file)
  9. * [API](#api)
  10. * [Configuring clients](#configuring-clients)
  11. * [Configuring services](#configuring-services)
  12. * [Configuring realms](#configuring-realms)
  13. * [Developing](#developing)
  14. * [Architecture](#architecture)
  15. * [API Docs](#viewing-the-api-docs)
  16. # Quick Start
  17. Clone and run (Requires Go 1.5+ and GB):
  18. ```bash
  19. gb build github.com/matrix-org/go-neb
  20. BIND_ADDRESS=:4050 DATABASE_TYPE=sqlite3 DATABASE_URL=go-neb.db?_busy_timeout=5000 BASE_URL=http://localhost:4050 bin/go-neb
  21. ```
  22. Get a Matrix user ID and access token and give it to Go-NEB:
  23. ```bash
  24. curl -X POST localhost:4050/admin/configureClient --data-binary '{
  25. "UserID": "@goneb:localhost",
  26. "HomeserverURL": "http://localhost:8008",
  27. "AccessToken": "<access_token>",
  28. "Sync": true,
  29. "AutoJoinRooms": true,
  30. "DisplayName": "My Bot"
  31. }'
  32. ```
  33. Tell it what service to run:
  34. ```bash
  35. curl -X POST localhost:4050/admin/configureService --data-binary '{
  36. "Type": "echo",
  37. "Id": "myserviceid",
  38. "UserID": "@goneb:localhost",
  39. "Config": {}
  40. }'
  41. ```
  42. Invite the bot user into a Matrix room and type `!echo hello world`. It will reply with `hello world`.
  43. ## Features
  44. ### Github
  45. - Login with OAuth2.
  46. - Ability to create Github issues on any project.
  47. - Ability to track updates (add webhooks) to projects. This includes new issues, pull requests as well as commits.
  48. - Ability to expand issues when mentioned as `foo/bar#1234`.
  49. - Ability to assign a "default repository" for a Matrix room to allow `#1234` to automatically expand, as well as shorter issue creation command syntax.
  50. ### JIRA
  51. - Login with OAuth1.
  52. - Ability to create JIRA issues on a project.
  53. - Ability to expand JIRA issues when mentioned as `FOO-1234`.
  54. ### Giphy
  55. - Ability to query Giphy's "text-to-gif" engine.
  56. ### Guggy
  57. # Installing
  58. Go-NEB is built using Go 1.5+ and [GB](https://getgb.io/). Once you have installed Go, run the following commands:
  59. ```bash
  60. # Install gb
  61. go get github.com/constabulary/gb/...
  62. # Clone the go-neb repository
  63. git clone https://github.com/matrix-org/go-neb
  64. cd go-neb
  65. # Build go-neb
  66. gb build github.com/matrix-org/go-neb
  67. ```
  68. # Running
  69. Go-NEB uses environment variables to configure its SQLite database and bind address. To run Go-NEB, run the following command:
  70. ```bash
  71. BIND_ADDRESS=:4050 DATABASE_TYPE=sqlite3 DATABASE_URL=go-neb.db?_busy_timeout=5000 BASE_URL=https://public.facing.endpoint bin/go-neb
  72. ```
  73. - `BIND_ADDRESS` is the port to listen on.
  74. - `DATABASE_TYPE` MUST be "sqlite3". No other type is supported.
  75. - `DATABASE_URL` is where to find the database file. One will be created if it does not exist. It is a URL so parameters can be passed to it. We recommend setting `_busy_timeout=5000` to prevent sqlite3 "database is locked" errors.
  76. - `BASE_URL` should be the public-facing endpoint that sites like Github can send webhooks to.
  77. - `CONFIG_FILE` is the path to the configuration file to read from. This isn't included in the example above, so Go-NEB will operate in HTTP mode.
  78. Go-NEB needs to be "configured" with clients and services before it will do anything useful. It can be configured via a configuration file OR by an HTTP API.
  79. ## Configuration file
  80. If you run Go-NEB with a `CONFIG_FILE` environment variable, it will load that file and use it for services, clients, etc. There is a [sample configuration file](config.sample.yaml) which explains all the options. In most cases, these are *direct mappings* to the corresponding HTTP API.
  81. # API
  82. The API is documented in sections using godoc. The sections consists of:
  83. - An HTTP API (the path and method to use)
  84. - A "JSON request body" (the JSON that is inside the HTTP request body)
  85. - "Configuration" information (any additional information that is specific to what you're creating)
  86. To form the complete API, you need to combine the HTTP API with the JSON request body, and the "Configuration" information (which is always under a JSON key called `Config`). In addition, most APIs have a `Type` which determines which piece of code to load. To find out what the right type is for the thing you're creating, check the constants defined in godoc.
  87. ## Configuring Clients
  88. Go-NEB needs to connect as a matrix user to receive messages. Go-NEB can listen for messages as multiple matrix users. The users are configured using an HTTP API and the config is stored in the database.
  89. - [HTTP API Docs](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/api/handlers/index.html#ConfigureClient.OnIncomingRequest)
  90. - [JSON Request Body Docs](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/api/index.html#ClientConfig)
  91. ## Configuring Services
  92. Services contain all the useful functionality in Go-NEB. They require a client to operate. Services are configured using an HTTP API and the config is stored in the database. Services use one of the matrix users configured on Go-NEB to send/receive matrix messages.
  93. Every service has an "ID", "type" and "user ID". Services may specify additional "config" keys: see the specific
  94. service you're interested in for the additional keys, if any.
  95. - [HTTP API Docs](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/api/handlers/index.html#ConfigureService.OnIncomingRequest)
  96. - [JSON Request Body Docs](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/api/index.html#ConfigureServiceRequest)
  97. List of Services:
  98. - [Echo](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/services/echo/) - An example service
  99. - [Giphy](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/services/giphy/) - A GIF bot
  100. - [Github](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/services/github/) - A Github bot
  101. - [Github Webhook](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/services/github/index.html#WebhookService) - A Github notification bot
  102. - [Guggy](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/services/guggy/) - A GIF bot
  103. - [JIRA](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/services/jira/) - Integration with JIRA
  104. - [RSS Bot](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/services/rssbot/) - An Atom/RSS feed reader
  105. ## Configuring Realms
  106. Realms are how Go-NEB authenticates users on third-party websites.
  107. - [HTTP API Docs](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/api/handlers/index.html#ConfigureAuthRealm.OnIncomingRequest)
  108. - [JSON Request Body Docs](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/api/index.html#ConfigureAuthRealmRequest)
  109. List of Realms:
  110. - [Github](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/realms/github/index.html#Realm)
  111. - [JIRA](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/realms/jira/index.html#Realm)
  112. Authentication via HTTP:
  113. - [Github](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/realms/github/index.html#Realm.RequestAuthSession)
  114. - [JIRA](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/realms/jira/index.html#Realm.RequestAuthSession)
  115. Authentication via the config file:
  116. - [Github](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/realms/github/index.html#Session)
  117. - [JIRA](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/realms/jira/index.html#Session)
  118. # Developing
  119. There's a bunch more tools this project uses when developing in order to do
  120. things like linting. Some of them are bundled with go (fmt and vet) but some
  121. are not. You should install the ones which are not:
  122. ```bash
  123. go get github.com/golang/lint/golint
  124. go get github.com/fzipp/gocyclo
  125. ```
  126. You can then install the pre-commit hook:
  127. ```bash
  128. ./hooks/install.sh
  129. ```
  130. ## Architecture
  131. ```
  132. HOMESERVER
  133. |
  134. +=============================================================+
  135. | | Go-NEB |
  136. | +---------+ |
  137. | | Clients | |
  138. | +---------+ |
  139. | | |
  140. | +---------+ +------------+ +--------------+ |
  141. | | Service |-------| Auth Realm |------| Auth Session |-+ |
  142. | +---------+ +------------+ +--------------+ | |
  143. | ^ ^ +---------------+ |
  144. | | | |
  145. +=============================================================+
  146. | |
  147. WEBHOOK REDIRECT
  148. REQUEST REQUEST
  149. Clients = A thing which can talk to homeservers and listen for events. /configureClient makes these.
  150. Service = An individual bot, configured by a user. /configureService makes these.
  151. Auth Realm = A place where a user can authenticate with. /configureAuthRealm makes these.
  152. Auth Session = An individual authentication session /requestAuthSession makes these.
  153. ```
  154. ## Viewing the API docs
  155. The full docs can be found on [Github Pages](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb). Alternatively, you can locally host the API docs:
  156. ```
  157. # Start a documentation server listening on :6060
  158. GOPATH=$GOPATH:$(pwd) godoc -v -http=localhost:6060 &
  159. # Open up the documentation for go-neb in a browser.
  160. sensible-browser http://localhost:6060/pkg/github.com/matrix-org/go-neb
  161. ```