Created
December 11, 2017 04:46
-
-
Save fujiwara/223f2bf9c21f3be04ef647cc14f891b1 to your computer and use it in GitHub Desktop.
mail2line
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 ( | |
"fmt" | |
"mime" | |
"net/http" | |
"net/mail" | |
"net/url" | |
"os" | |
"strings" | |
) | |
func main() { | |
text := "" | |
msg, err := mail.ReadMessage(os.Stdin) | |
if err != nil { | |
panic(err) | |
} | |
addrs, err := msg.Header.AddressList("From") | |
if err != nil { | |
panic(err) | |
} | |
if len(addrs) >= 0 { | |
text += addrs[0].Name + " " + addrs[0].Address + " から" | |
} | |
subject := msg.Header.Get("Subject") | |
if subject != "" { | |
dec := new(mime.WordDecoder) | |
s, err := dec.DecodeHeader(subject) | |
if err != nil { | |
panic(err) | |
} | |
text += s + " が届きました" | |
} | |
v := url.Values{"message": {text}} | |
req, err := http.NewRequest( | |
"POST", | |
"https://notify-api.line.me/api/notify", | |
strings.NewReader(v.Encode()), | |
) | |
if err != nil { | |
panic(err) | |
} | |
req.Header.Set("Authorization", "Bearer "+os.Getenv("TOKEN")) | |
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | |
resp, err := http.DefaultClient.Do(req) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(resp.Status) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment