Compare commits
4 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
e826c9aa08 | |
|
|
4f10099b41 | |
|
|
c8fec23964 | |
|
|
05fb63e633 |
148
main.go
148
main.go
|
|
@ -56,11 +56,16 @@ type Config struct {
|
||||||
} `toml:"postmark"`
|
} `toml:"postmark"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
MEETUP_INVITE_CLIENT_VERSION = "v3.0.0"
|
||||||
|
)
|
||||||
|
|
||||||
func retrieveSubscribers(cfg *Config) ([]string, error) {
|
func retrieveSubscribers(cfg *Config) ([]string, error) {
|
||||||
/* Prepare request payload */
|
/* Prepare request payload */
|
||||||
/* *** */
|
/* *** */
|
||||||
payload := map[string]string{
|
payload := map[string]string{
|
||||||
"city": cfg.HMC.City,
|
"city": cfg.HMC.City,
|
||||||
|
"client": MEETUP_INVITE_CLIENT_VERSION,
|
||||||
}
|
}
|
||||||
|
|
||||||
payloadBytes, err := json.Marshal(payload)
|
payloadBytes, err := json.Marshal(payload)
|
||||||
|
|
@ -190,6 +195,15 @@ func sendTest(cfg *Config, markdownFile string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Convert Markdown to Blog Post */
|
||||||
|
/* *** */
|
||||||
|
url, valid := bloggify(cfg, subject, body, false)
|
||||||
|
if !valid {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
body = fmt.Sprintf("[View Online](%s)\n\n%s", url, body)
|
||||||
|
|
||||||
/* Convert Markdown to HTML */
|
/* Convert Markdown to HTML */
|
||||||
/* *** */
|
/* *** */
|
||||||
html := blackfriday.Run([]byte(body))
|
html := blackfriday.Run([]byte(body))
|
||||||
|
|
@ -208,6 +222,117 @@ func sendTest(cfg *Config, markdownFile string) {
|
||||||
os.WriteFile(sha1File, []byte(sumString), 0666)
|
os.WriteFile(sha1File, []byte(sumString), 0666)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func bloggify(cfg *Config, subject, body string, isLive bool) (string, bool) {
|
||||||
|
if !isLive {
|
||||||
|
return "https://handmadecities.com/meetupinvite2000", true
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Prepare request payload */
|
||||||
|
/* *** */
|
||||||
|
payload := map[string]string{
|
||||||
|
"city": cfg.HMC.City,
|
||||||
|
"author": cfg.Postmark.SenderName,
|
||||||
|
"email": cfg.Postmark.SenderEmail,
|
||||||
|
"subject": subject,
|
||||||
|
"body": body,
|
||||||
|
"client": MEETUP_INVITE_CLIENT_VERSION,
|
||||||
|
}
|
||||||
|
|
||||||
|
payloadBytes, err := json.Marshal(payload)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error encoding payload: %v\n", err)
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Prepare request */
|
||||||
|
/* *** */
|
||||||
|
req, err := http.NewRequest("POST", "https://deploy.handmadecities.com/v1/bloggify", bytes.NewReader(payloadBytes))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error creating request: %v\n", err)
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Set("Authorization", cfg.HMC.SharedSecret)
|
||||||
|
|
||||||
|
/* Spinner while request runs */
|
||||||
|
/* *** */
|
||||||
|
stop := make(chan struct{})
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
frames := []rune{'⠁', '⠂', '⠄', '⡀', '⢀', '⠠', '⠐', '⠈'}
|
||||||
|
i := 0
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-stop:
|
||||||
|
close(done)
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
fmt.Printf("\r📝 Bloggifying your email... %c", frames[i%len(frames)])
|
||||||
|
i++
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
/* Execute request */
|
||||||
|
/* *** */
|
||||||
|
client := http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
close(stop)
|
||||||
|
<-done
|
||||||
|
fmt.Printf("\r\033[31m📝 Failed to bloggify: %v\033[0m\n", err)
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
/* Fail if status code is not in the 2xx range */
|
||||||
|
/* *** */
|
||||||
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
|
close(stop)
|
||||||
|
<-done
|
||||||
|
fmt.Printf("\r\033[31m📝 Bloggify error: HTTP status code %d\033[0m\n", resp.StatusCode)
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read response's body */
|
||||||
|
/* *** */
|
||||||
|
respBody, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
close(stop)
|
||||||
|
<-done
|
||||||
|
fmt.Printf("\r\033[31m📝 Error reading bloggify response: %v\033[0m\n", err)
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Parse JSON response */
|
||||||
|
/* *** */
|
||||||
|
var data map[string]interface{}
|
||||||
|
if err := json.Unmarshal(respBody, &data); err != nil {
|
||||||
|
close(stop)
|
||||||
|
<-done
|
||||||
|
fmt.Printf("\r\033[31m📝 Error parsing bloggify JSON: %v\033[0m\n", err)
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Extract the URL from the response */
|
||||||
|
/* *** */
|
||||||
|
url, ok := data["url"].(string)
|
||||||
|
if !ok || url == "" {
|
||||||
|
close(stop)
|
||||||
|
<-done
|
||||||
|
fmt.Printf("\r\033[31m📝 Bloggify response missing 'url' field\033[0m\n")
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
close(stop)
|
||||||
|
<-done
|
||||||
|
fmt.Printf("\r\033[32m📝 Your email is a blog post at %s\033[0m\n", url)
|
||||||
|
|
||||||
|
return url, true
|
||||||
|
}
|
||||||
|
|
||||||
func sendNews(cfg *Config, markdownFile string) {
|
func sendNews(cfg *Config, markdownFile string) {
|
||||||
/* Open and parse Markdown file */
|
/* Open and parse Markdown file */
|
||||||
/* *** */
|
/* *** */
|
||||||
|
|
@ -228,6 +353,16 @@ func sendNews(cfg *Config, markdownFile string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Convert Markdown to Blog Post */
|
||||||
|
/* *** */
|
||||||
|
url, valid := bloggify(cfg, subject, body, true)
|
||||||
|
if !valid {
|
||||||
|
fmt.Printf("Error converting '%s' into a blog post\n%v\n", markdownFile, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
body = fmt.Sprintf("[View Online](%s)\n\n%s", url, body)
|
||||||
|
|
||||||
/* Convert Markdown to HTML */
|
/* Convert Markdown to HTML */
|
||||||
/* *** */
|
/* *** */
|
||||||
html := blackfriday.Run([]byte(body))
|
html := blackfriday.Run([]byte(body))
|
||||||
|
|
@ -964,7 +1099,7 @@ func main() {
|
||||||
* Batch sends your invite using our Email API (Postmark)
|
* Batch sends your invite using our Email API (Postmark)
|
||||||
* Produces .track file that tracks email addresses we attempted to send to
|
* Produces .track file that tracks email addresses we attempted to send to
|
||||||
* Produces .log file with information received back from Postmark
|
* Produces .log file with information received back from Postmark
|
||||||
* You can always blast again: useful when emailing new subscribers. Addresses already in .track file will be skipped
|
* You can always blast again: addresses in .track files are skipped
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
@ -1017,6 +1152,17 @@ func main() {
|
||||||
cmd.Usage()
|
cmd.Usage()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reader := bufio.NewReader(os.Stdin)
|
||||||
|
fmt.Printf("Ready to blast to everyone in %s? (Y/n): ", config.HMC.City)
|
||||||
|
resp, _ := reader.ReadString('\n')
|
||||||
|
resp = strings.TrimSpace(strings.ToLower(resp))
|
||||||
|
|
||||||
|
if resp != "" && resp != "y" && resp != "yes" {
|
||||||
|
fmt.Println("Blast cancelled.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
updateFile := args[0]
|
updateFile := args[0]
|
||||||
sendNews(config, updateFile)
|
sendNews(config, updateFile)
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue