Part 1 and Part 2 are already available on medium.

In the previous articles, we have explained what we need to make our own GitHub bot, using libraries from the open source community. Today, I want to share with you a minimalist but fully-functional bot: WelcomeBot!

The sum up what I expect from WelcomeBot:

if a new contributor create a pull request, WelcomeBot welcomes him with a pre-defined comment using the contributor login;

if a new contributor create an issue, WelcomeBot welcomes him with a pre-defined comment using the contributor login;

Very simple and useful right ?

To do our own bot, we have to know two kind of information: the events from GitHub we need to listen and the information we extract from them.

To do your own bot, you have to know two kind of information: the events from GitHub you need to listen and the information you extract from them.

Regarding our bot, and the documentation of GitHub API v3, we will listen two events:

IssuesEvent (with action/type value equals to “opened”)

(with action/type value equals to “opened”) PullRequestEvent (with action/type value equals to “opened”)

We also need to know if a GitHub user have already contributed to the project:

We need to get all commits and issues from this user on the selected projects;

We need his login to customize a little bit the comment sent by the bot;

Finally, we need to be able to comment on a contribution (pull request or issue) but we have already done this feature in a previous article.

How to listen specific events only?

You can do it in PHP and using the GitHub back office, using the “Webhooks” section:

Select only the events you need

Using PHP and the GitHub event parser library, events can be filtered more precisely:

This is how we filter the two events we need for WelcomeBot

This way we ensure to not deal with any others events and we limit the useless calls to the bot. As you may note, we have re-used an improved version of the “event listener” used in Article 1.

How to get information about a GitHub user

We will also re-use and improve the client we have done in Article 2.

Regarding the documentation of KnpLabs GitHub client, we can easily get the information:

Get the commits of a specific branch and then filter by user.

If no commits from the user are found, it’s obviously a new contributor.

If no commits from the user are found, it’s obviously a new contributor. Get all the issues for the project and then filter by user.

If no issues from the user are found, it’s obviously a new issuer.

If no issues from the user are found, it’s obviously a new issuer. Also, retrieve the login user is easy using the GitHub event parser for both a pull request or an issue.

Let’s take a look to the produced code:

Is the GitHub user a new contributor or issuer?

The complete project

We finally setup our own parametersto make this bot works, and configure the webhook url endpoint in GitHub like I did for my testing repository:

Please use a better welcome message

And, as always, this is the complete fully functional project:

The complete source code of WelcomeBot

Consider this code open source: you can use it, play with it and create your owns bots for GitHub!

Thank you for reading, this series of articles is now finished and I was happy to share this information with you. If you want to see a real and complete application of GitHub bot, you can take a look to PrestonBot, the community bot of PrestaShop project.

If you have any question, don’t hesitate to use comments on Medium platform or to contact me on Twitter!