-
-
Save bash0C7/77ac4f311a40e5c956d5 to your computer and use it in GitHub Desktop.
Post message to group chat "idobata"
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 ( | |
"net/http" | |
"net/url" | |
"os" | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"strings" | |
) | |
func main() { | |
format, concurrency := func() (string, int) { | |
f := "" | |
flag.StringVar(&f, "format", "html", "html or json") | |
c := 0 | |
flag.IntVar(&c, "concurrency", 0, "concurrency") | |
flag.Parse() | |
return f, c | |
}() | |
source := func() string { | |
bytes, err := ioutil.ReadAll(os.Stdin) | |
if err != nil { | |
fmt.Println(err.Error()) | |
} | |
return string(bytes) | |
}() | |
webhook_url := make(chan string, concurrency) | |
post_done := make(chan int) | |
webhook_url_count := prepair_webhook_url(webhook_url, os.Getenv("URL")) | |
post_to_idobata(post_done, webhook_url, format, source) | |
exit_if(func() bool { | |
posted_count := 0 | |
for { | |
posted_count = posted_count + <-post_done | |
if webhook_url_count <= posted_count { | |
break | |
} | |
} | |
return true | |
}) | |
} | |
func prepair_webhook_url(webhook_url chan string, url string) int { | |
urls := strings.Split(url, ",") | |
go func() { | |
for _, u := range urls { | |
webhook_url <- u | |
} | |
}() | |
return len(urls) | |
} | |
func post_to_idobata(post_done chan int, webhook_url chan string, format string, source string) { | |
go func() { | |
for { | |
resp, err := http.PostForm(<-webhook_url, url.Values{"format": {format}, "source": {source}}) | |
if err != nil { | |
fmt.Println(err.Error()) | |
} | |
fmt.Println(resp.Status) | |
post_done <- 1 | |
} | |
}() | |
} | |
func exit_if(condition func() bool) { | |
if condition() { | |
os.Exit(0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment