Created
January 18, 2016 11:12
-
-
Save taiko19xx/88eeb24305dcc2290597 to your computer and use it in GitHub Desktop.
チャットワークのメッセージをRedisに突っ込むやつ
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 ( | |
"encoding/json" | |
"github.com/garyburd/redigo/redis" | |
"io/ioutil" | |
"log" | |
"net/http" | |
) | |
type accountData struct { | |
AccountID int64 `json:"account_id"` | |
Name string `json:"name"` | |
AvatarImageUrl string `json:"avatar_image_url"` | |
} | |
type messageData struct { | |
MessageId int64 `json:"message_id"` | |
Accont accountData `json:"account"` | |
Body string `json:"body"` | |
SendTime int64 `json:"send_time"` | |
UpdateTime int64 `json:"update_time"` | |
} | |
type JsonData struct { | |
Message []messageData | |
} | |
func main() { | |
req, _ := http.NewRequest("GET", "https://api.chatwork.com/v1/rooms/room_id/messages", nil) | |
req.Header.Set("X-ChatWorkToken", "token") | |
client := new(http.Client) | |
resp, _ := client.Do(req) | |
defer resp.Body.Close() | |
byteArray, _ := ioutil.ReadAll(resp.Body) | |
if string(byteArray) == "" { | |
log.Fatal("Empty!") | |
} | |
messageKey := make([]messageData, 0) | |
err := json.Unmarshal(byteArray, &messageKey) | |
if err != nil { | |
log.Fatal(err) | |
} | |
conn, _ := redis.Dial("tcp", "redis_Server") | |
defer conn.Close() | |
for _, value := range messageKey { | |
log.Println(value.MessageId) | |
b, _ := json.Marshal(value) | |
conn.Do("SET", value.MessageId, b) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment