“Quote of the day” Message Service using (Ballerina)
To view an overview of how to install and how to use ballerina and get the guidance follow this link.
It’s a simple system which sends the “Quotes of the day” messages to the gmail and mobile.

System works like that.
- The system first fetch the quotes from the Twitter.
- It will collect the gmail and mobile number of the subscribers from the spreadsheet.
- Then it will send the quotes to the Gmail and Mobile-Number.
you can simply change the system as a data fetching system from the twitter . You have to preprocess the fetched data first. Here processing could be involved in machine learning. But I haven’t done the processing part here. Processed data will be sent to the customers who are listed in the spreadsheet.

This project involves five SaaS services such as Twitter, Google SpreadSheets ,Gmail and Twilio.
What is a SAAS (Software as a Service) service?
Software as a service (SaaS) is a software distribution model in which a third-party provider hosts applications and makes them available to customers over the Internet. SaaS is one of three main categories of cloud computing, alongside infrastructure as a service (IaaS) and platform as a service (PaaS).
SaaS removes the need for organizations to install and run applications on their own computers or in their own data centers. This eliminates the expense of hardware acquisition, provisioning and maintenance, as well as software licensing, installation and support.
Lets’s move to the code.
First you should find the access tokens from the Twitter, Gmail, Spreadsheets and Twilio if you don’t want medium don’t put the access tokens. You can use the Ballerina connectors from BallerinaCentral to make your task easy.
The Ballerina Twitter connector allows you to tweet, retweet, untweet, and search for tweets through the Twitter REST API. You can also retrieve and destroy a status, and retrieve closest trend locations and top trends.
You can use the Ballerina Google Spreadsheet connector to read the spreadsheet, iterate through the rows and pick up the product name, email address and name of each customer from the columns. Then, you can use the Gmail connector to simply add the name to the body of a html mail template and send the email to the relevant customer.
The Ballerina Gmail connector allows you to send, read, and delete emails through the Gmail REST API. It handles OAuth 2.0 authentication. It also provides the ability to read, trash, untrash, delete threads, get the Gmail profile, mailbox history, etc.
The Ballerina Twilio connector allows you to send SMS, voice, and OTP messages through the Twilio REST API. You can also send user secrets via SMS or voice message, verify OTP, and add and delete users. It handles basic authentication
Create a package for your project go inside and create a config file in the name of ballerina.toml
Getting Keys and tokens from Twitter
Note: You need to have a Twitter account set up with a valid mobile number to try this.
- Go to https://apps.twitter.com/ and click Create New App.
- Fill the form that appears and click Create your Twitter application.
- Once your app is created, navigate to the Keys and Access Tokens tab. Make note of the Consumer Key (API Key) and Consumer Secret (API Secret). Generate an access token in the Your Access Token section by clicking Create my access token.
- Fill the first 4 lines with the token you got fromm Twitter App
consumerKey = ""
consumerSecret = ""
accessToken = ""
accessTokenSecret = ""
Getting Keys and token from Gmail and Spreadsheets
- Visit Google API Console, click Create Project, and follow the wizard to create a new project.
- Enable both Gmail and Google Sheets APIs for the project.
- Go to Credentials -> OAuth consent screen, enter a product name to be shown to users, and click Save.
- On the Credentials tab, click Create credentials and select OAuth client ID.
- Select an application type, enter a name for the application, and specify a redirect URI (enter https://developers.google.com/oauthplayground if you want to use OAuth 2.0 playground to receive the authorization code and obtain the access token and refresh token).
- Click Create. Your client ID and client secret appear.
- In a separate browser window or tab, visit OAuth 2.0 playground, select the required Gmail and Google Sheets API scopes, and then click Authorize APIs.
- When you receive your authorization code, click Exchange authorization code for tokens to obtain the refresh token and access token.
- Fill the below field using your tokens
gmailAccessToken = ""
gmailClientId = ""
gmailClientSecret = ""
gmailRefreshToken = ""
SPREADSHEET_ID=""
SHEET_NAME="Sheet1"
SENDER=""
USER_ID="me"
Setup Twilio configurations
Create a Twilio account and obtain the following parameters:
- Account SId
- Auth Token
For more information on obtaining the above parameters, see Create a Twilio Authy app.
Set Twilio credentials in ballerina.conf
(required parameters are TaccountSID
, TauthToken
and TxAuthKey .
TaccountSId=""
TauthToken=""
TxAuthyKey=""
Now you have setup your initial requirements to run the app.
Now move to the Ballerina code that will explain how to use these services and get the work done.
1. import these files.
import ballerina/http;
import ballerina/io;
import wso2/twitter;
import wso2/gmail;
import wso2/gsheets4;
import wso2/twilio;
import ballerina/config;
import ballerinax/docker;
import ballerina/log;
import ballerina/task;
import ballerina/runtime;
task:Timer? timer;
2. get the secrets from the ballerina.conf file
//Twitter API CONFdocumentation{A valid Client id with twitter.}
string twitterClientId = config:getAsString("twitterClientId");documentation{A valid Client Secret with twitter access.}
string twitterClientSecret = config:getAsString("twitterClientSecret");documentation{A valid AccessToken.}
string twitterAccessToken = config:getAsString("twitterAccessToken");documentation{A valid AccessTokenSecret.}
string twitterAccessTokenSecret = config:getAsString("twitterAccessTokenSecret");//Gmail API CONFdocumentation{A valid access token with gmail and google sheets access.}
string accessToken = config:getAsString("gmailAccessToken");documentation{The client ID for your application.}
string clientId = config:getAsString("gmailClientId");documentation{The client secret for your application.}
string clientSecret = config:getAsString("gmailClientSecret");documentation{A valid refreshToken with gmail and google sheets access.}
string refreshToken = config:getAsString("gmailRefreshToken");//Sender Detailsdocumentation{Sender email address.}
string senderEmail = config:getAsString("SENDER");documentation{The user's email address.}
string userId = config:getAsString("USER_ID");//SpreadSheet API CONFdocumentation{Spreadsheet id of the reference google sheet.}
string spreadsheetId = config:getAsString("SPREADSHEET_ID");documentation{Sheet name of the reference googlle sheet.}
string sheetName = config:getAsString("SHEET_NAME");//TWilio API CONFdocumentation{Spreadsheet id of the reference google sheet.}
string TaccountSId = config:getAsString("TaccountSId");documentation{Sheet name of the reference googlle sheet.}
string TauthToken = config:getAsString("TauthToken");
3. Create the Client endpoints
documentation{
twitter client endpoint declaration with oAuth2 client configurations.
}endpoint twitter:Client twitter {
clientId: twitterClientId,
clientSecret: twitterClientSecret,
accessToken: twitterAccessToken,
accessTokenSecret: twitterAccessTokenSecret
};documentation{
Gmail client endpoint declaration with oAuth2 client configurations.
}endpoint gmail:Client gmailClient {
clientConfig: {
auth: {
accessToken: accessToken,
clientId: clientId,
clientSecret: clientSecret,
refreshToken: refreshToken
}
}
};documentation{
Google Sheets client endpoint declaration with http client configurations.
}endpoint gsheets4:Client spreadsheetClient {
clientConfig: {
auth: {
accessToken: accessToken,
refreshToken: refreshToken,
clientId: clientId,
clientSecret: clientSecret
}
}
};documentation{
Google Sheets client endpoint declaration with http client configurations.
}endpoint twilio:Client twilioEP {
accountSId: TaccountSId,
authToken: TauthToken,
xAuthyKey: ""
};
4. In the main method run the service every 24 hours (DAY)
documentation{
Schedule a timer task, which initially runs from now.
After that, it runs every day.
It will collect the tweets and process it and send it to the users
}function main(string... args) {(function() returns error?) onTriggerFunction = startService;
function(error) onErrorFunction = cleanupError;
timer = new task:Timer(startService, cleanupError, 24 * 3600 * 1000, delay = 0);
timer.start();
runtime:sleep(30000);}
5. Start Service function that will fetch the tweets ,filter it,format and send as a notification. comments above every code describe the functionality of the all functions.
5. Use these helper functions to build the system. read the comments above the code to get understand what’s happening there.
To run the app just go to the project folder using Terminal. thenuse this command.
sudo ballerina run — config ballerina.toml hello_service.bal
This is the final high level architecture of the app.

Finally you can view the full code here : on GITHUB
I hope you have found this Guide Helpful. I wanted to publish a longer version however I don’t want to scare the readers away. please leave your comments that will help me to improve me. Please read the post about NLP from scratch if you are interested in data science. Thank you.