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.

247 lines
11 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
8 years ago
8 years ago
8 years ago
  1. # Go-NEB
  2. [![Build Status](https://travis-ci.org/matrix-org/go-neb.svg?branch=master)](https://travis-ci.org/matrix-org/go-neb)
  3. 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.
  4. # Table of Contents
  5. * [Quick Start](#quick-start)
  6. * [Features](#features)
  7. * [Installing](#installing)
  8. * [Running](#running)
  9. * [Configuration file](#configuration-file)
  10. * [API](#api)
  11. * [Configuring clients](#configuring-clients)
  12. * [Configuring services](#configuring-services)
  13. * [Configuring realms](#configuring-realms)
  14. * [Developing](#developing)
  15. * [Architecture](#architecture)
  16. * [API Docs](#viewing-the-api-docs)
  17. # Quick Start
  18. Clone and run (Requires Go 1.14+):
  19. ```bash
  20. go build github.com/matrix-org/go-neb
  21. BIND_ADDRESS=:4050 DATABASE_TYPE=sqlite3 DATABASE_URL=go-neb.db?_busy_timeout=5000 BASE_URL=http://localhost:4050 ./go-neb
  22. ```
  23. Get a Matrix user ID and access token and give it to Go-NEB:
  24. ```bash
  25. curl -X POST localhost:4050/admin/configureClient --data-binary '{
  26. "UserID": "@goneb:localhost",
  27. "HomeserverURL": "http://localhost:8008",
  28. "AccessToken": "<access_token>",
  29. "Sync": true,
  30. "AutoJoinRooms": true,
  31. "DisplayName": "My Bot"
  32. }'
  33. ```
  34. Tell it what service to run:
  35. ```bash
  36. curl -X POST localhost:4050/admin/configureService --data-binary '{
  37. "Type": "echo",
  38. "Id": "myserviceid",
  39. "UserID": "@goneb:localhost",
  40. "Config": {}
  41. }'
  42. ```
  43. Invite the bot user into a Matrix room and type `!echo hello world`. It will reply with `hello world`.
  44. ## Features
  45. ### Github
  46. - Login with OAuth2.
  47. - Ability to create Github issues on any project.
  48. - Ability to track updates (add webhooks) to projects. This includes new issues, pull requests as well as commits.
  49. - Ability to expand issues when mentioned as `foo/bar#1234`.
  50. - Ability to assign a "default repository" for a Matrix room to allow `#1234` to automatically expand, as well as shorter issue creation command syntax.
  51. ### JIRA
  52. - Login with OAuth1.
  53. - Ability to create JIRA issues on a project.
  54. - Ability to expand JIRA issues when mentioned as `FOO-1234`.
  55. ### Giphy
  56. - Ability to query Giphy's "text-to-gif" engine.
  57. ### Guggy
  58. - Ability to query Guggy's gif engine.
  59. ### RSS Bot
  60. - Ability to read Atom/RSS feeds.
  61. ### Travis CI
  62. - Ability to receive incoming build notifications.
  63. - Ability to adjust the message which is sent into the room.
  64. ### Alertmanager
  65. - Ability to receive alerts and render them with go templates
  66. # Installing
  67. Go-NEB is built using Go 1.14+. Once you have installed Go, run the following commands:
  68. ```bash
  69. # Clone the go-neb repository
  70. git clone https://github.com/matrix-org/go-neb
  71. cd go-neb
  72. # Build go-neb
  73. go build github.com/matrix-org/go-neb
  74. ```
  75. # Running
  76. Go-NEB uses environment variables to configure its SQLite database and bind address. To run Go-NEB, run the following command:
  77. ```bash
  78. BIND_ADDRESS=:4050 DATABASE_TYPE=sqlite3 DATABASE_URL=go-neb.db?_busy_timeout=5000 BASE_URL=https://public.facing.endpoint ./go-neb
  79. ```
  80. - `BIND_ADDRESS` is the port to listen on.
  81. - `DATABASE_TYPE` MUST be "sqlite3". No other type is supported.
  82. - `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.
  83. - `BASE_URL` should be the public-facing endpoint that sites like Github can send webhooks to.
  84. - `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.
  85. - `LOG_DIR` is a directory that log files will be written to, with log rotation enabled. If set, logging to stderr will be disabled.
  86. 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.
  87. ## Configuration file
  88. 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.
  89. # API
  90. The API is documented in sections using godoc. The sections consists of:
  91. - An HTTP API (the path and method to use)
  92. - A "JSON request body" (the JSON that is inside the HTTP request body)
  93. - "Configuration" information (any additional information that is specific to what you're creating)
  94. 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.
  95. ## Configuring Clients
  96. 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.
  97. - [HTTP API Docs](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/api/handlers/index.html#ConfigureClient.OnIncomingRequest)
  98. - [JSON Request Body Docs](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/api/index.html#ClientConfig)
  99. ## Configuring Services
  100. 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.
  101. Every service has an "ID", "type" and "user ID". Services may specify additional "config" keys: see the specific
  102. service you're interested in for the additional keys, if any.
  103. - [HTTP API Docs](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/api/handlers/index.html#ConfigureService.OnIncomingRequest)
  104. - [JSON Request Body Docs](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/api/index.html#ConfigureServiceRequest)
  105. List of Services:
  106. - [Echo](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/services/echo/) - An example service
  107. - [Giphy](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/services/giphy/) - A GIF bot
  108. - [Github](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/services/github/) - A Github bot
  109. - [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
  110. - [Guggy](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/services/guggy/) - A GIF bot
  111. - [JIRA](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/services/jira/) - Integration with JIRA
  112. - [RSS Bot](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/services/rssbot/) - An Atom/RSS feed reader
  113. - [Travis CI](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/services/travisci/) - Receive build notifications from Travis CI
  114. ## Configuring Realms
  115. Realms are how Go-NEB authenticates users on third-party websites.
  116. - [HTTP API Docs](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/api/handlers/index.html#ConfigureAuthRealm.OnIncomingRequest)
  117. - [JSON Request Body Docs](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/api/index.html#ConfigureAuthRealmRequest)
  118. List of Realms:
  119. - [Github](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/realms/github/index.html#Realm)
  120. - [JIRA](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/realms/jira/index.html#Realm)
  121. Authentication via HTTP:
  122. - [Github](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/realms/github/index.html#Realm.RequestAuthSession)
  123. - [JIRA](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/realms/jira/index.html#Realm.RequestAuthSession)
  124. Authentication via the config file:
  125. - [Github](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/realms/github/index.html#Session)
  126. - [JIRA](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/realms/jira/index.html#Session)
  127. # Contributing
  128. Before submitting pull requests, please read the [Matrix.org contribution guidelines](https://github.com/matrix-org/synapse/blob/develop/CONTRIBUTING.md#sign-off) regarding sign-off of your work.
  129. # Developing
  130. There's a bunch more tools this project uses when developing in order to do
  131. things like linting. Some of them are bundled with go (fmt and vet) but some
  132. are not. You should install the ones which are not:
  133. ```bash
  134. go get golang.org/x/lint/golint
  135. go get github.com/fzipp/gocyclo
  136. ```
  137. You can then install the pre-commit hook:
  138. ```bash
  139. ./hooks/install.sh
  140. ```
  141. ## Architecture
  142. ```
  143. HOMESERVER
  144. |
  145. +=============================================================+
  146. | | Go-NEB |
  147. | +---------+ |
  148. | | Clients | |
  149. | +---------+ |
  150. | | |
  151. | +---------+ +------------+ +--------------+ |
  152. | | Service |-------| Auth Realm |------| Auth Session |-+ |
  153. | +---------+ +------------+ +--------------+ | |
  154. | ^ ^ +---------------+ |
  155. | | | |
  156. +=============================================================+
  157. | |
  158. WEBHOOK REDIRECT
  159. REQUEST REQUEST
  160. Clients = A thing which can talk to homeservers and listen for events. /configureClient makes these.
  161. Service = An individual bot, configured by a user. /configureService makes these.
  162. Auth Realm = A place where a user can authenticate with. /configureAuthRealm makes these.
  163. Auth Session = An individual authentication session /requestAuthSession makes these.
  164. ```
  165. ## Viewing the API docs
  166. 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:
  167. ```
  168. # Start a documentation server listening on :6060
  169. GOPATH=$GOPATH:$(pwd) godoc -v -http=localhost:6060 &
  170. # Open up the documentation for go-neb in a browser.
  171. sensible-browser http://localhost:6060/pkg/github.com/matrix-org/go-neb
  172. ```
  173. ## Docker image
  174. To get started quickly, use the image from docker.io:
  175. ```
  176. docker run -v /path/to/data:/data -e "BASE_URL=http://your.public.url:4050" matrixdotorg/go-neb
  177. ```
  178. If you'd prefer to build the file yourself, clone this repository and build the `Dockerfile`.
  179. The image sets the following environment variables:
  180. ```
  181. BIND_ADDRESS=:4050
  182. DATABASE_TYPE=sqlite3
  183. DATABASE_URL=/data/go-neb.db?_busy_timeout=5000
  184. ```
  185. The image exposes port `4050` and a volume at `/data`. The `BASE_URL` environment variable needs to be set, a volume should be mounted at `/data` and port `4050` should be appropriately mapped as desired.