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.

486 lines
18 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
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. * [Configuring clients](#configuring-clients)
  10. * [Configuring services](#configuring-services)
  11. * [Echo Service](#echo-service)
  12. * [Github Service](#github-service)
  13. * [Github Webhook Service](#github-webhook-service)
  14. * [JIRA Service](#jira-service)
  15. * [Giphy Service](#giphy-service)
  16. * [Configuring realms](#configuring-realms)
  17. * [Github Realm](#github-realm)
  18. * [Github Authentication](#github-authentication)
  19. * [JIRA Realm](#jira-realm)
  20. * [Developing](#developing)
  21. * [Architecture](#architecture)
  22. * [API Docs](#viewing-the-api-docs)
  23. # Quick Start
  24. Clone and run (Requires Go 1.5+ and GB):
  25. ```bash
  26. gb build github.com/matrix-org/go-neb
  27. BIND_ADDRESS=:4050 DATABASE_TYPE=sqlite3 DATABASE_URL=go-neb.db?_busy_timeout=5000 BASE_URL=http://localhost:4050 bin/go-neb
  28. ```
  29. Get a Matrix user ID and access token and give it to Go-NEB:
  30. ```bash
  31. curl -X POST localhost:4050/admin/configureClient --data-binary '{
  32. "UserID": "@goneb:localhost",
  33. "HomeserverURL": "http://localhost:8008",
  34. "AccessToken": "<access_token>",
  35. "Sync": true,
  36. "AutoJoinRooms": true,
  37. "DisplayName": "My Bot"
  38. }'
  39. ```
  40. Tell it what service to run:
  41. ```bash
  42. curl -X POST localhost:4050/admin/configureService --data-binary '{
  43. "Type": "echo",
  44. "Id": "myserviceid",
  45. "UserID": "@goneb:localhost",
  46. "Config": {}
  47. }'
  48. ```
  49. Invite the bot user into a Matrix room and type `!echo hello world`. It will reply with `hello world`.
  50. ## Features
  51. ### Github
  52. - Login with OAuth2.
  53. - Ability to create Github issues on any project.
  54. - Ability to track updates (add webhooks) to projects. This includes new issues, pull requests as well as commits.
  55. - Ability to expand issues when mentioned as `foo/bar#1234`.
  56. - Ability to assign a "default repository" for a Matrix room to allow `#1234` to automatically expand, as well as shorter issue creation command syntax.
  57. ### JIRA
  58. - Login with OAuth1.
  59. - Ability to create JIRA issues on a project.
  60. - Ability to expand JIRA issues when mentioned as `FOO-1234`.
  61. ### Giphy
  62. - Ability to query Giphy's "text-to-gif" engine.
  63. # Installing
  64. Go-NEB is built using Go 1.5+ and [GB](https://getgb.io/). Once you have installed Go, run the following commands:
  65. ```bash
  66. # Install gb
  67. go get github.com/constabulary/gb/...
  68. # Clone the go-neb repository
  69. git clone https://github.com/matrix-org/go-neb
  70. cd go-neb
  71. # Build go-neb
  72. gb build github.com/matrix-org/go-neb
  73. ```
  74. # Running
  75. Go-NEB uses environment variables to configure its SQLite database and bind address. To run Go-NEB, run the following command:
  76. ```bash
  77. BIND_ADDRESS=:4050 DATABASE_TYPE=sqlite3 DATABASE_URL=go-neb.db?_busy_timeout=5000 BASE_URL=https://public.facing.endpoint bin/go-neb
  78. ```
  79. - `BIND_ADDRESS` is the port to listen on.
  80. - `DATABASE_TYPE` MUST be "sqlite3". No other type is supported.
  81. - `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.
  82. - `BASE_URL` should be the public-facing endpoint that sites like Github can send webhooks to.
  83. - `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.
  84. 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.
  85. ## Configuration file
  86. 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.
  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. To create a user:
  89. ```bash
  90. curl -X POST localhost:4050/admin/configureClient --data-binary '{
  91. "UserID": "@goneb:localhost:8448",
  92. "HomeserverURL": "http://localhost:8008",
  93. "AccessToken": "<access_token>",
  94. "Sync": true,
  95. "AutoJoinRooms": true,
  96. "DisplayName": "My Bot"
  97. }'
  98. ```
  99. - `UserID` is the complete user ID of the client to connect as. The user MUST already exist.
  100. - `HomeserverURL` is the complete Homeserver URL for the given user ID.
  101. - `AccessToken` is the user's access token.
  102. - `Sync`, if `true`, will start a `/sync` stream so this client will receive incoming messages. This is required for services which need a live stream to the server (e.g. to respond to `!commands` and expand issues). It is not required for services which do not respond to Matrix users (e.g. webhook notifications).
  103. - `AutoJoinRooms`, if `true`, will automatically join rooms when an invite is received. This option is only valid when `Sync: true`.
  104. - `DisplayName`, if set, will set the given user's profile display name to the string given.
  105. Go-NEB will respond with the previous configuration for this client, if one exists, as well as echo back the complete configuration for the client:
  106. ```json
  107. {
  108. "OldClient": {},
  109. "NewClient": {
  110. "UserID": "@goneb:localhost:8448",
  111. "HomeserverURL": "http://localhost:8008",
  112. "AccessToken": "<access_token>",
  113. "Sync": true,
  114. "AutoJoinRooms": true,
  115. "DisplayName": "My Bot"
  116. }
  117. }
  118. ```
  119. ## Configuring Services
  120. 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.
  121. Every service MUST have the following fields:
  122. - `Type` : The type of service. This determines which code is executed.
  123. - `Id` : An arbitrary string which you can use to identify this service.
  124. - `UserID` : A user ID of a client which has been previously configured on Go-NEB. If this user does not exist, an error will be returned.
  125. - `Config` : A JSON object. The contents of this object depends on the service.
  126. The information about a Service can be retrieved based on their `Id` like so:
  127. ```bash
  128. curl -X POST localhost:4050/admin/getService --data-binary '{
  129. "Id": "myserviceid"
  130. }'
  131. ```
  132. This will return:
  133. ```yaml
  134. # HTTP 200 OK
  135. {
  136. "Type": "echo",
  137. "Id": "myserviceid",
  138. "UserID": "@goneb:localhost:8448",
  139. "Config": {}
  140. }
  141. ```
  142. If the service is not found, this will return:
  143. ```yaml
  144. # HTTP 404 Not Found
  145. { "message": "Service not found" }
  146. ```
  147. If you configure an existing Service (based on ID), the entire service will be replaced with the new information.
  148. ### Echo Service
  149. The simplest service. This will echo back any `!echo` command. To configure one:
  150. ```bash
  151. curl -X POST localhost:4050/admin/configureService --data-binary '{
  152. "Type": "echo",
  153. "Id": "myserviceid",
  154. "UserID": "@goneb:localhost:8448",
  155. "Config": {
  156. }
  157. }'
  158. ```
  159. Then invite `@goneb:localhost:8448` to any Matrix room and it will automatically join (if the client was configured to do so). Then try typing `!echo hello world` and the bot will respond with `hello world`.
  160. ### Github Service
  161. *Before you can set up a Github Service, you need to set up a [Github Realm](#github-realm).*
  162. *This service [requires a client](#configuring-clients) which has `Sync: true`.*
  163. This service will add the following command for [users who have associated their account with Github](#github-authentication):
  164. ```
  165. !github create owner/repo "Some title" "Some description"
  166. ```
  167. This service will also expand the following string into a short summary of the Github issue:
  168. ```
  169. owner/repo#1234
  170. ```
  171. You can create this service like so:
  172. ```bash
  173. curl -X POST localhost:4050/admin/configureService --data-binary '{
  174. "Type": "github",
  175. "Id": "githubcommands",
  176. "UserID": "@goneb:localhost",
  177. "Config": {
  178. "RealmID": "mygithubrealm"
  179. }
  180. }'
  181. ```
  182. - `RealmID`: The ID of the Github Realm you created earlier.
  183. You can set a "default repository" for a Matrix room by sending a `m.room.bot.options` state event which has the following `content`:
  184. ```json
  185. {
  186. "github": {
  187. "default_repo": "owner/repo"
  188. }
  189. }
  190. ```
  191. This will allow you to omit the `owner/repo` from both commands and expansions e.g `#12` will be treated as `owner/repo#12`.
  192. ### Github Webhook Service
  193. *Before you can set up a Github Webhook Service, you need to set up a [Github Realm](#github-realm).*
  194. This service will send notices into a Matrix room when Github sends webhook events to it. It requires a public domain which Github can reach. This service does not require a syncing client. Notices will be sent as the given `UserID`. To create this service:
  195. ```bash
  196. curl -X POST localhost:4050/admin/configureService --data-binary '{
  197. "Type": "github-webhook",
  198. "Id": "ghwebhooks",
  199. "UserID": "@goneb:localhost",
  200. "Config": {
  201. "RealmID": "mygithubrealm",
  202. "SecretToken": "a random string",
  203. "ClientUserID": "@a_real_user:localhost",
  204. "Rooms": {
  205. "!wefiuwegfiuwhe:localhost": {
  206. "Repos": {
  207. "owner/repo": {
  208. "Events": ["push"]
  209. },
  210. "owner/another-repo": {
  211. "Events": ["issues"]
  212. }
  213. }
  214. }
  215. }
  216. }
  217. }'
  218. ```
  219. - `RealmID`: The ID of the Github realm you created earlier.
  220. - `SecretToken`: Optional. If supplied, Go-NEB will perform security checks on incoming webhook requests using this token.
  221. - `ClientUserID`: The user ID of the Github user to setup webhooks as. This user MUST have [associated their user ID with a Github account](#github-authentication). Webhooks will be created using their OAuth token.
  222. - `Rooms`: A map of room IDs to room info.
  223. - `Repos`: A map of repositories to repo info.
  224. - `Events`: A list of webhook events to send into this room. Can be any of:
  225. - `push`: When users push to this repository.
  226. - `pull_request`: When a pull request is made to this repository.
  227. - `issues`: When an issue is opened/closed.
  228. - `issue_comment`: When an issue or pull request is commented on.
  229. - `pull_request_review_comment`: When a line comment is made on a pull request.
  230. ### JIRA Service
  231. *Before you can set up a JIRA Service, you need to set up a [JIRA Realm](#jira-realm).*
  232. TODO: Expand this section.
  233. ```
  234. curl -X POST localhost:4050/admin/configureService --data-binary '{
  235. "Type": "jira",
  236. "Id": "jid",
  237. "UserID": "@goneb:localhost",
  238. "Config": {
  239. "ClientUserID": "@example:localhost",
  240. "Rooms": {
  241. "!EmwxeXCVubhskuWvaw:localhost": {
  242. "Realms": {
  243. "jira_realm_id": {
  244. "Projects": {
  245. "BOTS": {
  246. "Expand": true,
  247. "Track": true
  248. }
  249. }
  250. }
  251. }
  252. }
  253. }
  254. }
  255. }'
  256. ```
  257. ### Giphy Service
  258. A simple service that adds the ability to use the `!giphy` command. To configure one:
  259. ```bash
  260. curl -X POST localhost:4050/admin/configureService --data-binary '{
  261. "Type": "giphy",
  262. "Id": "giphyid",
  263. "UserID": "@goneb:localhost",
  264. "Config": {
  265. "APIKey": "YOUR_API_KEY"
  266. }
  267. }'
  268. ```
  269. Then invite the user into a room and type `!giphy food` and it will respond with a GIF.
  270. ## Configuring Realms
  271. Realms are how Go-NEB authenticates users on third-party websites. Every realm MUST have the following fields:
  272. - `ID` : An arbitrary string you can use to remember what the realm is.
  273. - `Type`: The type of realm. This determines what code gets executed.
  274. - `Config`: A JSON object. The contents depends on the realm `Type`.
  275. They are configured like so:
  276. ```bash
  277. curl -X POST localhost:4050/admin/configureAuthRealm --data-binary '{
  278. "ID": "some_arbitrary_string",
  279. "Type": "some_realm_type",
  280. "Config": {
  281. ...
  282. }
  283. }'
  284. ```
  285. ### Github Realm
  286. This has the `Type` of `github`. To set up this realm:
  287. ```bash
  288. curl -X POST localhost:4050/admin/configureAuthRealm --data-binary '{
  289. "ID": "mygithubrealm",
  290. "Type": "github",
  291. "Config": {
  292. "ClientSecret": "YOUR_CLIENT_SECRET",
  293. "ClientID": "YOUR_CLIENT_ID",
  294. "StarterLink": "https://example.com/requestGithubOAuthToken"
  295. }
  296. }'
  297. ```
  298. - `ClientSecret`: Your Github application client secret
  299. - `ClientID`: Your Github application client ID
  300. - `StarterLink`: Optional. If supplied, `!github` commands will return this link whenever someone is prompted to login to Github.
  301. #### Github authentication
  302. Once you have configured a Github realm, you can associate any Matrix user ID with any Github user. To do this:
  303. ```bash
  304. curl -X POST localhost:4050/admin/requestAuthSession --data-binary '{
  305. "RealmID": "mygithubrealm",
  306. "UserID": "@real_matrix_user:localhost",
  307. "Config": {
  308. "RedirectURL": "https://optional-url.com/to/redirect/to/after/auth"
  309. }
  310. }'
  311. ```
  312. - `UserID`: The Matrix user ID to associate with.
  313. - `RedirectURL`: Optional. The URL to redirect to after authentication.
  314. This request will return an OAuth URL:
  315. ```json
  316. {
  317. "URL": "https://github.com/login/oauth/authorize?client_id=abcdef&client_secret=acascacac...."
  318. }
  319. ```
  320. Follow this link to associate this user ID with this Github account. Once this is complete, Go-NEB will have an OAuth token for this user ID and will be able to create issues as their real Github account.
  321. To remove this session:
  322. ```bash
  323. curl -X POST localhost:4050/admin/removeAuthSession --data-binary '{
  324. "RealmID": "mygithubrealm",
  325. "UserID": "@real_matrix_user:localhost",
  326. "Config": {}
  327. }'
  328. ```
  329. ### JIRA Realm
  330. This has the `Type` of `jira`. To set up this realm:
  331. ```bash
  332. curl -X POST localhost:4050/admin/configureAuthRealm --data-binary '{
  333. "ID": "jirarealm",
  334. "Type": "jira",
  335. "Config": {
  336. "JIRAEndpoint": "matrix.org/jira/",
  337. "ConsumerName": "goneb",
  338. "ConsumerKey": "goneb",
  339. "ConsumerSecret": "random_long_string",
  340. "PrivateKeyPEM": "-----BEGIN RSA PRIVATE KEY-----\r\nMIIEowIBAAKCAQEA39UhbOvQHEkBP9fGnhU+eSObTWBDGWygVYzbcONOlqEOTJUN\r\n8gmnellWqJO45S4jB1vLLnuXiHqEWnmaShIvbUem3QnDDqghu0gfqXHMlQr5R8ZP\r\norTt1F2idWy1wk5rVXeLKSG7uriYhDVOVS69WuefoW5v55b5YZV283v2jROjxHuj\r\ngAsJA7k6tvpYiSXApUl6YHmECfBoiwG9bwItkHwhZ\/fG9i4H8\/aOyr3WlaWbVeKX\r\n+m38lmYZvzQFRAk5ab1vzCGz4cyc\r\nTk2qmZpcjHRd1ijcOkgC23KF8lHWF5Zx0tySR+DWL1JeGm8NJxKMRJZuE8MIkJYF\r\nryE7kjspNItk6npkA3\/A4PWwElhddI4JpiuK+29mMNipRcYYy9e0vH\/igejv7ayd\r\nPLCRMQKBgBDSNWlZT0nNd2DXVqTW9p+MG72VKhDgmEwFB1acOw0lpu1XE8R1wmwG\r\nZRl\/xzri3LOW2Gpc77xu6fs3NIkzQw3v1ifYhX3OrVsCIRBbDjPQI3yYjkhGx24s\r\nVhhZ5S\/TkGk3Kw59bDC6KGqAuQAwX9req2l1NiuNaPU9rE7tf6Bk\r\n-----END RSA PRIVATE KEY-----"
  341. }
  342. }'
  343. ```
  344. - `JIRAEndpoint`: The base URL of the JIRA installation you wish to talk to.
  345. - `ConsumerName`: The desired "Consumer Name" field of the "Application Links" admin page on JIRA. Generally this is the name of the service. Users will need to enter this string into their JIRA admin web form.
  346. - `ConsumerKey`: The desired "Consumer Key" field of the "Application Links" admin page on JIRA. Generally this is the name of the service. Users will need to enter this string into their JIRA admin web form.
  347. - `ConsumerSecret`: The desired "Consumer Secret" field of the "Application Links" admin page on JIRA. This should be a random long string. Users will need to enter this string into their JIRA admin web form.
  348. - `PrivateKeyPEM`: A string which contains the private key for performing OAuth 1.0 requests. This MUST be in PEM format. It must NOT have a password. Go-NEB will convert this into a **public** key in PEM format and return this to users. Users will need to enter the public key into their JIRA admin web form.
  349. - `StarterLink`: Optional. If supplied, `!jira` commands will return this link whenever someone is prompted to login to JIRA.
  350. To generate a private key PEM: (JIRA does not support bit lengths >2048)
  351. ```bash
  352. openssl genrsa -out privkey.pem 2048
  353. cat privkey.pem
  354. ```
  355. #### JIRA authentication
  356. ```
  357. curl -X POST localhost:4050/admin/requestAuthSession --data-binary '{
  358. "RealmID": "jirarealm",
  359. "UserID": "@example:localhost",
  360. "Config": {
  361. "RedirectURL": "https://optional-url.com/to/redirect/to/after/auth"
  362. }
  363. }'
  364. ```
  365. Returns:
  366. ```json
  367. {
  368. "URL":"https://jira.somewhere.com/plugins/servlet/oauth/authorize?oauth_token=7yeuierbgweguiegrTbOT"
  369. }
  370. ```
  371. # Developing
  372. There's a bunch more tools this project uses when developing in order to do
  373. things like linting. Some of them are bundled with go (fmt and vet) but some
  374. are not. You should install the ones which are not:
  375. ```bash
  376. go get github.com/golang/lint/golint
  377. go get github.com/fzipp/gocyclo
  378. ```
  379. You can then install the pre-commit hook:
  380. ```bash
  381. ./hooks/install.sh
  382. ```
  383. ## Architecture
  384. ```
  385. HOMESERVER
  386. |
  387. +=============================================================+
  388. | | Go-NEB |
  389. | +---------+ |
  390. | | Clients | |
  391. | +---------+ |
  392. | | |
  393. | +---------+ +------------+ +--------------+ |
  394. | | Service |-------| Auth Realm |------| Auth Session |-+ |
  395. | +---------+ +------------+ +--------------+ | |
  396. | ^ ^ +---------------+ |
  397. | | | |
  398. +=============================================================+
  399. | |
  400. WEBHOOK REDIRECT
  401. REQUEST REQUEST
  402. Clients = A thing which can talk to homeservers and listen for events. /configureClient makes these.
  403. Service = An individual bot, configured by a user. /configureService makes these.
  404. Auth Realm = A place where a user can authenticate with. /configureAuthRealm makes these.
  405. Auth Session = An individual authentication session /requestAuthSession makes these.
  406. ```
  407. ## Viewing the API docs
  408. ```
  409. # Start a documentation server listening on :6060
  410. GOPATH=$GOPATH:$(pwd) godoc -v -http=localhost:6060 &
  411. # Open up the documentation for go-neb in a browser.
  412. sensible-browser http://localhost:6060/pkg/github.com/matrix-org/go-neb
  413. ```