-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (72 loc) · 2.11 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"context"
"log"
"github.com/aws/aws-lambda-go/lambda"
)
func handler(ctx context.Context) {
// Create a DynamoDB client
dbClient, err := newDynamoDBClient(ctx, getDynamoDBConfig().TableName)
if err != nil {
log.Printf("[error] Failed to create DynamoDB client: %v", err)
return
}
// Check if today's tweets have already been sent
// If it has, exit
currentDateJST := getCurrentDateJST()
if exists, err := dbClient.checkItemExists(ctx, currentDateJST); err != nil {
log.Printf("[error] Failed to check if tweets sent today: %v", err)
return
} else if exists {
log.Println("[info] Today's tweet have already been sent")
return
}
// Search for tweets
tweets, err := realtimeSearch(getScrapeConfig())
if err != nil {
log.Printf("[error] Failed to search latest tweet: %v", err)
return
}
// if no tweets are found, exit
if len(tweets) == 0 {
log.Println("[info] No tweets found")
return
}
// Check if the latest tweet is from today in JST
latestTweet := tweets[0]
haveUnsentTweet, err := latestTweet.isOnToday()
if err != nil {
log.Printf("[error] Failed to check if the latest tweet is from today: %v", err)
return
}
// If it's from today in JST, it means it hasn't been sent yet, so send it to the webhook
// If it's from yesterday, it means there are no tweets for today yet, so exit
if !haveUnsentTweet {
log.Println("[info] Today's tweet have not been found yet")
return
}
// Send the tweet to the webhook
statusCode, err := doWebhook(getWebhookConfig(), latestTweet)
if err != nil || statusCode >= 400 {
log.Printf("[error] Failed to send to webhook. status Code: %v, error: %v", statusCode, err)
return
}
// Save the sent record in DynamoDB
err = dbClient.saveSentRecord(ctx, currentDateJST, latestTweet.id)
if err != nil {
log.Printf("[error] Failed to save sent record to DynamoDB: %v", err)
return
}
}
func main() {
if isRunningOnLambda() {
lambda.Start(handler)
} else {
err := LoadEnvFromConfig()
if err != nil {
log.Printf("[error] Failed to load environment variables: %v", err)
return
}
handler(context.Background())
}
}