Skip to content

Commit

Permalink
[client] Add new fanciness: Openshift WebHook
Browse files Browse the repository at this point in the history
Feature:

    Please read the README.md for documentation on this
    feature.
  • Loading branch information
GochoMugo committed Nov 15, 2016
1 parent 9ab20ad commit 56f6e41
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,7 +6,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

Added:

* New fanciness: Openshift WebHook


## 0.4.0 - 2016-11-10
Expand Down
42 changes: 39 additions & 3 deletions README.md
Expand Up @@ -53,9 +53,9 @@ constructor.
> Here comes the fanciness
<a name="feature-toggle"></a>
All of the features below are **enabled by default**. However, you may want to
disable some of them. This can be done passing a corresponding option to
the constructor. For example, disabling
Most of the features below are **enabled by default**. However, you may
want to disable some of them. This can be done passing a corresponding
option to the constructor. For example, disabling
[Chat ID Resolution](#chat-id-resolution):

```js
Expand All @@ -68,12 +68,26 @@ const bot = new Tgfancy(token, {

See example at `example/feature-toggled.js`.

<a name="feature-enable"></a>
To enable the rest of the features, you need to set the corresponding
*feature toggle option*. For example, enabling
[Openshift WebHook](#openshift-webhook):

```js
const bot = new Tgfancy(token, {
tgfancy: {
openshiftWebHook: true, // 'true' to enable!
},
});
```

**tgfancy** adds the following fanciness:

* [Ordered Sending](#ordered-sending)
* [Text Paging](#text-paging)
* [Chat ID Resolution](#chat-id-resolution)
* [Kick-without-Ban](#kick-without-ban)
* [Openshift WebHook](#openshift-webhook)


<a name="ordered-sending"></a>
Expand Down Expand Up @@ -215,6 +229,28 @@ bot.kickChatMember(chatId, userId, false);
See example at `example/kick-only.js`.


<a name="openshift-webhook"></a>
### Openshift WebHook:

It is easier to set up webhook for your bot on [Openshift][openshift].
Enabling this feature allows **automatic detection if running on Openshift**
and setting up webhook for the bot instance.

**Feature enable option:** `openshiftWebHook` (see [above](#feature-enable))

For example,

```js
const bot = Tgfancy(token, {
tgfancy: {
openshiftWebHook: true, // enable this feature
},
});
```

[openshift]:https://openshift.com


## license:

**The MIT License (MIT)**
Expand Down
35 changes: 31 additions & 4 deletions lib/client.js
Expand Up @@ -9,6 +9,7 @@
* 1. Use of queue to send messages was first proposed at
* https://github.com/yagop/node-telegram-bot-api/issues/192#issuecomment-249488807
* 2. Kick-without-Ban was suggested to me by @kamikazechaser
* 3. Openshift Webhook was inspired by the example at https://github.com/yagop/node-telegram-bot-api/blob/master/examples/openShiftWebHook.js
*/


Expand Down Expand Up @@ -65,17 +66,37 @@ exports = module.exports = class Tgfancy extends TelegramBot {
* @param {Function} [options.resolveChatId]
*/
constructor(token, options={}) {
super(token, options);
const self = this;
this.token = token;
this.options = _.defaultsDeep({}, options.tgfancy, {
const opts = _.defaultsDeep({
_openshift: false,
_openshiftDomain: undefined,
}, options.tgfancy, {
orderedSending: true,
textPaging: true,
chatIdResolution: true,
kickWithoutBan: true,
openshiftWebHook: false,
resolveChatId: tgresolve,
});

// setting up Openshift webhook, if we are actually
// on Openshift. We are using an informational environment
// variable to check if we are on Openshift.
// See https://developers.openshift.com/managing-your-applications/environment-variables.html#informational-variables
// and https://developers.openshift.com/languages/nodejs/environment-variables.html
if (opts.openshiftWebHook && process.env.OPENSHIFT_APP_UUID) {
opts._openshift = true;
opts._openshiftDomain = `${process.env.OPENSHIFT_APP_DNS}:443/bot${token}`;
opts.webHook = {
host: process.env.OPENSHIFT_NODEJS_IP,
port: process.env.OPENSHIFT_NODEJS_PORT,
};
}

super(token, opts);
this.token = token;
this.options = opts;
const self = this;

// promisify function we'll be using
this.options.resolveChatId = Promise.promisify(this.options.resolveChatId);

Expand Down Expand Up @@ -112,6 +133,12 @@ exports = module.exports = class Tgfancy extends TelegramBot {
self[methodName] = self._resolveChatId(self[methodName], methodDesc[1]);
});
}

// We are now performing 'on ready' actions.
// Let's set the correct webhook when on Openshift
if (this.options._openshift) {
this.setWebHook(this.options._openshiftDomain);
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -42,6 +42,7 @@
"load-grunt-tasks": "^3.5.2",
"mocha": "^3.1.2",
"mocha-lcov-reporter": "^1.2.0",
"request": "^2.78.0",
"should": "^11.1.1"
}
}
76 changes: 70 additions & 6 deletions test/test.index.js
Expand Up @@ -8,11 +8,13 @@


// npm-installed modules
const _ = require("lodash");
const TelegramBot = require("node-telegram-bot-api");
const request = require("request");
const should = require("should");


// own modules
const TelegramBot = require("node-telegram-bot-api");
const Tgfancy = require("..");


Expand All @@ -33,12 +35,21 @@ if (!userid) {
console.error("Error: Telegram user ID is required");
process.exit(1);
}
const client = new Tgfancy(token, {
tgfancy: {
resolveChatId,
},
});
const timeout = 15 * 1000; // 15 secs
let client = createClient();


// construct the client. This is useful when we need
// to re-create the client, particularly when testing openshift webhook
// This allows us to re-use one token for all of our tests.
function createClient(options) {
const opts = _.defaultsDeep({}, options, {
tgfancy: {
resolveChatId,
},
});
return new Tgfancy(token, opts);
}


// We are using a custom resolver function, since we are testing
Expand Down Expand Up @@ -136,4 +147,57 @@ describe("Chat-ID Resolution (using Tgfancy#sendMessage())", function() {
});


describe("Openshift Webhook", function() {
let ip, port;

before(function() {
// this is a dummy URL that does NOT even know wtf is happening
process.env.OPENSHIFT_APP_UUID = 12345;
process.env.OPENSHIFT_APP_DNS = "http://gmugo.in/owh";
process.env.OPENSHIFT_NODEJS_IP = ip = "127.0.0.1";
process.env.OPENSHIFT_NODEJS_PORT = port = 9678;
client = createClient({
tgfancy: {
// enable the openshift-webhook fanciness
openshiftWebHook: true,
},
});
});
after(function() {
delete process.env.OPENSHIFT_APP_UUID;
delete process.env.OPENSHIFT_APP_DNS;
delete process.env.OPENSHIFT_NODEJS_IP;
delete process.env.OPENSHIFT_NODEJS_PORT;
client = createClient();
});
function triggerWebhook() {
request.post({
url: `http://${ip}:${port}/bot${token}`,
body: {
"update_id": 666,
message: {
"message_id": 666,
date: Date.now(),
chat: {
id: 666,
type: "private",
},
text: "Trigger!",
},
},
json: true,
}, function(error) {
should(error).not.be.ok();
});
}

it("receives message", function(done) {
client.once("message", function() {
return done();
});
process.nextTick(triggerWebhook);
});
});


describe.skip("Kick-without-Ban");

0 comments on commit 56f6e41

Please sign in to comment.