From 95ad25558719ea06811d6b8057b8f8242c18f967 Mon Sep 17 00:00:00 2001 From: alankan886 Date: Fri, 12 Feb 2021 22:43:42 -0500 Subject: [PATCH 1/5] feat(service): Add Instagram service --- service/instagram/instagram.go | 71 ++++++++++++++++++++++++++++++++++ service/instagram/usage.md | 47 ++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 service/instagram/instagram.go create mode 100644 service/instagram/usage.md diff --git a/service/instagram/instagram.go b/service/instagram/instagram.go new file mode 100644 index 00000000..971383a2 --- /dev/null +++ b/service/instagram/instagram.go @@ -0,0 +1,71 @@ +package instagram + +import ( + "fmt" + "log" + + "github.com/ahmdrz/goinsta/v2" + "github.com/pkg/errors" +) + +// Instagram struct holds necessary data to communicate with the unofficial Instagram API. +type Instagram struct { + client *goinsta.Instagram + usernames []string +} + +// New returns a new instance of a Instagram notification service. +func New(username, password string) (*Instagram, error) { + client := goinsta.New(username, password) + err := client.Login() + if err != nil { + return nil, err + } + + insta := &Instagram{ + client: client, + usernames: []string{}, + } + + return insta, nil +} + +// AddReceivers takes Instagram usernames and adds them to the internal usernames list. +// The Send method will send a given message to all those users. +func (i *Instagram) AddReceivers(usernames ...string) { + for _, username := range usernames { + i.usernames = append(i.usernames, username) + } +} + +// Send takes a message subject and a message body and sends them to all previously set users. +func (i Instagram) Send(subject, message string) error { + fullMessage := subject + "\n" + message + for _, username := range i.usernames { + // Search finds users with from most similar to least similar usernames + result, err := i.client.Search.User(username) + if err != nil { + log.Println(err.Error()) + return err + } + user := result.Users[0] + if user.Username == username { + // Doc says to use Conversation.Send for messages after initial message. + // But seems like Inbox.New works for further messages, and Instagram.Conversation doesn't show any conversations. + err = i.client.Inbox.New(&user, fullMessage) + if err != nil { + return errors.Wrapf(err, "failed to send message to Instagram user '%d'", username) + } + } else { + cause := errors.New(fmt.Sprintf("the closest username found is '%s'", user.Username)) + return errors.Wrapf(cause, "failed to find the user with username '%s'", username) + } + } + + return nil +} + +// Logout closes the current session to the API +func (i *Instagram) Logout() error { + return i.client.Logout() +} diff --git a/service/instagram/usage.md b/service/instagram/usage.md new file mode 100644 index 00000000..463abcd0 --- /dev/null +++ b/service/instagram/usage.md @@ -0,0 +1,47 @@ +# Instagram Usage + +Ensure that you have already navigated to your GOPATH and installed the following packages: + +* `go get -u github.com/nikoksr/notify` +* `go get github.com/ahmdrz/goinsta/v2` - You might need this one too + + +## Sample Code + +```go +package main + +import ( + "github.com/nikoksr/notify" + "github.com/nikoksr/notify/service/instagram" +) + +func main() { + + notifier := notify.New() + + // Assuming you already have an Instagram account + // Provide your Instagram username and password + service := instagram.New("Username", "Password") + + // Passing usernames as receivers for our messages. + service.AddReceivers("friend1", "friend2") + + // Tell our notifier to use the Instagram service. You can repeat the above process + // for as many services as you like and just tell the notifier to use them. + notifier.UseServices(service) + + // Send a message + err := notifier.Send( + "Hello\n", + "I am a bot written in Go!", + ) + + if err != nil { + panic(err) + } + + // This is Instagram specific, logout after you are done to close the session. + service.Logout() +} +``` \ No newline at end of file From 1a21c2089b7a4f52bcc7af01ffbb56da0b6c2d7b Mon Sep 17 00:00:00 2001 From: alankan886 Date: Fri, 12 Feb 2021 22:56:40 -0500 Subject: [PATCH 2/5] fix: fix format token from %d to %s --- service/instagram/instagram.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/instagram/instagram.go b/service/instagram/instagram.go index 971383a2..c9ec79f3 100644 --- a/service/instagram/instagram.go +++ b/service/instagram/instagram.go @@ -54,7 +54,7 @@ func (i Instagram) Send(subject, message string) error { // But seems like Inbox.New works for further messages, and Instagram.Conversation doesn't show any conversations. err = i.client.Inbox.New(&user, fullMessage) if err != nil { - return errors.Wrapf(err, "failed to send message to Instagram user '%d'", username) + return errors.Wrapf(err, "failed to send message to Instagram user '%s'", username) } } else { cause := errors.New(fmt.Sprintf("the closest username found is '%s'", user.Username)) From b9a9569d6f76eaa81002a85c181030c1f6550e3f Mon Sep 17 00:00:00 2001 From: alankan886 Date: Fri, 12 Feb 2021 22:59:03 -0500 Subject: [PATCH 3/5] docs: update README --- README.md | 2 ++ go.mod | 1 + go.sum | 2 ++ 3 files changed, 5 insertions(+) diff --git a/README.md b/README.md index b420d5e3..5874a2d7 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ _ = notifier.Send( - *Discord* - *Email* +- *Instagram* - *Microsoft Teams* - *Slack* - *Telegram* @@ -77,6 +78,7 @@ _ = notifier.Send( - Discord support: [bwmarrin/discordgo](https://github.com/bwmarrin/discordgo) - Email support: [jordan-wright/email](https://github.com/jordan-wright/email) +- Instagram support: [ahmdrz/goinsta](https://github.com/ahmdrz/goinsta) - Microsoft Teams support: [atc0005/go-teams-notify](https://github.com/atc0005/go-teams-notify) - Slack support: [slack-go/slack](https://github.com/slack-go/slack) - Telegram support: [go-telegram-bot-api/telegram-bot-api](https://github.com/go-telegram-bot-api/telegram-bot-api) diff --git a/go.mod b/go.mod index d0219b27..b018750b 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/nikoksr/notify go 1.15 require ( + github.com/ahmdrz/goinsta/v2 v2.4.5 github.com/atc0005/go-teams-notify/v2 v2.4.2 github.com/bwmarrin/discordgo v0.23.1 github.com/cschomburg/go-pushbullet v0.0.0-20171206132031-67759df45fbb diff --git a/go.sum b/go.sum index e93ca52b..5b8e219f 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/ahmdrz/goinsta/v2 v2.4.5 h1:yiNEpTNEx/VWlcCq93miWUx6d2uxdEyklwljsZaiD5I= +github.com/ahmdrz/goinsta/v2 v2.4.5/go.mod h1:XUEELCWd3hVSqADejqxTa8Uc7Yto9cb6a6HrfkGwVf8= github.com/atc0005/go-teams-notify/v2 v2.4.2 h1:3KQ8e8LN4kwuWWHpnCNTXA15JdLRaNWcCS1VS0a4SO0= github.com/atc0005/go-teams-notify/v2 v2.4.2/go.mod h1:BSlh1HBcgWcGoNM3Abm36WMPcj+k8Wf0ZLZx6lBx2qk= github.com/bwmarrin/discordgo v0.23.1 h1:xlK4/69bpl/VSoCYaKe3BOc9j1HkNopoRdCppRYu8dk= From 5d41f42a6bd980aa1149c54db3f17a5f4b80aa98 Mon Sep 17 00:00:00 2001 From: alankan886 Date: Fri, 12 Feb 2021 23:16:18 -0500 Subject: [PATCH 4/5] fix: fix lint issue --- service/instagram/instagram.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/service/instagram/instagram.go b/service/instagram/instagram.go index c9ec79f3..8a6a0c02 100644 --- a/service/instagram/instagram.go +++ b/service/instagram/instagram.go @@ -33,9 +33,7 @@ func New(username, password string) (*Instagram, error) { // AddReceivers takes Instagram usernames and adds them to the internal usernames list. // The Send method will send a given message to all those users. func (i *Instagram) AddReceivers(usernames ...string) { - for _, username := range usernames { - i.usernames = append(i.usernames, username) - } + i.usernames = append(i.usernames, usernames...) } // Send takes a message subject and a message body and sends them to all previously set users. From 2974df442222676de658923795b7e27144297370 Mon Sep 17 00:00:00 2001 From: alankan886 Date: Fri, 12 Feb 2021 23:27:49 -0500 Subject: [PATCH 5/5] refactor: refactor error creation with simpler operation --- service/instagram/instagram.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/instagram/instagram.go b/service/instagram/instagram.go index 8a6a0c02..96b62260 100644 --- a/service/instagram/instagram.go +++ b/service/instagram/instagram.go @@ -55,7 +55,7 @@ func (i Instagram) Send(subject, message string) error { return errors.Wrapf(err, "failed to send message to Instagram user '%s'", username) } } else { - cause := errors.New(fmt.Sprintf("the closest username found is '%s'", user.Username)) + cause := fmt.Errorf("the closest username found is '%s'", user.Username) return errors.Wrapf(cause, "failed to find the user with username '%s'", username) } }