diff --git a/app.js b/app.js index 3418205..516efde 100644 --- a/app.js +++ b/app.js @@ -1,63 +1,13 @@ const { Telegraf } = require('telegraf'); -const google = require('googlethis'); const fs = require('fs'); const https = require('https'); - +const { getJoke } = require('./dadJokes'); const r34 = require('./rule34'); -const { addToLogs } = require('./botTools'); +const { addToLogs, isTrue, image_search } = require('./botTools'); //bot initialization const bot = new Telegraf(process.env.TELEGRAM); -//functions -function image_search(query, ctx) { - // - //Search for an image on google and send it to the user - // - const images = google.image(query, { safe: false }).catch(err => { - console.log(err); - addToLogs("--> error : " + err); - bot.telegram.sendMessage(ctx.chat.id, "Something went wrong", {"reply_to_message_id": ctx.update.message.message_id}); - }); - images.then((results) => { - var imgLink = results[Math.floor(Math.random() * results.length)].url - bot.telegram.sendPhoto(ctx.chat.id, imgLink, {"caption": "This is a random image for the query : " + query}).catch(err => { - console.log(err); - addToLogs("--> error : " + err); - bot.telegram.sendMessage(ctx.chat.id, "Something went wrong", {"reply_to_message_id": ctx.update.message.message_id}); - }); - console.log("--> sent the image for the query: " + query); - addToLogs("--> sent the image for the query: " + query); - }) -} - -function isTrue(message, ctx) { - // - //Check if the message is a command - // - if (message != undefined) { - console.log("--> message received: " + message); - addToLogs("--> message received: " + message); - var totalSum = 0 - - for (var i = 0; i < message.length; i++) { - totalSum += message.charCodeAt(i) - } - if (totalSum%2 == 0) { - bot.telegram.sendMessage(ctx.chat.id, "This message is true", {"reply_to_message_id": ctx.update.message.reply_to_message.message_id}); - console.log("--> sent true for the query: " + message); - addToLogs("--> sent true for the query: " + message); - } - else { - bot.telegram.sendMessage(ctx.chat.id, "This message is false", {"reply_to_message_id": ctx.update.message.reply_to_message.message_id}); - console.log("--> sent false for the query: " + message); - addToLogs("--> sent false for the query: " + message); - } - } else { - bot.telegram.sendMessage(ctx.chat.id, "Please reply to a text message", {'reply_to_message_id': ctx.update.message.message_id}); - } -} - //bot commands bot.command('start', ctx => { bot.telegram.sendMessage(ctx.chat.id, 'hello there! Welcome to my new telegram bot.\nType /help for help.', {}) @@ -84,11 +34,11 @@ bot.command('github', ctx => { }) bot.command('s', ctx => { - image_search(ctx.message.text.slice(+3), ctx) + image_search(ctx.message.text.slice(+3), ctx, bot) }) bot.command('truce', ctx => { - isTrue(ctx.update.message.reply_to_message.text, ctx) + isTrue(ctx.update.message.reply_to_message.text, ctx, bot) }) bot.command('chatInfo', ctx => { @@ -110,8 +60,9 @@ bot.command('r34', ctx => { r34.r34(ctx.message.text.slice(+5), ctx, bot) }) -bot.command('rps', ctx => { - rockPaperCiscors(ctx.message.text.slice(+5), ctx) +bot.command('dadjoke', ctx => { + getJoke(ctx, bot) + console.log('--> sent a dad joke') }) //bot launch diff --git a/botTools.js b/botTools.js index 72b592a..c1b58f4 100644 --- a/botTools.js +++ b/botTools.js @@ -1,4 +1,53 @@ const fs = require('fs'); +const google = require('googlethis'); + +function image_search(query, ctx, bot) { + // + //Search for an image on google and send it to the user + // + const images = google.image(query, { safe: false }).catch(err => { + console.log(err); + addToLogs("--> error : " + err); + bot.telegram.sendMessage(ctx.chat.id, "Something went wrong", {"reply_to_message_id": ctx.update.message.message_id}); + }); + images.then((results) => { + var imgLink = results[Math.floor(Math.random() * results.length)].url + bot.telegram.sendPhoto(ctx.chat.id, imgLink, {"caption": "This is a random image for the query : " + query}).catch(err => { + console.log(err); + addToLogs("--> error : " + err); + bot.telegram.sendMessage(ctx.chat.id, "Something went wrong", {"reply_to_message_id": ctx.update.message.message_id}); + }); + console.log("--> sent the image for the query: " + query); + addToLogs("--> sent the image for the query: " + query); + }) +} + +function isTrue(message, ctx, bot) { + // + //Check if the message is a command + // + if (message != undefined) { + console.log("--> message received: " + message); + addToLogs("--> message received: " + message); + var totalSum = 0 + + for (var i = 0; i < message.length; i++) { + totalSum += message.charCodeAt(i) + } + if (totalSum%2 == 0) { + bot.telegram.sendMessage(ctx.chat.id, "This message is true", {"reply_to_message_id": ctx.update.message.reply_to_message.message_id}); + console.log("--> sent true for the query: " + message); + addToLogs("--> sent true for the query: " + message); + } + else { + bot.telegram.sendMessage(ctx.chat.id, "This message is false", {"reply_to_message_id": ctx.update.message.reply_to_message.message_id}); + console.log("--> sent false for the query: " + message); + addToLogs("--> sent false for the query: " + message); + } + } else { + bot.telegram.sendMessage(ctx.chat.id, "Please reply to a text message", {'reply_to_message_id': ctx.update.message.message_id}); + } +} function addToLogs(message) { // @@ -11,4 +60,4 @@ function addToLogs(message) { }); } -module.exports = { addToLogs }; \ No newline at end of file +module.exports = { addToLogs, isTrue, image_search }; \ No newline at end of file diff --git a/dadJokes.js b/dadJokes.js new file mode 100644 index 0000000..8352da2 --- /dev/null +++ b/dadJokes.js @@ -0,0 +1,23 @@ +const request = require('request'); + +function getJoke(ctx, bot) { + const options = { + method: 'GET', + url: 'https://dad-jokes.p.rapidapi.com/random/joke', + headers: { + 'X-RapidAPI-Key': process.env.DADJOKES, + 'X-RapidAPI-Host': 'dad-jokes.p.rapidapi.com', + useQueryString: true + } + }; + + request(options, function (error, response, body) { + if (error) throw new Error(error); + + res = JSON.parse(body) + bot.telegram.sendMessage(ctx.chat.id, res.body[0].setup, {}); + bot.telegram.sendMessage(ctx.chat.id, res.body[0].punchline, {}); + }); +} + +module.exports = { getJoke }; \ No newline at end of file diff --git a/games.js b/games.js index f9137c2..48b5eb4 100644 --- a/games.js +++ b/games.js @@ -1,5 +1,50 @@ -function rockPaperScissors() { - // Your code here -} +function rockPaperScissorsAgainstBot(ctx, bot) { + // Variables + var userChoice; + var computerChoice; + const CHOICES = ["rock", "paper", "scissors"]; -module.exports = { rockPaperScissors }; \ No newline at end of file + // Computer choice + + computerChoice = CHOICES[Math.floor(Math.random() * CHOICES.length)]; + + // User choice + + bot.telegram.sendMessage(ctx.chat.id, "Choose between rock, paper or scissors", {}); + + + + //Display choices + bot.telegram.sendMessage(ctx.chat.id, "You chose " + userChoice + ". The bot chose " + computerChoice + "."); + + // Winner + switch (userChoice) { + case "rock": + if (computerChoice == "rock") { + ctx.reply("It's a tie"); + } else if (computerChoice == "paper") { + ctx.reply("You lose"); + } else { + ctx.reply("You win"); + } + break; + case "paper": + if (computerChoice == "rock") { + ctx.reply("You win"); + } else if (computerChoice == "paper") { + ctx.reply("It's a tie"); + } else { + ctx.reply("You lose"); + } + break; + case "scissors": + if (computerChoice == "rock") { + ctx.reply("You lose"); + } else if (computerChoice == "paper") { + ctx.reply("You win"); + } else { + ctx.reply("It's a tie"); + } + break; + } +} \ No newline at end of file