This is Day#1 of the #ServerlessChallenge, where I'm creating/building serverless apps using different AWS services.
The short description: an app sends me a WhatsApp message every day to teach me a random Spanish word with its meaning.
The long/tech description
Using the serverless framework, I've built a simple lambda function that is being triggered with an EventBridge rule:
events: [
{
schedule: {
rate: ["rate(1 day)"]
}
}
]
EventBridge rules are so simple and interesting to write, as example if you want to write a rule that triggers an event on only the work days at 9 am, it will be like this: cron(0 9 ? MON-FRI )
The lambda function is so small, what it does:
Getting a random Spanish word using paunovic/random-words
Translate this word using bing-translate-api
Send a good-formatted WhatsApp every day with the word and its translation using Twilio
// src/functions/teach/index.ts
const word = RANDOM.word();
const translation = await translate(word,
'process.env.LANGUAGE_TO',
'process.env.LANGUAGE_FROM'
);
const message = {
from: `whatsapp:${process.env.TWILIO_SENDER}`,
body: `Hola! ๐ \n
Today's word is: *${translation.text}* \n
Which means: *${translation.translation}*`,
to: `whatsapp:${process.env.TWILIO_RECEIVER}`
};
await client.messages.create(message);
And as my favorite song says: โซ Don't place the API key/secrets & configs in the code directly, my friend โซ
You can safely store any keys/secrets/configs inside the serverless configuration provider.environment
and they will be added as env variables for the lambda function
// serverless.ts
const serverlessConfiguration: AWS = {
//..
provider: {
environment: {
TWILIO_ACCOUNT_SID: 'xx',
TWILIO_AUTH_TOKEN: 'xx',
TWILIO_SENDER: 'xx',
TWILIO_RECEIVER: 'xx',
LANGUAGE_TO: 'es',
LANGUAGE_FROM: 'en'
},
},
}
the github repo: https://github.com/Rochdy/teachMeSpanish
That was the app of Day#1! looking forward to posting soon about the next app of the challenge.