Skip to content

Commit 3a7d66f

Browse files
authored
Merge pull request #6 from line/v2
V2
2 parents 8635ae8 + a20a8ef commit 3a7d66f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+4045
-1958
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
language: go
22
go:
33
- 1.6
4+
- 1.7
45
- tip
56
sudo: false

README.md

Lines changed: 17 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22

33
[![Build Status](https://travis-ci.org/line/line-bot-sdk-go.svg?branch=master)](https://travis-ci.org/line/line-bot-sdk-go)
44

5-
SDK of the LINE BOT API Trial for Go
5+
Go SDK for the LINE Messaging API
6+
7+
8+
## About LINE Messaging API
9+
10+
Please refer to the official api documents for details.
11+
12+
en: https://devdocs.line.me/en/
13+
14+
ja: https://devdocs.line.me/ja/
15+
616

717
## Installation ##
818

@@ -18,7 +28,7 @@ import (
1828
)
1929

2030
func main() {
21-
bot, err := linebot.NewClient(<Channel ID>, "<Channel Secret>", "<MID>")
31+
bot, err := linebot.New("<channel secret>", "<channel access token>")
2232
...
2333
}
2434

@@ -28,176 +38,14 @@ func main() {
2838

2939
```go
3040
client := &http.Client{}
31-
bot, err := linebot.NewClient(<Channel ID>, "<Channel Secret>", "<MID>", linebot.WithHTTPClient(client))
41+
bot, err := linebot.New("<channel secret>", "<channel accsss token>", linebot.WithHTTPClient(client))
3242
...
3343
```
3444

35-
## Usage ##
36-
37-
### Sending messages ###
38-
39-
Send a text message, image, video, audio, location, or sticker to the mids.
40-
41-
- [https://developers.line.me/bot-api/api-reference#sending_message](https://developers.line.me/bot-api/api-reference#sending_message)
42-
43-
```go
44-
// send text
45-
res, err := bot.SendText([]string{"<target user's MID>"}, "Hello, world!")
46-
47-
// send image
48-
res, err := bot.SendImage([]string{"<target user's MID>"}, "http://example.com/image.jpg", "http://example.com/image_preview.jpg")
49-
50-
// send video
51-
res, err := bot.SendVideo([]string{"<target user's MID>"}, "http://example.com/video.mp4", "http://example.com/image_preview.jpg")
52-
53-
// send audio
54-
res, err := bot.SendAudio([]string{"<target user's MID>"}, "http://example.com/audio.mp3", 2000)
55-
56-
// send location
57-
res, err := bot.SendLocation([]string{"<target user's MID>"}, "location label", "tokyo shibuya-ku", 35.661777, 139.704051)
58-
59-
// send sticker
60-
res, err := bot.SendSticker([]string{"<target user's MID>"}, 1, 1, 100)
61-
```
62-
63-
### Sending multiple messages ###
64-
65-
The `multiple_message` method allows you to use the _Sending multiple messages API_.
66-
67-
- [https://developers.line.me/bot-api/api-reference#sending_multiple_messages](https://developers.line.me/bot-api/api-reference#sending_multiple_messages)
68-
69-
```go
70-
res, err := bot.NewMultipleMessage().
71-
AddText("Hello,").
72-
AddText("world!").
73-
AddImage("http://example.com/image.jpg", "http://example.com/image_preview.jpg")
74-
AddVideo("http://example.com/video.mp4", "http://example.com/image_preview.jpg")
75-
AddAudio("http://example.com/audio.mp3", 2000)
76-
AddLocation("Location label", "tokyo shibuya-ku", 35.61823286112982, 139.72824096679688).
77-
AddSticker(1, 1, 100).
78-
Send([]string{"<target user's MID>"})
79-
```
80-
81-
### Sending rich messages ###
82-
83-
The `rich_message` method allows you to use the _Sending rich messages API_.
84-
85-
- [https://developers.line.me/bot-api/api-reference#sending_rich_content_message](https://developers.line.me/bot-api/api-reference#sending_rich_content_message)
86-
87-
```go
88-
res, err := bot.NewRichMessage(1040).
89-
SetAction("MANGA", "manga", "https://store.line.me/family/manga/en").
90-
SetListener("MANGA", 0, 0, 520, 520).
91-
SetAction("MUSIC", "music", "https://store.line.me/family/music/en").
92-
SetListener("MUSIC", 520, 0, 520, 520).
93-
Send([]string{"<target user's MID>"}, "https://example.com/rich-image/foo", "This is a alt text.")
94-
```
95-
96-
### Receiving messages ###
45+
## Requirements
9746

98-
The following utility method allows you to easily process messages sent from the BOT API platform via a Callback URL.
47+
This library requires Go 1.6 or later.
9948

100-
- [https://developers.line.me/bot-api/api-reference#receiving_messages](https://developers.line.me/bot-api/api-reference#receiving_messages)
49+
## LICENSE
10150

102-
```go
103-
http.HandleFunc("/callback", func(w http.ResponseWriter, req *http.Request) {
104-
received, err := bot.ParseRequest(req)
105-
if err != nil {
106-
if err == linebot.ErrInvalidSignature {
107-
w.WriteHeader(400)
108-
} else {
109-
w.WriteHeader(500)
110-
}
111-
return
112-
}
113-
for _, result := range received.Results {
114-
content := result.Content()
115-
if content != nil && content.IsMessage && content.ContentType == linebot.ContentTypeText {
116-
text, err := content.TextContent()
117-
_, err := bot.SendText([]string{content.From}, "OK " + text.Text)
118-
if err != nil {
119-
log.Println(err)
120-
}
121-
}
122-
}
123-
})
124-
if err := http.ListenAndServe(":8080", nil); err != nil {
125-
log.Fatal(err)
126-
}
127-
```
128-
129-
### Getting message content ###
130-
131-
Retrieve the original file which was sent by user.
132-
133-
- [https://developers.line.me/bot-api/api-reference#getting_message_content](https://developers.line.me/bot-api/api-reference#getting_message_content)
134-
135-
```go
136-
http.HandleFunc("/callback", func(w http.ResponseWriter, req *http.Request) {
137-
...
138-
for _, result := range received.Results {
139-
content := result.Content()
140-
if content != nil && content.IsMessage && content.ContentType == linebot.ContentTypeImage {
141-
res, err := bot.GetMessageContent(content)
142-
if err != nil {
143-
return
144-
}
145-
defer res.Content.Close()
146-
image, err := jpeg.Decode(res.Content)
147-
if err != nil {
148-
return
149-
}
150-
log.Printf("image %v", image.Bounds())
151-
}
152-
}
153-
})
154-
```
155-
156-
### Getting previews of message content ###
157-
158-
Retrieve the preview image file which was sent by user.
159-
160-
- [https://developers.line.me/bot-api/api-reference#getting_message_content_preview](https://developers.line.me/bot-api/api-reference#getting_message_content_preview)
161-
162-
```go
163-
http.HandleFunc("/callback", func(w http.ResponseWriter, req *http.Request) {
164-
...
165-
for _, result := range received.Results {
166-
content := result.Content()
167-
if content != nil && content.IsMessage && content.ContentType == linebot.MessageContentTypeImage {
168-
res, err := bot.GetMessageContentPreview(content)
169-
if err != nil {
170-
return
171-
}
172-
defer res.Content.Close()
173-
image, err := jpeg.Decode(res.Content)
174-
if err != nil {
175-
return
176-
}
177-
log.Printf("image %v", image.Bounds())
178-
}
179-
}
180-
})
181-
```
182-
183-
### Getting user profile information ###
184-
185-
You can retrieve the user profile information by specifying the mid.
186-
187-
- [https://developers.line.me/bot-api/api-reference#getting_user_profile_information](https://developers.line.me/bot-api/api-reference#getting_user_profile_information)
188-
189-
```go
190-
http.HandleFunc("/callback", func(w http.ResponseWriter, req *http.Request) {
191-
...
192-
for _, result := range received.Results {
193-
content := result.Content()
194-
if content != nil {
195-
result, err := bot.GetUserProfile([]string{content.From})
196-
if err != nil {
197-
return
198-
}
199-
log.Printf("profile: %v", result)
200-
}
201-
}
202-
})
203-
```
51+
See LICENSE.txt

examples/echo_bot/main.go

Lines changed: 0 additions & 63 deletions
This file was deleted.

examples/echo_bot/server.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2016 LINE Corporation
2+
//
3+
// LINE Corporation licenses this file to you under the Apache License,
4+
// version 2.0 (the "License"); you may not use this file except in compliance
5+
// with the License. You may obtain a copy of the License at:
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
// License for the specific language governing permissions and limitations
13+
// under the License.
14+
15+
package main
16+
17+
import (
18+
"log"
19+
"net/http"
20+
"os"
21+
22+
"github.com/line/line-bot-sdk-go/linebot"
23+
)
24+
25+
func main() {
26+
bot, err := linebot.New(
27+
os.Getenv("CHANNEL_SECRET"),
28+
os.Getenv("CHANNEL_TOKEN"),
29+
)
30+
if err != nil {
31+
log.Fatal(err)
32+
}
33+
34+
// Setup HTTP Server for receiving requests from LINE platform
35+
http.HandleFunc("/callback", func(w http.ResponseWriter, req *http.Request) {
36+
events, err := bot.ParseRequest(req)
37+
if err != nil {
38+
if err == linebot.ErrInvalidSignature {
39+
w.WriteHeader(400)
40+
} else {
41+
w.WriteHeader(500)
42+
}
43+
return
44+
}
45+
for _, event := range events {
46+
if event.Type == linebot.EventTypeMessage {
47+
switch message := event.Message.(type) {
48+
case *linebot.TextMessage:
49+
source := event.Source
50+
if source.Type == linebot.EventSourceTypeUser {
51+
if _, err = bot.PushMessage(source.UserID, linebot.NewTextMessage(message.Text)).Do(); err != nil {
52+
log.Print(err)
53+
}
54+
}
55+
}
56+
}
57+
}
58+
})
59+
if err := http.ListenAndServe(":"+os.Getenv("PORT"), nil); err != nil {
60+
log.Fatal(err)
61+
}
62+
}

0 commit comments

Comments
 (0)