Email via scheduled tasks (mathematica/wolfram) deployed to Wolfram Cloud

New version for 10.3

The scheduled task demos for Mathematica to run in the Wolfram Cloud are not very helpful if you want to use your own code. It is not clear on how to get things working and where you need to include the functions. I have been able to get it working using a 'WITH' statement and defining functions in the scheduled task.

"WITH" where i define the local variables separated by , (comma)

I define my modules (my functions) in the scheduled task and separate by ; (semi colon)

Note that processEvent is interesting as it fixes a problem with the wolfram databin returning values as String. I convert the values to integers using ToExpression and then I am able to plot them using the normal EventSeries.

Example code

Code:

dailyDashCloudObject = 
With[{shortid = "xxxxxx", dbin = Databin[shortid], 
    ccDestinations = {"yyyyy@ficonab.com"}, 
    CloudDeploy[
    ScheduledTask[
    processEvent[evt_TemporalData] := 
        Module[{dates, vals, series, i}, dates = evt["Dates"];
        series = {evt["Dates"][[#, 1]], 
        ToExpression[evt["Values"][[#]]]} & /@ 
        Table[i, {i, Length[evt["Values"]]}];
        EventSeries[series]]; 
    plotDatabin[db_Databin, tag_String] := 
        Module[{}, 
        DateListPlot[processEvent[EventSeries[db][tag]], 
        PlotLabel -> tag]]; 
    SendMail[<|"To" -> $WolframID, "Cc" -> ccDestinations, 
    "Subject" -> StringJoin["Daily Dash Report ", DateString[]], 
    "Body" -> 
    GraphicsGrid[{{plotDatabin[dbin, "revenue"], 
    plotDatabin[dbin, "fred"]}, {plotDatabin[dbin, 
    "fred2"]}}, ImageSize -> Large] |>], 
    "Daily"], "dailyDashBoardEmail"] ]

Of course this is all changed. Now you really need to use a driver block to get it working well. In the driver you set up the associatin and then you submit it as a job using document generator. Timespecs are not generally accurate as yet. Note the cloudGet to do a Needs on the background tasks. Email me with questions.

Example code

Code:

driver := 
  Block[{results, data, monthly, shortid, monthlydata, lotrev, lotpay,
      lotnet, adbin},
    monthlydata = fred`getMonth["summary"];
    results = 
     Association["totalRev" -> data["Total"], 
      "runDate" :> DateString[], 
      "distCount" -> data["Distributor Count"], "monthly" -> monthly, 
      "databin" -> adbin, "monthlydata" -> monthlydata, 
      "txcount" -> data["Transaction Count"], 
      "avgDist" -> data["Avg"] 
      ];
    results] &;
tplrunTime = 
 DateObject[{_, _, _, 21}]; (* daily at some approximate time *)dailyDashTemplate = 
 With[{ftemplatename = 
    StringJoin[NotebookDirectory[], "FredTemplate.nb"], 
   ccDestinations = {}}, 
  CloudDeploy[ CloudGet[CloudObject["yyyyyyy.wl"]];
   DocumentGenerator[File[ftemplatename], driver, 
    tplrunTime,(*NotificationFunction \[Rule] All, *)
    DeliveryFunction -> {{ 
        "uuuuuu@estormtech.com"} -> 
       "PDF"} ],
   "dailyTemplateEmail"]  (*cloud deploy *)
  ]  (* with *)