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.

283 lines
13 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. * [SAS verification](#sas-verification)
  15. * [Developing](#developing)
  16. * [Architecture](#architecture)
  17. * [API Docs](#viewing-the-api-docs)
  18. # Quick Start
  19. Clone and run (Requires Go 1.14+):
  20. ```bash
  21. go build github.com/matrix-org/go-neb
  22. BIND_ADDRESS=:4050 DATABASE_TYPE=sqlite3 DATABASE_URL=go-neb.db?_busy_timeout=5000 BASE_URL=http://localhost:4050 ./go-neb
  23. ```
  24. Get a Matrix user ID and access token. You can do this, for example, with the following curl command by replacing the user ID, password and Synapse URL with your own.
  25. ```bash
  26. curl -X POST --header 'Content-Type: application/json' -d '{
  27. "identifier": { "type": "m.id.user", "user": "nebUsername" },
  28. "password": "nebPassword",
  29. "type": "m.login.password"
  30. }' 'http://localhost:8008/_matrix/client/r0/login'
  31. ```
  32. This is preferable to, for example, logging in via Riot and copying the access token and device ID from there, as then Riot will have uploaded its own device keys which Go-NEB won't have access to causing it to be unable to create encryption sessions.
  33. The response of this command will be a JSON object with an access token and device ID.
  34. Then, give the values to Go-NEB:
  35. ```bash
  36. curl -X POST localhost:4050/admin/configureClient --data-binary '{
  37. "UserID": "@goneb:localhost",
  38. "HomeserverURL": "http://localhost:8008",
  39. "AccessToken": "<access_token>",
  40. "DeviceID": "<DEVICEID>",
  41. "Sync": true,
  42. "AutoJoinRooms": true,
  43. "DisplayName": "My Bot"
  44. }'
  45. ```
  46. Tell it what service to run:
  47. ```bash
  48. curl -X POST localhost:4050/admin/configureService --data-binary '{
  49. "Type": "echo",
  50. "Id": "myserviceid",
  51. "UserID": "@goneb:localhost",
  52. "Config": {}
  53. }'
  54. ```
  55. Invite the bot user into a Matrix room and type `!echo hello world`. It will reply with `hello world`.
  56. ## Features
  57. ### Github
  58. - Login with OAuth2.
  59. - Ability to create Github issues on any project.
  60. - Ability to track updates (add webhooks) to projects. This includes new issues, pull requests as well as commits.
  61. - Ability to expand issues when mentioned as `foo/bar#1234`.
  62. - Ability to assign a "default repository" for a Matrix room to allow `#1234` to automatically expand, as well as shorter issue creation command syntax.
  63. ### JIRA
  64. - Login with OAuth1.
  65. - Ability to create JIRA issues on a project.
  66. - Ability to expand JIRA issues when mentioned as `FOO-1234`.
  67. ### Giphy
  68. - Ability to query Giphy's "text-to-gif" engine.
  69. ### Guggy
  70. - Ability to query Guggy's gif engine.
  71. ### RSS Bot
  72. - Ability to read Atom/RSS feeds.
  73. ### Travis CI
  74. - Ability to receive incoming build notifications.
  75. - Ability to adjust the message which is sent into the room.
  76. ### Alertmanager
  77. - Ability to receive alerts and render them with go templates
  78. # Installing
  79. Go-NEB is built using Go 1.14+. Once you have installed Go, run the following commands:
  80. ```bash
  81. # Clone the go-neb repository
  82. git clone https://github.com/matrix-org/go-neb
  83. cd go-neb
  84. # Build go-neb
  85. go build github.com/matrix-org/go-neb
  86. ```
  87. # Running
  88. Go-NEB uses environment variables to configure its SQLite database and bind address. To run Go-NEB, run the following command:
  89. ```bash
  90. BIND_ADDRESS=:4050 DATABASE_TYPE=sqlite3 DATABASE_URL=go-neb.db?_busy_timeout=5000 BASE_URL=https://public.facing.endpoint ./go-neb
  91. ```
  92. - `BIND_ADDRESS` is the port to listen on.
  93. - `DATABASE_TYPE` MUST be "sqlite3". No other type is supported.
  94. - `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.
  95. - `BASE_URL` should be the public-facing endpoint that sites like Github can send webhooks to.
  96. - `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.
  97. - `LOG_DIR` is a directory that log files will be written to, with log rotation enabled. If set, logging to stderr will be disabled.
  98. 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.
  99. ## Configuration file
  100. 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.
  101. # API
  102. The API is documented in sections using godoc. The sections consists of:
  103. - An HTTP API (the path and method to use)
  104. - A "JSON request body" (the JSON that is inside the HTTP request body)
  105. - "Configuration" information (any additional information that is specific to what you're creating)
  106. 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.
  107. ## Configuring Clients
  108. 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.
  109. - [HTTP API Docs](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/api/handlers/index.html#ConfigureClient.OnIncomingRequest)
  110. - [JSON Request Body Docs](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/api/index.html#ClientConfig)
  111. ## Configuring Services
  112. 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.
  113. Every service has an "ID", "type" and "user ID". Services may specify additional "config" keys: see the specific
  114. service you're interested in for the additional keys, if any.
  115. - [HTTP API Docs](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/api/handlers/index.html#ConfigureService.OnIncomingRequest)
  116. - [JSON Request Body Docs](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/api/index.html#ConfigureServiceRequest)
  117. List of Services:
  118. - [Echo](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/services/echo/) - An example service
  119. - [Giphy](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/services/giphy/) - A GIF bot
  120. - [Github](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/services/github/) - A Github bot
  121. - [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
  122. - [Guggy](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/services/guggy/) - A GIF bot
  123. - [JIRA](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/services/jira/) - Integration with JIRA
  124. - [RSS Bot](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/services/rssbot/) - An Atom/RSS feed reader
  125. - [Travis CI](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/services/travisci/) - Receive build notifications from Travis CI
  126. ## Configuring Realms
  127. Realms are how Go-NEB authenticates users on third-party websites.
  128. - [HTTP API Docs](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/api/handlers/index.html#ConfigureAuthRealm.OnIncomingRequest)
  129. - [JSON Request Body Docs](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/api/index.html#ConfigureAuthRealmRequest)
  130. List of Realms:
  131. - [Github](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/realms/github/index.html#Realm)
  132. - [JIRA](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/realms/jira/index.html#Realm)
  133. Authentication via HTTP:
  134. - [Github](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/realms/github/index.html#Realm.RequestAuthSession)
  135. - [JIRA](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/realms/jira/index.html#Realm.RequestAuthSession)
  136. Authentication via the config file:
  137. - [Github](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/realms/github/index.html#Session)
  138. - [JIRA](https://matrix-org.github.io/go-neb/pkg/github.com/matrix-org/go-neb/realms/jira/index.html#Session)
  139. ## SAS verification
  140. Go-NEB supports SAS verification using the decimal method. Another user can start a verification transaction with Go-NEB using their client, and it will be accepted. In order to confirm the devices, the 3 SAS integers must then be sent to Go-NEB, to the endpoint '/verifySAS' so that it can mark the device as trusted.
  141. For example, if your user ID is `@user:localhost` and your device ID is `ABCD`, you start a SAS verification with Go-NEB and get the SAS "1111 2222 3333". You can perform the following curl request to let Go-NEB know the SAS integers so that it can match them with its own:
  142. ```bash
  143. curl -X POST --header 'Content-Type: application/json' -d '{
  144. "UserID": "@neb:localhost",
  145. "OtherUserID": "@user:localhost",
  146. "OtherDeviceID": "ABCD",
  147. "SAS": [1111,2222,3333]
  148. }' 'http://localhost:4050/verifySAS'
  149. ```
  150. If the SAS match and you also confirm that via the other device's client, the verification should finish successfully.
  151. # Contributing
  152. 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.
  153. # Developing
  154. This project depends on `libolm` for the end-to-end encryption. Therefore,
  155. you need to install `libolm3` and `libolm-dev` on Ubuntu / `libolm-devel` on
  156. CentOS to be able to build and run it.
  157. There's a bunch more tools this project uses when developing in order to do
  158. things like linting. Some of them are bundled with go (fmt and vet) but some
  159. are not. You should install the ones which are not:
  160. ```bash
  161. go get golang.org/x/lint/golint
  162. go get github.com/fzipp/gocyclo
  163. ```
  164. You can then install the pre-commit hook:
  165. ```bash
  166. ./hooks/install.sh
  167. ```
  168. ## Architecture
  169. ```
  170. HOMESERVER
  171. |
  172. +=============================================================+
  173. | | Go-NEB |
  174. | +---------+ |
  175. | | Clients | |
  176. | +---------+ |
  177. | | |
  178. | +---------+ +------------+ +--------------+ |
  179. | | Service |-------| Auth Realm |------| Auth Session |-+ |
  180. | +---------+ +------------+ +--------------+ | |
  181. | ^ ^ +---------------+ |
  182. | | | |
  183. +=============================================================+
  184. | |
  185. WEBHOOK REDIRECT
  186. REQUEST REQUEST
  187. Clients = A thing which can talk to homeservers and listen for events. /configureClient makes these.
  188. Service = An individual bot, configured by a user. /configureService makes these.
  189. Auth Realm = A place where a user can authenticate with. /configureAuthRealm makes these.
  190. Auth Session = An individual authentication session /requestAuthSession makes these.
  191. ```
  192. ## Viewing the API docs
  193. 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:
  194. ```
  195. # Start a documentation server listening on :6060
  196. GOPATH=$GOPATH:$(pwd) godoc -v -http=localhost:6060 &
  197. # Open up the documentation for go-neb in a browser.
  198. sensible-browser http://localhost:6060/pkg/github.com/matrix-org/go-neb
  199. ```
  200. ## Docker image
  201. To get started quickly, use the image from docker.io:
  202. ```
  203. docker run -v /path/to/data:/data -e "BASE_URL=http://your.public.url:4050" matrixdotorg/go-neb
  204. ```
  205. If you'd prefer to build the file yourself, clone this repository and build the `Dockerfile`.
  206. The image sets the following environment variables:
  207. ```
  208. BIND_ADDRESS=:4050
  209. DATABASE_TYPE=sqlite3
  210. DATABASE_URL=/data/go-neb.db?_busy_timeout=5000
  211. ```
  212. 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.