@18f/trello-webhook-server 中文文档教程
trello-webhook-server
< href="https://david-dm.org/18F/trello-webhook-server">
创建一个 Trello webhook 服务器,它掩盖了大部分(公认的适度)复杂性,所以你可以做你想做的事。 有关 Trello webhook 的更多详细信息,请参阅 Trello webhook API 文档。
除了提供独立模式(模块创建自己的 HTTP 服务器)之外,还可以附加到现有的 http.server 或 Express/restify-style 服务器。
Installing
来自 npm:
npm install --save @18f/trello-webhook-server
Using
Creating an instance
创建 webhook 服务器的实例:
var TrelloWebhookServer = require('@18f/trello-webhook-server');
var server = new TrelloWebhookServer(config);
config 参数具有以下属性:
Argument | Description |
---|---|
port | The port that the webhook server will listen on. REQUIRED if the server property is not set. Must be numeric (or can be made numeric) and be a valid valid. |
server | An http.Server or Express/restify server to attach to. Must be an http.Server or Express/restify server object. |
hostURL | REQUIRED The URL that will eventually reach the webhook server. This should be a full HTTP URL that is reachable by Trello. E.g., https://asdf.localtunnel.me. If attaching to an existing http.Server or Express/restify server, the webhook server will only listen to its assigned path - that is, if you specify https://asdf.localtunnel.me/trello-webhook , this module will only listen to events at that path. |
apiKey | REQUIRED Obtained from Trello. Located near the top of that page. |
apiToken | REQUIRED Obtained from Trello. Located near the bottom of that page. |
clientSecret | REQUIRED Obtained from Trello. There's a link to generate the key at the end of the first paragraph headed "Token." This is used to verify that webhook requests are actually from Trello (see the "Webhook Signatures" section on the Trello webhook API documentation). |
任何无效的配置参数都将引发异常。
Getting it started
在您选择要收听的模型之前,Webhook 侦听器不会向 Trello 注册。 为此,请使用您想要通知的事物的 ID 调用 start
方法。 如果您已经有一个 http.Server 对象并将其传递给构造函数,请确保在调用 start
之前它正在侦听。
server.start('trello-model-id');
如果一切顺利(即 HTTP 服务器启动并且 Trello 接受 webhook 注册),这将返回最终使用 Trello webhook ID 解析的承诺。 如果有任何问题,承诺将拒绝。
注意:最好将网络书 ID 打印在您可以看到的地方。 如果出现问题,您可能需要手动删除它。 这可以通过对 https://api.trello 的
DELETE
请求来完成.com/1/webhooks/your-webhook-id/。
start
方法内部发生了一些神奇的变化:它将自身挂接到进程 SIGINT
和 SIGTERM
事件。 当它收到其中任何一个时,它会从 Trello 中注销自己,然后重新发送事件。 您可以通过调用 server.cleanup()
手动启动此过程,但目前不推荐这样做,因为 cleanup()
除了取消注册 webbook 之外还没有进行任何清理- HTTP 服务器仍在运行等。
Getting events
注册事件处理程序以获取 webhook 事件。 目前只有一个巨大的 data
事件:
server.on('data', function(event) {
...do stuff...
})
请参阅 Trello webhook API 文档 有关这些事件对象的更多信息。
Demos
检查此存储库的 /demo
目录以获取有关如何开始的示例。 请注意,为了运行演示,您需要手动安装 dotenv 和 restify 因为它们不是该项目的依赖项。 使用 dotenv
允许您将环境变量放在 .env
文件中,并在运行时将它们加载到您的 process.environment
中。
Public domain
该项目属于全球公共领域。 如 CONTRIBUTING 所述:
该项目在美国属于公共领域,通过 CC0 1.0 通用公共领域贡献。
所有对该项目的贡献都将在 CC0 奉献精神下发布。 提交拉取请求,即表示您同意遵守此版权权益弃权书。
trello-webhook-server
Creates a Trello webhook server that masks most of the (admittedly modest) complexity, so you can just do you what you want to do. For more details about Trello webhooks, see the Trello webhook API documentation.
In addition to offering a standalone mode, where the module creates its own HTTP server, can also attach to an existing http.server or Express/restify-style server.
Installing
From npm:
npm install --save @18f/trello-webhook-server
Using
Creating an instance
To create an instance of the webhook server:
var TrelloWebhookServer = require('@18f/trello-webhook-server');
var server = new TrelloWebhookServer(config);
The config parameter has the following properties:
Argument | Description |
---|---|
port | The port that the webhook server will listen on. REQUIRED if the server property is not set. Must be numeric (or can be made numeric) and be a valid valid. |
server | An http.Server or Express/restify server to attach to. Must be an http.Server or Express/restify server object. |
hostURL | REQUIRED The URL that will eventually reach the webhook server. This should be a full HTTP URL that is reachable by Trello. E.g., https://asdf.localtunnel.me. If attaching to an existing http.Server or Express/restify server, the webhook server will only listen to its assigned path - that is, if you specify https://asdf.localtunnel.me/trello-webhook , this module will only listen to events at that path. |
apiKey | REQUIRED Obtained from Trello. Located near the top of that page. |
apiToken | REQUIRED Obtained from Trello. Located near the bottom of that page. |
clientSecret | REQUIRED Obtained from Trello. There's a link to generate the key at the end of the first paragraph headed "Token." This is used to verify that webhook requests are actually from Trello (see the "Webhook Signatures" section on the Trello webhook API documentation). |
Any invalid config parameters will throw an exception.
Getting it started
The webhook listener doesn't register itself with Trello until you pick a model to listen to. To do that, call the start
method with the ID of the thing you want notifications for. If you already have an http.Server object and passed that into the constructor, make sure it is listening before you call start
.
server.start('trello-model-id');
This will return a promise that eventually resolves with the Trello webhook ID if everything goes well (i.e., the HTTP server starts and Trello accepts the webhook registration). If there are any problems, the promise will reject.
NOTE: It would be a good idea to print the webook ID somewhere you can see it. If something goes haywire, you may need to manually delete it. This can be accomplished with a
DELETE
request to https://api.trello.com/1/webhooks/your-webhook-id/.
There's some hand-wavy magic happening inside the start
method: it hooks itself to the process SIGINT
and SIGTERM
events. When it receives either of those, it unregisters itself from Trello and then resends the event. You can manually initiate this process by calling server.cleanup()
, but that's not recommended at this time because cleanup()
doesn't yet do any cleanup besides unregistering the webook - the HTTP server is still running, etc.
Getting events
Register an event handler to get webhook events. Currently there's only one giant data
event:
server.on('data', function(event) {
...do stuff...
})
See the "Trigger Webhooks" section of the Trello webhook API documentation for more information about what these event objects look like.
Demos
Check the /demo
directory of this repository for samples of how to get going. Note that in order to run the demos, you will need to manually install dotenv and restify as they are not dependencies of this project. Using dotenv
allows you to put your environment variables in a .env
file and have them loaded into your process.environment
at runtime.
Public domain
This project is in the worldwide public domain. As stated in CONTRIBUTING:
This project is in the public domain within the United States, and copyright and related rights in the work worldwide are waived through the CC0 1.0 Universal public domain dedication.
All contributions to this project will be released under the CC0 dedication. By submitting a pull request, you are agreeing to comply with this waiver of copyright interest.