Skip to content

Commit

Permalink
Merge pull request #23 from urbanriskmap/dev
Browse files Browse the repository at this point in the history
Merge dev to form next release
  • Loading branch information
tomasholderness authored Jul 10, 2018
2 parents 85dcc4b + ed8ea77 commit 9b2c970
Show file tree
Hide file tree
Showing 19 changed files with 422 additions and 17 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ env.yml
bin/
.nyc_output/
private/
.env
.env-dev
.env*
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ v0.0.4
- improved error handling in Lambda functions
- added unit tests for twitter.js
- updated node modules
- updated README
- updated README
- add additional setup command scripts
- add user locale support
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](http://www.gnu.org/licenses/gpl-3.0) [![Build Status](https://travis-ci.org/urbanriskmap/cognicity-reports-twitter-dm.svg?branch=master)](https://travis-ci.org/urbanriskmap/cognicity-reports-twitter-dm) [![Coverage Status](https://coveralls.io/repos/github/urbanriskmap/cognicity-reports-twitter-dm/badge.svg?branch=master)](https://coveralls.io/github/urbanriskmap/cognicity-reports-twitter-dm?branch=master)
[![Build Status](https://travis-ci.org/urbanriskmap/cognicity-reports-twitter-dm.svg?branch=master)](https://travis-ci.org/urbanriskmap/cognicity-reports-twitter-dm) [![Coverage Status](https://coveralls.io/repos/github/urbanriskmap/cognicity-reports-twitter-dm/badge.svg?branch=master)](https://coveralls.io/github/urbanriskmap/cognicity-reports-twitter-dm?branch=master) [![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](http://www.gnu.org/licenses/gpl-3.0)

## cognicity-reports-twitter-dm

Expand Down Expand Up @@ -34,6 +34,7 @@ Configuration variables are as follows (these should be set in the Lambda enviro
* `CARDS_API` - the endpoint to get new report cards
* `CARDS_API_KEY` - the api key for the cards endpoint
* `CARDS_URL` - the URL for the card resources to be sent to the user
* `DEFAULT_INSTANCE_COUNTRY_CODE` - default country code for deployment (e.g. 'us')
* `DEFAULT_INSTANCE_REGION_CODE` - in case a report is submitted outside the city, the code that the bot should fall back on for geographic reference
* `DEFAULT_LANGUAGE` - default language for user interactions
* `MAP_URL` - the risk map URL
Expand Down
38 changes: 38 additions & 0 deletions commands/add-welcome-message-rule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Add a webhook
// npx babel-node commands/add-welcome-message.js
import config from '../src/config';
import request from 'request';

const ENV = 'production'

const run = function(){
console.log('running');
const oauth = {
consumer_key: config.TWITTER_CONSUMER_KEY,
consumer_secret: config.TWITTER_CONSUMER_SECRET,
token: config.TWITTER_TOKEN,
token_secret: config.TWITTER_TOKEN_SECRET,
}

console.log(oauth);

const opts = {
url: config.TWITTER_ENDPOINT + 'direct_messages/welcome_messages/rules/new.json',
oauth: oauth,
json: true,
headers: {
'content-type': 'application/json',
},
body: {
"welcome_message_rule" : {
"welcome_message_id": '<INSERT RULE ID HERE>',
},
},
}
request.post(opts, function(err, response, body){
console.log('returned')
console.log(err);
console.log(body);
});
}
run();
51 changes: 51 additions & 0 deletions commands/add-welcome-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Add a webhook
// npx babel-node commands/add-welcome-message.js
import config from '../src/config';
import buttons from '../src/lib/buttons.json';
import request from 'request';

const ENV = 'production'

const run = function(){
console.log('running');
const oauth = {
consumer_key: config.TWITTER_CONSUMER_KEY,
consumer_secret: config.TWITTER_CONSUMER_SECRET,
token: config.TWITTER_TOKEN,
token_secret: config.TWITTER_TOKEN_SECRET,
}

console.log(oauth);

const opts = {
url: config.TWITTER_ENDPOINT + 'direct_messages/welcome_messages/new',
oauth: oauth,
json: true,
headers: {
'content-type': 'application/json',
},
body: {
"welcome_message" : {
"name": "simple_welcome-message 03",
"message_data": {
"text": buttons[config.DEFAULT_LANGUAGE].text.welcome,
"quick_reply": {
"type": "options",
"options": [
{
"label": buttons[config.DEFAULT_LANGUAGE].text.start,
"metadata": "start",
},
]
}
}
}
}
}
request.post(opts, function(err, response, body){
console.log('returned')
console.log(err);
console.log(body);
});
}
run();
35 changes: 35 additions & 0 deletions commands/delete-subscriptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Delete a subscription
// npx babel-node commands/delete-subscription.js
import config from '../src/config';
import request from 'request';

const ENV = 'production'

const run = function(){
console.log('running');
const oauth = {
consumer_key: config.TWITTER_CONSUMER_KEY,
consumer_secret: config.TWITTER_CONSUMER_SECRET,
token: config.TWITTER_TOKEN,
token_secret: config.TWITTER_TOKEN_SECRET,
}

console.log(oauth);

const opts = {
url: config.TWITTER_ENDPOINT + 'account_activity/all/' + ENV + '/subscriptions.json',
oauth: oauth,
json: true,
headers: {
'content-type': 'application/json',
},
body: {}
}

request.delete(opts, function(err, response, body){
console.log('returned')
console.log(err);
console.log(body);
});
}
run();
33 changes: 33 additions & 0 deletions commands/delete-welcome-message-rule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Delete a webhook message rule
// npx babel-node commands/delete-welcome-message-rule.js
import config from '../src/config';
import request from 'request';

const ENV = 'production'

const run = function(){
console.log('running');
const oauth = {
consumer_key: config.TWITTER_CONSUMER_KEY,
consumer_secret: config.TWITTER_CONSUMER_SECRET,
token: config.TWITTER_TOKEN,
token_secret: config.TWITTER_TOKEN_SECRET,
}

console.log(oauth);

const opts = {
url: config.TWITTER_ENDPOINT + 'direct_messages/welcome_messages/rules/destroy.json?id=<INSERT RULE ID HERE>',
oauth: oauth,
json: true,
headers: {
'content-type': 'application/json',
},
}
request.delete(opts, function(err, response, body){
console.log('returned')
console.log(err);
console.log(body);
});
}
run();
36 changes: 36 additions & 0 deletions commands/list-subscriptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Add a subscription
// npx babel-node commands/delete-subscription.js
import config from '../src/config';
import request from 'request';

const ENV = 'production'

const run = function(){
console.log('running');
const oauth = {
consumer_key: config.TWITTER_CONSUMER_KEY,
consumer_secret: config.TWITTER_CONSUMER_SECRET,
token: config.TWITTER_TOKEN,
token_secret: config.TWITTER_TOKEN_SECRET,
}

console.log(oauth);

const opts = {
url: config.TWITTER_ENDPOINT + 'account_activity/all/' + ENV + '/subscriptions.json',
oauth: oauth,
json: true,
headers: {
'content-type': 'application/json',
},
body: {}
}

request.get(opts, function(err, response, body){
console.log('returned')
console.log(response.statusCode);
console.log(err);
console.log(body);
});
}
run();
33 changes: 33 additions & 0 deletions commands/list-welcome-message-rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Add a webhook
// npx babel-node commands/add-welcome-message.js
import config from '../src/config';
import request from 'request';

const ENV = 'production'

const run = function(){
console.log('running');
const oauth = {
consumer_key: config.TWITTER_CONSUMER_KEY,
consumer_secret: config.TWITTER_CONSUMER_SECRET,
token: config.TWITTER_TOKEN,
token_secret: config.TWITTER_TOKEN_SECRET,
}

console.log(oauth);

const opts = {
url: config.TWITTER_ENDPOINT + 'direct_messages/welcome_messages/rules/list',
oauth: oauth,
json: true,
headers: {
'content-type': 'application/json',
},
}
request.get(opts, function(err, response, body){
console.log('returned')
console.log(err);
console.log(body);
});
}
run();
3 changes: 3 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable max-len */

require('dotenv').config({silent: true});

export default {
Expand All @@ -6,6 +8,7 @@ export default {
CARDS_API: process.env.CARDS_API || 'https://data.riskmap.us/cards/',
CARDS_API_KEY: process.env.CARDS_API_KEY,
CARDS_URL: process.env.CARDS_URL || 'https://cards.riskmap.us/flood/',
DEFAULT_INSTANCE_COUNTRY_CODE: process.env.DEFAULT_INSTANCE_COUNTRY_CODE || 'us',
DEFAULT_INSTANCE_REGION_CODE: process.env.DEFAULT_INSTANCE_REGION_CODE ||
'brw',
DEFAULT_LANGUAGE: process.env.DEFAULT_LANGUAGE || 'en',
Expand Down
22 changes: 16 additions & 6 deletions src/lib/buttons.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@
},
"es": {
"text": {
"welcome": "[ES} Welcome to RiskMap bot. Click 'Get Started' or send me a message.",
"start": "[ES] Get Started",
"report": "[ES] Report flooding",
"map": "[ES] View live reports",
"view": "[ES] View your report",
"add": "[ES] Add another report"
"welcome": "Bienvenido a RiskMap bot. Haga clic en 'Comenzar' o envíeme un mensaje.",
"start": "> Comienzo",
"report": "Inportar inundaciones",
"map": "Ver el mapa de inundación en vivo",
"view": "Ver su informe",
"add": "Agregar otro informe"
}
},
"in": {
"text": {
"welcome": "Welcome to RiskMap bot. Click 'Get Started' or send me a message.",
"start": "> Get Started",
"report": "Report flooding",
"map": "View live flood map",
"view": "View your report",
"add": "Add another report"
}
}
}
60 changes: 60 additions & 0 deletions src/lib/locale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import request from 'request';

// Define global
let DEFAULT_LANGUAGE = 'en';

/**
* Class for getting Twitter user's locale
* @class Locale
* @param {Object} config - module configuration (config.js)
*/
export default class Locale {
/**
* constructor for class Locale
* @param {Object} config - module configuration (config.js)
*/
constructor(config) {
this.config = config;
this.request = request;

// Update the global with the config
DEFAULT_LANGUAGE = this.config.DEFAULT_LANGUAGE;
}

/**
* Get a user's locale by user ID
* @method get
* @param {String} userId - Twitter user ID
* @return {Promise} - response
*/
get(userId) {
return new Promise((resolve, reject) => {
const oauth = {
consumer_key: this.config.TWITTER_CONSUMER_KEY,
consumer_secret: this.config.TWITTER_CONSUMER_SECRET,
token: this.config.TWITTER_TOKEN,
token_secret: this.config.TWITTER_TOKEN_SECRET,
};
const opts = {
url: this.config.TWITTER_ENDPOINT +
'users/show.json?include_entities=false&user_id=' +
String(userId),
oauth: oauth,
json: true,
headers: {
'content-type': 'application/json',
},
};

request.get(opts, function(err, response, body) {
if (err) {
console.log('Error getting user locale. ' +
err.message);
resolve(DEFAULT_LANGUAGE); // Acess the global default
} else {
resolve(body.lang);
}
});
});
}
}
File renamed without changes.
16 changes: 16 additions & 0 deletions src/lib/messages-in.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"en": {
"texts": {
"default": "RiskMap bot helps you report flooding in realtime. In life-threatening situations call 911.",
"card": "RiskMap bot helps you report flooding in realtime. In life-threatening situations call 911.",
"thanks": "Thank you for your report. Click to see your report on the map."
}
},
"es": {
"texts": {
"default": "[ES] RiskMap bot helps you report flooding in realtime. In life-threatening situations call 911.",
"card": "[ES] RiskMap bot helps you report flooding in realtime. In life-threatening situations call 911.",
"thanks": "[ES] Thank you for your report. Click to see your report on the map."
}
}
}
Loading

0 comments on commit 9b2c970

Please sign in to comment.