Last active
July 10, 2021 17:32
-
-
Save rodgtr1/a8362e853daebf682d3303b053a5a343 to your computer and use it in GitHub Desktop.
Lambda Function To Automate Birthday Greetings From CSV Written in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/csv" | |
"encoding/json" | |
"fmt" | |
"os" | |
"time" | |
"github.com/aws/aws-lambda-go/lambda" | |
"github.com/twilio/twilio-go" | |
openapi "github.com/twilio/twilio-go/rest/api/v2010" | |
) | |
type Birthdays struct { | |
Date string | |
Name string | |
GreetingName string | |
PhoneNumber string | |
} | |
func main() { | |
lambda.Start(sendBirthdayGreeting) | |
} | |
func sendBirthdayGreeting() { | |
t := time.Now() | |
date := fmt.Sprintf("%02d/%02d/%02d", int(t.Month()), t.Day(), t.Year()) | |
fmt.Println(date) | |
// Loop through CSV and match todays date with date in CSV | |
f, err := os.Open("Birthdays2021.csv") // For read access. | |
if err != nil { | |
fmt.Println(err) | |
} | |
defer f.Close() | |
r := csv.NewReader(f) | |
var bd Birthdays | |
for { | |
line, err := r.Read() | |
if err != nil { | |
fmt.Println(err) | |
} | |
if line[0] == date { | |
bd = Birthdays{ | |
Date: line[0], | |
Name: line[1], | |
GreetingName: line[2], | |
PhoneNumber: line[3], | |
} | |
break | |
} | |
} | |
text := "Happy Birthday " + bd.GreetingName | |
// Send text message | |
accountSid := os.Getenv("accountSid") | |
authToken := os.Getenv("authToken") | |
from := os.Getenv("from") | |
to := "+1" + bd.PhoneNumber | |
client := twilio.NewRestClientWithParams(accountSid, authToken, twilio.RestClientParams{}) | |
params := &openapi.CreateMessageParams{} | |
params.SetTo(to) | |
params.SetFrom(from) | |
params.SetBody(text) | |
resp, err := client.ApiV2010.CreateMessage(params) | |
if err != nil { | |
fmt.Println(err.Error()) | |
err = nil | |
} else { | |
response, _ := json.Marshal(*resp) | |
fmt.Println("Response: " + string(response)) | |
} | |
// Translate this all to the Lambda | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment