-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from yoelsusanto/service/line
feat(service): Add line service
- Loading branch information
Showing
5 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package line | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/line/line-bot-sdk-go/linebot" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// Line struct holds info about client and destination ID for communicating with line API | ||
type Line struct { | ||
client *linebot.Client | ||
receiverIDs []string | ||
} | ||
|
||
// New creates a new instance of Line notifier service | ||
// For more info about line api credential: | ||
// -> https://github.com/line/line-bot-sdk-go | ||
func New(channelSecret, channelAccessToken string) (*Line, error) { | ||
bot, err := linebot.New(channelSecret, channelAccessToken) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
l := &Line{ | ||
client: bot, | ||
} | ||
|
||
return l, nil | ||
} | ||
|
||
// AddReceivers receives user, group or room IDs then add them to internal receivers list | ||
func (l *Line) AddReceivers(receiverIDs ...string) { | ||
l.receiverIDs = append(l.receiverIDs, receiverIDs...) | ||
} | ||
|
||
// Send receives message subject and body then sends it to all receivers set previously | ||
// Subject will be on the first line followed by message on the next line | ||
func (l *Line) Send(ctx context.Context, subject, message string) error { | ||
lineMessage := &linebot.TextMessage{ | ||
Text: subject + "\n" + message, | ||
} | ||
|
||
for _, receiverID := range l.receiverIDs { | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
default: | ||
_, err := l.client.PushMessage(receiverID, lineMessage).WithContext(ctx).Do() | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to send message to LINE contact '%s'", receiverID) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Line Usage | ||
|
||
Install notifier using: | ||
|
||
* `go get -u github.com/nikoksr/notify` | ||
|
||
|
||
## Sample Code | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/nikoksr/notify" | ||
"github.com/nikoksr/notify/service/line" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
func main() { | ||
// Assuming you already have a line messaging API credential | ||
// Provide your channel secret and access token | ||
lineService, _ := line.New("channelSecret", "channelAccessToken") | ||
|
||
// Add id from various receivers | ||
// You can try to use your own line id for testing | ||
lineService.AddReceivers("userID1", "groupID1") | ||
|
||
notifier := notify.New() | ||
|
||
// Tell our notifier to use the line service. You can repeat the above process | ||
// for as many services as you like and just tell the notifier to use them. | ||
notifier.UseServices(lineService) | ||
|
||
// Send a message | ||
err := notifier.Send( | ||
context.Background(), | ||
"Welcome", | ||
"I am a bot written in Go!", | ||
) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
``` |