Skip to content

Commit

Permalink
Core functionality added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Deeb committed Dec 21, 2015
1 parent d5cb9cf commit 3dd0692
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 36 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
===
A [Telegram](https://telegram.org) bot to help you decide what to eat. Written using [node-telegram-api](https://github.com/mdibaiee/node-telegram-api).

Notes
------
- Functionality of this bot is still under development.
- Pull requests to correct stylizing or inefficiencies are greatly apperciated!
Pull requests welcomed and greatly apperciated!

Setup
------
Expand Down
135 changes: 106 additions & 29 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,42 @@ var Message = require('telegram-api/types/Message');
var Question = require('telegram-api/types/Question');
var json = require('./restaurants.json');

function sendInvalidMessage(id) {
const msg = new Message().to(id).text('Invalid answer');
bot.send(msg);
}

function feedbackPrompt(id) {
bot.send(new Message().to(id).text('If you want to help my development or make a bot like me, check out my source!\n https://github.com/michaeljdeeb/indecisive-bot'));
}

function recursiveRestaurantQuestion(availableRestaurants, id, answer) {
if(availableRestaurants.length == 0) {
bot.send(new Message().to(id).text('No restaurants matched those filters. Type \'IDK\' to start over.'));
feedbackPrompt(id);
} else if(availableRestaurants.length == 1) {
bot.send(new Message().to(id).text('The only matching restaurant is: ' + availableRestaurants[0].name + '\nEnjoy your meal!'));
feedbackPrompt(id);
} else {
const randomRestaurant = new Question({
text: 'How about ' + availableRestaurants[Math.floor(Math.random()*availableRestaurants.length)].name + '?',
answers: [['Yes'], ['No']]
});

randomRestaurant.to(id);
bot.send(randomRestaurant).then(answer => {
if(answer.text == 'Yes') {
bot.send(new Message().to(id).text('Enjoy your meal!'));
feedbackPrompt(id);
} else {
recursiveRestaurantQuestion(availableRestaurants, id, answer);
}
}, () => {
sendInvalidMessage(id);
});
}
}

function getKeyboardOptions(jsonKey) {
var uniqueChoices = new Set();
for(var i=0; i<json.length; i++) {
Expand All @@ -24,6 +60,39 @@ function getKeyboardOptions(jsonKey) {
return listKeyboardFormat;
}

function removeFarRestaurants(availableRestaurants) {
for(var i=0; i<availableRestaurants.length; i++) {
if(availableRestaurants[i].isClose == 'false') {
availableRestaurants.splice(i, 1);
}
}

return availableRestaurants;
}

function filterRestaurantsBy(filterKey, filterValue, availableRestaurants) {
var filteredRestaurants = [];

if(filterValue == 'No Preference') {
return availableRestaurants;
} else {
for(var i=0; i<availableRestaurants.length; i++) {
if(Array.isArray(availableRestaurants[i][filterKey])) {
for(var j=0; j<availableRestaurants[i][filterKey].length; j++) {
if(availableRestaurants[i][filterKey][j] == filterValue) {
filteredRestaurants.push(availableRestaurants[i]);
}
}
} else {
if(availableRestaurants[i][filterKey] == filterValue) {
filteredRestaurants.push(availableRestaurants[i]);
}
}
}
return filteredRestaurants;
}
}

var bot = new Bot({
token: process.env.TELEGRAM_BOT_TOKEN
});
Expand Down Expand Up @@ -57,41 +126,49 @@ const distanceQuestion = new Question({
});

bot.get('IDK', message => {
var filteredRestaurants = json;
const id = message.chat.id;

randomQuestion.to(id).reply(message.message_id);

function recursiveRestaurantQuestion() {
const randomRestaurant = new Question({
text: 'How about ' + json[Math.floor(Math.random()*json.length)].name + '?',
answers: [['Yes'], ['No']]
});

randomRestaurant.to(id).reply(message.message_id);
bot.send(randomRestaurant).then(answer => {
if(answer.text == 'Yes') {
const msg = new Message().to(id).text('Enjoy your meal!');
bot.send(msg);
} else {
recursiveRestaurantQuestion();
}
}, () => {
const msg = new Message().to(id).text('Invalid answer');
bot.send(msg);
});
}

randomQuestion.to(id);
bot.send(randomQuestion).then(answer => {
if(answer.text == 'Yes') {
recursiveRestaurantQuestion();
recursiveRestaurantQuestion(filteredRestaurants, id, null);
} else {
const msg = new Message().to(id).text('Ability to filter by other parameters coming soon. For now enjoy getting random restaurant suggestions.\n\n' +
'If you want to make a bot like me, check out my source!\n' +
'https://github.com/michaeljdeeb/indecisive-bot');
bot.send(msg);
serviceQuestion.to(id);
bot.send(serviceQuestion).then(service => {
filteredRestaurants = filterRestaurantsBy('service', service.text, filteredRestaurants);
if(service.text != 'Delivery') {
distanceQuestion.to(id);
bot.send(distanceQuestion).then(isFar => {
if(isFar.text == 'No') {
filteredRestaurants = removeFarRestaurants(filteredRestaurants);
}

cuisineQuestion.to(id);
bot.send(cuisineQuestion).then(cuisine => {
filteredRestaurants = filterRestaurantsBy('cuisine', cuisine.text, filteredRestaurants);
recursiveRestaurantQuestion(filteredRestaurants, id, null);

}, () =>{
sendInvalidMessage(id);
});
}, () => {
sendInvalidMessage(id);
});
} else {
cuisineQuestion.to(id);
bot.send(cuisineQuestion).then(cuisine => {
filteredRestaurants = filterRestaurantsBy('cuisine', cuisine.text, filteredRestaurants);
recursiveRestaurantQuestion(filteredRestaurants, id, null);
}, () =>{
sendInvalidMessage(id);
});
}
}, () => {
sendInvalidMessage(id);
});
}
}, () => {
const msg = new Message().to(id).text('Invalid answer');
bot.send(msg);
sendInvalidMessage(id);
});
});
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "indecisive-bot",
"description": "Bot to help indecisive diners",
"version": "0.0.1",
"description": "A Telegram bot to help you decide what to eat.",
"version": "1.0",
"private": true,
"dependencies": {
"telegram-api": "*"
}
}
}

0 comments on commit 3dd0692

Please sign in to comment.