Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ _ = notifier.Send(

- *Discord*
- *Email*
- *Instagram*
- *Mailgun*
- *Microsoft Teams*
- *Plivo*
Expand All @@ -81,6 +82,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)
- Logo: [MariaLetta/free-gophers-pack](https://github.com/MariaLetta/free-gophers-pack)
- Mailgun support: [mailgun/mailgun-go](https://github.com/mailgun/mailgun-go)
- Microsoft Teams support: [atc0005/go-teams-notify](https://github.com/atc0005/go-teams-notify)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.2
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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.2 h1:BzrtTktixGHIu9Tt7dEE6diysEF9HWnXeHuoJEt2fH4=
Expand Down
69 changes: 69 additions & 0 deletions service/instagram/instagram.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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) {
i.usernames = append(i.usernames, usernames...)
}

// 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 '%s'", username)
}
} else {
cause := fmt.Errorf("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()
}
47 changes: 47 additions & 0 deletions service/instagram/usage.md
Original file line number Diff line number Diff line change
@@ -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()
}
```