Skip to content

Instantly share code, notes, and snippets.

@jasonmc
Last active November 16, 2017 21:51
Show Gist options
  • Save jasonmc/3c4883e1559ac41ff1d895e8f54470fa to your computer and use it in GitHub Desktop.
Save jasonmc/3c4883e1559ac41ff1d895e8f54470fa to your computer and use it in GitHub Desktop.
(*
Copyright 2017 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Author: Jason McCandless
*)
open Microsoft.Extensions.Configuration
open SendGrid
open SendGrid.Helpers.Mail
open System.IO
open System.Net
module config =
let root = ConfigurationBuilder()
.AddJsonFile("config.json")
.Build()
let apiKey = root.GetValue<string> "apiKey"
let from = root.GetValue<string> "fromAddress"
let url = "https://egov.uscis.gov/casestatus/mycasestatus.do"
let toCsList (x: seq<'a>) = new ResizeArray<_>(x)
let createMail from tos subject plainTextContent htmlContent =
MailHelper.CreateSingleEmailToMultipleRecipients(
from, tos |> toCsList, subject, plainTextContent, htmlContent)
let check caseNum = async {
let rParams = dict ["appReceiptNum", caseNum; "initCaseSearch", "CHECK STATUS"]
let c = new Http.FormUrlEncodedContent(rParams)
let httpClient = new Http.HttpClient()
let! result = httpClient.PostAsync (config.url, c) |> Async.AwaitTask
let! status = result.Content.ReadAsStringAsync() |> Async.AwaitTask
return status.Contains "Case Was Received"
}
let sendMsg recipients caseNum =
let client = SendGridClient(config.apiKey)
let subject = "I-140 status changed!"
let toAddrs = recipients |> Seq.map EmailAddress
let plainTextContent = sprintf "Check on case: %s" caseNum
let htmlContent = sprintf "Check on case <strong>%s</strong>" caseNum
let from = EmailAddress config.from
let msg = createMail from toAddrs subject plainTextContent htmlContent
client.SendEmailAsync(msg) |> Async.AwaitTask
let perform caseNum recipients = async {
let! waiting = check caseNum
if not waiting then
printfn "Status changed, going to send a mail"
do! sendMsg recipients caseNum |> Async.Ignore
else
printfn "Still waiting"
}
[<EntryPoint>]
let main argv =
assert not (isNull config.apiKey)
assert not (isNull config.from)
match argv|> Array.toList with
| caseNum :: recipients -> perform caseNum recipients |> Async.RunSynchronously
| _ -> failwith "Need a case number and some recipients"
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment