-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Criando bot para whatsapp com a twilio
- Loading branch information
0 parents
commit c804bed
Showing
4 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
*.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: node src/index.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...')); |