Created
May 26, 2015 12:47
-
-
Save 7yan00/717a2461da01c2ddae26 to your computer and use it in GitHub Desktop.
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 ( | |
"bufio" | |
"bytes" | |
"encoding/json" | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"regexp" | |
"strings" | |
"github.com/mrjones/oauth" | |
) | |
type sMessage struct { | |
Utt string `json:"utt"` | |
Mode string `json:"mode"` | |
} | |
type rMessage struct { | |
Utt string `json:"utt"` | |
Yomi string `json:"yomi"` | |
Mode string `json:"mode"` | |
Da int `json:"do"` | |
Context string `json:"context"` | |
} | |
type user struct { | |
Id int64 `json:"id"` | |
Name string `json:"name"` | |
ScreenName string `json:"screen_name"` | |
} | |
type status struct { | |
Id uint64 `json:"id"` | |
Text string `json:"text"` | |
User user `json:"user"` | |
} | |
var consumerKey *string = flag.String( | |
"consumerkey", | |
"mEF22DxPk6cocNoc3lQQBoj55", | |
"Consumer Key from Twitter. See: https://dev.twitter.com/apps/new") | |
var consumerSecret *string = flag.String( | |
"consumersecret", | |
"cGOq2NGmEqdwzVPPkQfMJuh6HEVFuVz5qFqBQJAteVuKC4ZQS9", | |
"Consumer Secret from Twitter. See: https://dev.twitter.com/apps/new") | |
var accessToken *oauth.AccessToken | |
var c = oauth.NewConsumer( | |
*consumerKey, | |
*consumerSecret, | |
oauth.ServiceProvider{ | |
RequestTokenUrl: "https://api.twitter.com/oauth/request_token", | |
AuthorizeTokenUrl: "https://api.twitter.com/oauth/authorize", | |
AccessTokenUrl: "https://api.twitter.com/oauth/access_token", | |
}) | |
func main() { | |
ScreenName := "yukatan123456" | |
re := regexp.MustCompile(`^(.+)\(@` + ScreenName + `\)$`) | |
flag.Parse() | |
fmt.Println("loading consumerkey......") | |
loading() | |
get_timeline(func(b []byte) { | |
s := new(status) | |
err := json.Unmarshal(b, s) | |
if err != nil { | |
return | |
} | |
fmt.Println("阿澄佳奈") | |
fmt.Println(s) | |
match := re.FindStringSubmatch(s.Text) | |
if len(match) != 2 { | |
return | |
} | |
mes := strings.TrimSpace(match[1]) | |
fmt.Println("mes") | |
fmt.Println(mes) | |
get_apl(mes, s.User.ScreenName, s.Id) | |
}) | |
} | |
func get_apl(word string, name string, id uint64) { | |
hoge := sMessage{word, "dialog"} | |
fuga, _ := json.Marshal(hoge) | |
fofo := bytes.NewReader(fuga) | |
res, err := http.Post("https://api.apigw.smt.docomo.ne.jp/dialogue/v1/dialogue?APIKEY=6250596174454f7a6268415a346b6f65556b4a4f5837534b32306448367a5a4c646c67624c5077484c7041", "application/json", fofo) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
resBytes, _ := ioutil.ReadAll(res.Body) | |
fmt.Println("resbytes") | |
fmt.Println(string(resBytes)) | |
messages := new(rMessage) | |
err = json.Unmarshal(resBytes, messages) | |
if err != nil { | |
return | |
} | |
fmt.Println("utt") | |
fmt.Println(messages.Utt) | |
err = UpdateStatus(fmt.Sprintf("@%v %v", name, messages.Utt), id) | |
} | |
func loading() { | |
if len(*consumerKey) == 0 || len(*consumerSecret) == 0 { | |
fmt.Println("You must set the --consumerkey and --consumersecret flags.") | |
fmt.Println("---") | |
os.Exit(1) | |
} | |
requestToken, url, err := c.GetRequestTokenAndUrl("oob") | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("(1) Go to: " + url) | |
fmt.Println("(2) Grant access, you should get back a verification code.") | |
fmt.Println("(3) Enter that verification code here: ") | |
verificationCode := "" | |
fmt.Scanln(&verificationCode) | |
fmt.Println("loading successed.") | |
accessToken, err = c.AuthorizeToken(requestToken, verificationCode) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
func get_timeline(procLine func(b []byte)) { | |
response, err := c.Get( | |
"https://userstream.twitter.com/1.1/user.json", | |
map[string]string{}, | |
accessToken) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer response.Body.Close() | |
scanner := bufio.NewScanner(response.Body) | |
for scanner.Scan() { | |
go procLine([]byte(scanner.Text())) | |
} | |
if err = scanner.Err(); err != nil { | |
fmt.Println(err) | |
} | |
statuses := []status{} | |
_ = json.NewDecoder(response.Body).Decode(&statuses) | |
for _, s := range statuses { | |
fmt.Printf("@%v: %v\n", s.User.ScreenName, s.Text) | |
} | |
} | |
func UpdateStatus(text string, inReplyToStatusId uint64) error { | |
response, err := c.Post("https://api.twitter.com/1.1/statuses/update.json", | |
map[string]string{"status": text, "in_reply_to_status_id": fmt.Sprint(inReplyToStatusId)}, accessToken) | |
if err != nil { | |
return err | |
} | |
defer response.Body.Close() | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment