Skip to content

Instantly share code, notes, and snippets.

@nuwanbando
Created March 23, 2020 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuwanbando/0c7da52c5ff05c4c2ffa946713edeb01 to your computer and use it in GitHub Desktop.
Save nuwanbando/0c7da52c5ff05c4c2ffa946713edeb01 to your computer and use it in GitHub Desktop.
import ballerina/io;
import ballerina/http;
//import miyurud/twilio;
//import ballerina/config;
import ballerina/observe;
#Twilio config
//twilio:TwilioConfiguration twilioConfig = {
// accountSId: "ACc055c08964b76ef55b0a755a3edcd6f3",
// authToken: "e5f33508721405842550c906c26febb2"
// xAuthyKey: config:getAsString("X_AUTHY_KEY")
//};
//twilio:Client twilioClient = new(twilioConfig);
#sending a whatsapp message with today's weather info
public function main() {
json|error weatherData = fetchTodaysWeather();
json[] daily = [];
if(weatherData is json){
daily = <json[]> weatherData.DailyForecasts;
}
string message = "Hello " + "Nuwan" + " good morning!! Today is "
+ daily[0].Day.IconPhrase.toString() + " and the night is "+ daily[0].Night.IconPhrase.toString() + ". Today's maximum temprature is "
+ daily[0].Temperature.Maximum.Value.toString() + "F and the minimum temprature is "
+ daily[0].Temperature.Minimum.Value.toString() + "F. Have a great day!! ~this is ur Ballerina weather bot ;)";
io:println(message);
// error? pushToWhatsAppResult = pushToWhatsApp(message);
// if (pushToWhatsAppResult is error) {
// io:println(pushToWhatsAppResult.reason());
// }
}
@observe:Observable
function fetchTodaysWeather() returns @tainted json|error {
http:Client accuWeatherEP = new ("http://dataservice.accuweather.com");
http:Response weatherResp = new;
weatherResp = check accuWeatherEP->get("/forecasts/v1/daily/1day/"+ "07042" +"?apikey=8dbh68Zg2J6WxAK37Cy2jVJTSMdnyAmV");
return weatherResp.getJsonPayload();
}
//@observe:Observable
//function pushToWhatsApp(string message) returns error? {
// var whatsAppResponse = twilioClient->sendWhatsAppMessage("whatsapp:" + config:getAsString("TWILIO_SANDBOX_NUMBER"),
// "whatsapp:" + config:getAsString("DESTINATION_PHONE_NUMBER"),
// message);
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment