Skip to content

Commit

Permalink
Criando bot para whatsapp com a twilio
Browse files Browse the repository at this point in the history
  • Loading branch information
jaovitu committed Aug 23, 2022
0 parents commit c804bed
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
*.lock
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: node src/index.js
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "twilio-wpp-teste",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"dev": "npx nodemon src/index.js"
},
"dependencies": {
"express": "^4.18.1",
"twilio": "^3.80.1"
},
"devDependencies": {
"nodemon": "^2.0.19"
}
}
44 changes: 44 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const express = require('express');
const twilio = require('twilio');

const app = express();

app.use(express.urlencoded({ extended: true }));
app.use(express.json());

const options = [ 'pedra', 'papel', 'tesoura' ];

const wins = {
pedra: 'tesoura',
papel: 'pedra',
tesoura: 'papel'
};

app.post('/message', (request, response) => {
const { Body: userChoice } = request.body;

if ( !(options.includes(userChoice)) ) {
return response.send('<Response><Message>Escolha pedra, papel ou tesoura.</Message></Response>');
}

const botChoice = options[ Math.floor( Math.random() * options.length ) ];

const isUserWinner = wins[userChoice.toLowerCase()] === botChoice;

if (!isUserWinner) {
const twiml = new twilio.twiml.MessagingResponse();
twiml.message(`*${botChoice}* ganha de *${userChoice}*`);
twiml.message(`Eu ganhei!!!`);

return response.send(twiml.toString());
}

const twiml = twilio.twiml.MessagingResponse();
twiml.message(`*${userChoice}* ganha de *${botChoice}*`);
twiml.message('Você ganhou :(... Quero revanche!');

response.send(twiml.toString());

});

app.listen(3000, () => console.log('Server is running on port 3000...'));

0 comments on commit c804bed

Please sign in to comment.