Skip to content

Instantly share code, notes, and snippets.

@bash0C7
Created Jan 18, 2014
Embed
What would you like to do?
Post message to group chat "idobata"
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