Skip to content

Instantly share code, notes, and snippets.

@tomasholderness
Last active May 6, 2021
Embed
What would you like to do?
PetaBencana Bot Twitter Disaster Filter
/**
* parseRequest - Parse an incoming tweet from PowerTrack activity object to determine disaster type.
* A method of CogniCity PowertrackDataSource()
* @param {object} tweetActivity - Tweet activity object
* @param {boolean} addressed - Whether the tweet was addressed to the chat bot (e.g. @petabencana...)
*/
function parseRequest(tweetActivity, addressed) {
// Extract metadata
var username = tweetActivity.actor.preferredUsername;
var words = tweetActivity.body;
var filter = words.match(/banjir|flood|gempa|earthquake|forestfire|kebakaranhutan|haze/gi);
var language = this._parseLangsFromActivity(tweetActivity)[0];
var disaster = 'default';
if (filter) { filter = filter[0].toLowerCase(); }
// Determine disaster by keyword
switch (filter) {
case null:
this.logger.info('Bot could not detect request keyword');
return;
case 'banjir':
this.logger.info('Bot detected request keyword "banjir"');
language = 'id';
disaster = 'flood';
break;
case 'flood':
this.logger.info('Bot detected request keyword "flood"');
language = 'en';
disaster = 'flood';
break;
case 'gempa':
this.logger.info('Bot detected request keyword "gempa"');
language = 'id';
disaster = 'earthquake';
break;
case 'earthquake':
this.logger.info('Bot detected request keyword "earthquake"');
language = 'en';
disaster = 'earthquake';
break;
case 'forestfire':
this.logger.info('Bot detected request keyword "forestfire"');
language = 'en';
disaster = 'fire';
break;
case 'kebakaranhutan':
this.logger.info('Bot detected request keyword "kebakaranhutan"');
language = 'id';
disaster = 'fire';
break;
case 'haze':
this.logger.info('Bot detected request keyword "haze"');
language = 'en';
disaster = 'haze';
break;
// Next, initiate conversation with user
this._sendStart(username, language, disaster, tweetActivity)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment