Skip to content

Commit

Permalink
Merge pull request #6 from go-microbot/feature/v0.6.0
Browse files Browse the repository at this point in the history
v0.6.0
  • Loading branch information
MonkeyBuisness authored Dec 3, 2020
2 parents 852eb27 + c35d775 commit 50e2169
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 4 deletions.
2 changes: 2 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ type Bot interface {
SendRichMediaMessage(ctx context.Context, req apiModels.SendRichMediaMessageRequest) (*apiModels.MessageResponse, error)
// https://developers.viber.com/docs/api/rest-bot-api/#get-user-details.
GetUserDetails(ctx context.Context, req apiModels.UserIDRequest) (*apiModels.UserDetailsResponse, error)
// https://developers.viber.com/docs/api/rest-bot-api/#get-online.
GetOnline(ctx context.Context, req apiModels.UserIDsRequest) (*apiModels.GetOnlineResponse, error)
}
28 changes: 28 additions & 0 deletions api/get_online.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package api

import (
"context"
"net/http"

apiModels "github.com/go-microbot/viber/api/models"
)

// GetOnline represents method to fetch the online status of a given subscribed account members.
// The API supports up to 100 user ID per request and
// those users must be subscribed to the account.
func (api *ViberAPI) GetOnline(ctx context.Context, req apiModels.UserIDsRequest) (*apiModels.GetOnlineResponse, error) {
resp, err := api.NewRequest("get_online").
Method(http.MethodPost).
Body(NewJSONBody(req)).
Do(ctx)
if err != nil {
return nil, err
}

var onlineResp apiModels.GetOnlineResponse
if err := resp.Decode(&onlineResp); err != nil {
return nil, err
}

return &onlineResp, err
}
37 changes: 37 additions & 0 deletions api/get_online_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package api

import (
"context"
"testing"

apiModels "github.com/go-microbot/viber/api/models"
"github.com/go-microbot/viber/models"
"github.com/stretchr/testify/require"
)

type getOnline struct{}

func (h getOnline) Test(ctx context.Context, t *testing.T) context.Context {
memberID := ctx.Value(chatMemberIDCtxKey)
require.NotNil(t, memberID)

online, err := testAPI.GetOnline(ctx, apiModels.UserIDsRequest{
IDs: []string{memberID.(string)},
})
require.NoError(t, err)
require.NotNil(t, online)
require.NotEmpty(t, online.Users)
var found bool
for i := range online.Users {
if online.Users[i].ID == memberID.(string) {
require.True(t,
online.Users[i].OnlineStatus == models.UserOnlineStatusOnline ||
online.Users[i].OnlineStatus == models.UserOnlineStatusOffline)
found = true
break
}
}
require.True(t, found)

return ctx
}
23 changes: 23 additions & 0 deletions api/mocks/bot.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions api/models/get_online_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package models

import "github.com/go-microbot/viber/models"

// GetOnlineResponse represents `get_online` response model.
type GetOnlineResponse struct {
MessageResponse
Users []GetOnlineUser `json:"users"`
}

// GetOnlineUser represents user online status.
type GetOnlineUser struct {
// Unique Viber user id.
ID string `json:"id"`
// Online status code.
// 0 for online,
// 1 for offline,
// 2 for undisclosed - user set Viber to hide status,
// 3 for try later - internal error,
// 4 for unavailable - not a Viber user/unsubscribed/unregistered.
OnlineStatus models.UserOnlineStatus `json:"online_status"`
// Online status message.
OnlineStatusMessage string `json:"online_status_message"`
}
6 changes: 6 additions & 0 deletions api/models/user_id_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ type UserIDRequest struct {
// Unique Viber user id. Required. Subscribed valid user ID.
ID string `json:"id"`
}

// UserIDsRequest represents user IDs request model.
type UserIDsRequest struct {
// Unique Viber user IDs. Required. 100 IDs per request.
IDs []string `json:"ids"`
}
4 changes: 4 additions & 0 deletions api/viber_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ func TestViberAPI_Integration(t *testing.T) {
name: "getUserDetails",
testHandler: getUserDetails{},
},
{
name: "getOnline",
testHandler: getOnline{},
},
}
for i := range testCases {
tc := &testCases[i]
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-bot/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/go-microbot/viber/examples/simple-bot

go 1.15

require github.com/go-microbot/viber v0.2.0
require github.com/go-microbot/viber v0.5.0
4 changes: 2 additions & 2 deletions examples/simple-bot/go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-microbot/viber v0.2.0 h1:/yWY7VcyqEb58mNcV/6xK4p6qODu9e++1yJRmWbMxzI=
github.com/go-microbot/viber v0.2.0/go.mod h1:zxiRubhhfIRz+68mddc2nUqXIACMyZjRhKWiKwc33Mc=
github.com/go-microbot/viber v0.5.0 h1:pf0wk5UPivnUBU78Zk2j6daoCQ6jM4LkIC2LCUFuPAg=
github.com/go-microbot/viber v0.5.0/go.mod h1:j05aJKGLrQd1LA7xVnBijhQDH34i81VPeNjmceuW5xg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
6 changes: 5 additions & 1 deletion examples/simple-bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ func main() {
// start listening.
go myBot.WaitForUpdates(bot.NewWebhookStrategy(bot.WebhookConfig{
ServeURL: "localhost:8443", // server to catch Viber requests.
// if you want to validate each callback signature add these parameters as well.
// More info: https://developers.viber.com/docs/api/rest-bot-api/#callbacks.
VerifySignature: true,
SignatureKey: token,
}))

// setup Webhook.
go func() {
whResp, err := botAPI.SetWebhook(context.Background(), apiModels.SetWebhookRequest{
URL: "https://55442d01e546.ngrok.io", // use your website URL (SSL required).
URL: "https://03322284e668.ngrok.io", // use your website URL (SSL required).
})
if err != nil {
panic(err)
Expand Down
13 changes: 13 additions & 0 deletions models/user_online_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package models

// There are available user online statuses.
const (
UserOnlineStatusOnline UserOnlineStatus = 0
UserOnlineStatusOffline UserOnlineStatus = 1
UserOnlineStatusUndisclosed UserOnlineStatus = 2
UserOnlineStatusInternalError UserOnlineStatus = 3
UserOnlineStatusUnavailable UserOnlineStatus = 4
)

// UserOnlineStatus represents user online status.
type UserOnlineStatus int64

0 comments on commit 50e2169

Please sign in to comment.