mirror of
https://github.com/zhigang1992/probot.git
synced 2026-06-15 02:18:58 +08:00
51 lines
2.1 KiB
Markdown
51 lines
2.1 KiB
Markdown
---
|
|
next: docs/development.md
|
|
---
|
|
|
|
# Hello World
|
|
|
|
A Probot app is just a [Node.js module](https://nodejs.org/api/modules.html) that exports a function:
|
|
|
|
```js
|
|
module.exports = app => {
|
|
// your code here
|
|
}
|
|
```
|
|
|
|
The `app` parameter is an instance of [`Application`](https://probot.github.io/api/latest/classes/application.html) and gives you access to all of the GitHub goodness.
|
|
|
|
`app.on` will listen for any [webhook events triggered by GitHub](./webhooks.md), which will notify you when anything interesting happens on GitHub that your app wants to know about.
|
|
|
|
```js
|
|
module.exports = app => {
|
|
app.on('issues.opened', async context => {
|
|
// A new issue was opened, what should we do with it?
|
|
context.log(context.payload)
|
|
})
|
|
}
|
|
```
|
|
|
|
The `context` passed to the event handler includes everything about the event that was triggered, as well as some helpful properties for doing something useful in response to the event. `context.github` is an authenticated GitHub client that can be used to [make API calls](./github-api.md), and allows you to do almost anything programmatically that you can do through a web browser on GitHub.
|
|
|
|
Here is an example of an autoresponder app that comments on opened issues:
|
|
|
|
```js
|
|
module.exports = app => {
|
|
app.on('issues.opened', async context => {
|
|
// `context` extracts information from the event, which can be passed to
|
|
// GitHub API calls. This will return:
|
|
// {owner: 'yourname', repo: 'yourrepo', number: 123, body: 'Hello World!}
|
|
const params = context.issue({body: 'Hello World!'})
|
|
|
|
// Post a comment on the issue
|
|
return context.github.issues.createComment(params)
|
|
})
|
|
}
|
|
```
|
|
|
|
To get started, you can use the instructions for [Developing an App](https://probot.github.io/docs/development/) or remix this 'Hello World' project on Glitch:
|
|
|
|
[](https://glitch.com/edit/#!/remix/probot-hello-world)
|
|
|
|
Don't know what to build? Browse the [list of ideas](https://github.com/probot/ideas/issues) from the community for inspiration.
|