Skip to content

Commit

Permalink
Webhook listener in Node.js
Browse files Browse the repository at this point in the history
  • Loading branch information
avivace committed Apr 1, 2017
1 parent 684a18a commit 99fb454
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 12 deletions.
35 changes: 23 additions & 12 deletions README.md
@@ -1,27 +1,38 @@
# RadioGit
A Python Bot, built upon the [python-telegram-bot](https://github.com/python-telegram-bot/python-telegram-bot) wrapper, to broadcast GitHub repository events and activities to Telegram chats.
A Python Bot - built upon the [python-telegram-bot](https://github.com/python-telegram-bot/python-telegram-bot) wrapper and Node.js - to broadcast GitHub repository events and activities to Telegram chats.

Provides public interface, handles multiple users with multiple subscription to repositories.
Every user can add and remove his subscriptions.

Every user can add and remove his subscriptions. Currently, only **commit** events are supported.
A public instance of the bot is online at [@radiogit_bot](https://t.me/radiogit_bot).

## Usage
**Dependencies**: PHP, a web server, python (telegram, sqlite3).
## Overview
- The `webhoook-listener` contains a Node app to listen and record JSON payloads from GitHub;
- The `bot` folder contains the actual Telegram bot, which provides an user interface (allowing personal preferences) and delivers updates;
- In `misc` you'll find some other related code and the old PHP implementation of the webhook listening part (working but **deprecated**).

1. Clone this repo;
2. Set up the webhook listener in your webserver folder, make sure it's accessible and accepting POST requests;
3. Insert your bot token and run the bot with `python bot.py`;
## Install
### Webhook Listener
**Dependencies**: Node.js (Express 4, body-parser)
Edit the port/webserver configuration and run `node index.js` (you may find useful setting up a reverse proxy). Make sure it's accessible and accepting POST requests, the URL will be the *Payload URL* when setting up the actual webhook in GitHub repository settings.

A public instance of the bot is online at [@radiogit_bot](https://t.me/radiogit_bot).
You can test the listener with `curl` with something like this:
```
curl -d @payload.json -H "Content-Type: application/json" PAYLOAD/URL/SOME/POST
```
Assuming `payload.json` is something like [this](https://developer.github.com/v3/activity/events/types/#issuesevent).

If the repository url is https://github.com/RepoOwner/RepoName, send `/sub RepoOwner/RepoName` to set up event subscription. Adding the bot to a group and senting `/sub` commands from it will make events sent to the group.
### The actual bot
**Dependencies**: python3 (telegram, sqlite3).
Insert your bot token and run the bot with `python bot.py`

The `misc` folder contains various other code related to Telegram bots.
## Usage
When everything is ready, talk to the bot.
If the repository url is https://github.com/RepoOwner/RepoName, send `/sub RepoOwner/RepoName` to set up event subscription. Adding the bot to a group and senting `/sub` commands from it will make events sent to the group.

## TODO
- Support other types of events;
- Support other types of events (currently supporting only commit events, as POC);
- Implement *secret* checking;
- Implement webhook listener in something not PHP;
- Conversation Handling;
- Rate Limitation;
- Message Templates;
Expand Down
File renamed without changes.
32 changes: 32 additions & 0 deletions index.js
@@ -0,0 +1,32 @@
var express = require('express')
, bodyParser = require('body-parser');
var app = express();
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('/var/www/html/apps/radiogit/db.sqlite');


app.get('/', function (req, res) {
res.send('RadioGit backend is up and running')
})

app.use(bodyParser.json());
app.post('/test', function(request, response){
var text = request.body.repository.full_name + " - " + request.body.commits[0].author.username + " committed " + request.body.commits[0].message + " " + " " + request.body.commits[0].url;
var query = 'INSERT INTO Payloads VALUES (NULL, "' + request.body.repository.full_name + '","' + text + '")';
response.send("Thank you kind sir");
db.serialize(function() {
db.run(query);
console.log(query);
});


});

app.listen(3000);

// On CTRL+C correctly close the database
process.on('SIGINT', function(){
console.log('Cya');
db.close();
process.exit(0);
});
File renamed without changes.
17 changes: 17 additions & 0 deletions package.json
@@ -0,0 +1,17 @@
{
"name": "radiogit-weblistener",
"version": "0.0.1",
"description": "",
"main": "index.js",
"dependencies": {
"body-parser": "^1.17.1",
"ejs": "^2.5.6",
"express": "^4.15.2"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Antonio Vivace",
"license": "GPL-3.0"
}
31 changes: 31 additions & 0 deletions webhook-listener/index.js
@@ -0,0 +1,31 @@
var express = require('express')
, bodyParser = require('body-parser');
var app = express();
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('/var/www/html/apps/radiogit/db.sqlite');

app.get('/', function (req, res) {
res.send('RadioGit backend is up and running')
})

app.use(bodyParser.json());
app.post('/test', function(request, response){
//console.log(request.body);
var text = request.body.repository.full_name + " - " + request.body.commits[0].author.username + " committed " + request.body.commits[0].message + " " + " " + request.body.commits[0].url;
var query = 'INSERT INTO Payloads VALUES (NULL, "' + request.body.repository.full_name + '","' + text + '")';
response.send("Thank you kind sir");
db.serialize(function() {
db.run(query);
console.log(query);
});


});

app.listen(3000);

process.on('SIGINT', function(){
console.log('Cya');
db.close();
process.exit(0);
});
17 changes: 17 additions & 0 deletions webhook-listener/package.json
@@ -0,0 +1,17 @@
{
"name": "radiogit-weblistener",
"version": "0.0.1",
"description": "",
"main": "index.js",
"dependencies": {
"body-parser": "^1.17.1",
"ejs": "^2.5.6",
"express": "^4.15.2"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Antonio Vivace",
"license": "GPL-3.0"
}

0 comments on commit 99fb454

Please sign in to comment.