Skip to content

Commit

Permalink
prep cards
Browse files Browse the repository at this point in the history
  • Loading branch information
abrahamq committed Sep 26, 2018
1 parent 19d2f87 commit ff69a70
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ jspm_packages
bin
private

# Vim
*.swp
*.swo
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"url": "https://github.com/urbanriskmap/cognicity-reports-telegram-lambda/issues"
},
"dependencies": {
"@urbanriskmap/cognicity-bot-core": "1.0.5",
"@urbanriskmap/cognicity-bot-core": "1.1.3",
"axios": "^0.18.0",
"dotenv": "^6.0.0",
"joi": "^13.4.0",
Expand Down
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default {
BOT_TOKEN: process.env.BOT_TOKEN,
CARDS_API: process.env.CARDS_API || 'https://data.riskmap.us/cards/',
CARDS_API_KEY: process.env.CARDS_API_KEY,
CARDS_DECK: (process.env.CARDS_DECK || 'flood').split(','),
CARDS_URL: process.env.CARDS_URL || 'https://cards.riskmap.us/flood/',
DEFAULT_LANGUAGE: process.env.DEFAULT_LANGUAGE || 'en',
DEFAULT_INSTANCE_COUNTRY_CODE: process.env.DEFAULT_INSTANCE_COUNTRY_CODE || 'us',
Expand Down
4 changes: 2 additions & 2 deletions src/lib/messages-in.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"en": {
"texts": {
"default": "RiskMap bot helps you report flooding in realtime. Send /flood to report. In life-threatening situations contact emergency services.",
"default": "RiskMap bot helps you report flooding in realtime. Send /flood to report flooding. Send /disruption to report infrastructure failure. In life-threatening situations contact emergency services.",
"card": "Please report using this one-time link ",
"thanks": "Thank you for your report. You can access it using this link "
}
}
}
}
32 changes: 26 additions & 6 deletions src/lib/telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ export default class Telegram {
*/
_classify(text) {
// filter the message by keyword
const re = new RegExp(/\/flood/gi);
if (re.exec(text) !== null) {
const reFlood = new RegExp(/\/flood/gi);
const rePrep = new RegExp(/\/disruption/gi);
if (reFlood.exec(text) !== null) {
return 'flood';
} else if (rePrep.exec(text)) {
return 'prep';
} else {
return null;
}
Expand All @@ -44,14 +47,19 @@ export default class Telegram {
* @private
* @param {String} userId - User or Telegram chat ID for reply
* @param {Object} message - Bot message object
* @param {String} disasterType - one of 'prep' or 'flood'
* @return {String} - URI for request
**/
_prepareLinkResponse(userId, message) {
_prepareLinkResponse(userId, message, disasterType) {
let customLink = message.link;
if (disasterType === 'prep') {
customLink = message.prepLink;
}
return (this.config.TELEGRAM_ENDPOINT +
this.config.BOT_TOKEN +
'/sendmessage?text=' +
message.text +
message.link +
customLink +
'&chat_id=' +
userId);
}
Expand Down Expand Up @@ -105,7 +113,8 @@ export default class Telegram {
}
this.bot.thanks(body)
.then((msg) => {
const response = this._prepareLinkResponse(body.userId, msg);
const response =
this._prepareLinkResponse(body.userId, msg, 'flood');
resolve(this._sendMessage(response));
}).catch((err) => reject(err));
});
Expand All @@ -126,10 +135,21 @@ export default class Telegram {
network: 'telegram',
};
if (this._classify(telegramMessage.text) === 'flood') {
// user requested flood card
properties.language = 'en'; // user speaks English
this.bot.card(properties)
.then((msg) => {
const response = this.
_prepareLinkResponse(properties.userId, msg, 'flood');
resolve(this._sendMessage(response));
}).catch((err) => reject(err));
} else if (this._classify(telegramMessage.text) === 'prep') {
// user requested prep card
properties.language = 'en'; // user speaks English
this.bot.card(properties)
.then((msg) => {
const response = this._prepareLinkResponse(properties.userId, msg);
const response = this.
_prepareLinkResponse(properties.userId, msg, 'prep');
resolve(this._sendMessage(response));
}).catch((err) => reject(err));
} else {
Expand Down
24 changes: 23 additions & 1 deletion src/test/testTelegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ export default function() {
const mockCard = function(properties) {
return new Promise((resolve, reject) => {
if (botError === false) {
resolve({text: 'mocked card message', link: ' card'});
resolve({
text: 'mocked card message',
link: ' card',
prepLink: ' prepLink',
});
} else {
reject(new Error(`bot error`));
}
Expand Down Expand Up @@ -132,6 +136,23 @@ export default function() {
});
});

it('Can get prep messsage', function(done) {
const message = {
from: {
language_code: 'en-US',
},
chat: {
id: 1,
},
text: '/disruption',
};
telegram.sendReply(message)
.then((res) => {
test.value(res).is('https://api.telegram.org/bot' + config.BOT_TOKEN + '/sendmessage?text=mocked card message prepLink&chat_id=1');
done();
});
});

it('Can handle axios error geting card message', function(done) {
const message = {
from: {
Expand Down Expand Up @@ -168,6 +189,7 @@ export default function() {
});
});


it('Can catch error getting default messsage', function(done) {
const message = {
from: {
Expand Down

0 comments on commit ff69a70

Please sign in to comment.