-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9c0e52b
Showing
8 changed files
with
727 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package viber | ||
|
||
import "encoding/json" | ||
|
||
// Member of account details | ||
type Member struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
Avatar string `json:"avatar"` | ||
Role string `json:"role"` | ||
} | ||
|
||
// Account details | ||
type Account struct { | ||
Status int `json:"status"` | ||
StatusMessage string `json:"status_message"` | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
URI string `json:"uri"` | ||
Icon string `json:"icon"` | ||
Background string `json:"background"` | ||
Category string `json:"category"` | ||
Subcategory string `json:"subcategory"` | ||
Location struct { | ||
Lon float64 `json:"lon"` | ||
Lat float64 `json:"lat"` | ||
} `json:"location"` | ||
Country string `json:"country"` | ||
Webhook string `json:"webhook"` | ||
EventTypes []string `json:"event_types"` | ||
SubscribersCount int `json:"subscribers_count"` | ||
Members []Member `json:"members"` | ||
} | ||
|
||
// AccountInfo returns Public chat info | ||
func (v *Viber) AccountInfo() (Account, error) { | ||
var a Account | ||
b, err := v.PostData("https://chatapi.viber.com/pa/get_account_info", struct{}{}) | ||
if err != nil { | ||
return a, err | ||
} | ||
|
||
err = json.Unmarshal(b, &a) | ||
if err != nil { | ||
return a, err | ||
} | ||
|
||
if a.Status != 0 { | ||
return a, Error{Status: a.Status, StatusMessage: a.StatusMessage} | ||
} | ||
|
||
return a, err | ||
} |
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,22 @@ | ||
package viber | ||
|
||
// Error from Viber | ||
type Error struct { | ||
Status int | ||
StatusMessage string | ||
} | ||
|
||
// Error interface function | ||
func (e Error) Error() string { | ||
//return fmt.Sprintf("Viber error, status ID: %d Status: %s", id, status) | ||
return e.StatusMessage | ||
} | ||
|
||
// ErrorStatus code of Viber error, returns -1 if e is not Viber error | ||
func ErrorStatus(e interface{}) int { | ||
switch e.(type) { | ||
case Error: | ||
return e.(Error).Status | ||
} | ||
return -1 | ||
} |
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,308 @@ | ||
package viber | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
/* | ||
{ | ||
"receiver": "01234567890A=", | ||
"min_api_version": 1, | ||
"sender": { | ||
"name": "John McClane", | ||
"avatar": "http://avatar.example.com" | ||
}, | ||
"tracking_data": "tracking data", | ||
"type": "text", | ||
"text": "a message from pa" | ||
} | ||
*/ | ||
|
||
// MessageType for viber messaging | ||
type MessageType string | ||
|
||
type messageResponse struct { | ||
Status int `json:"status"` | ||
StatusMessage string `json:"status_message"` | ||
MessageToken int64 `json:"message_token"` | ||
} | ||
|
||
// Message interface for all types of viber messages | ||
type Message interface { | ||
SetReceiver(r string) | ||
SetFrom(from string) | ||
} | ||
|
||
// TextMessage for Viber | ||
type TextMessage struct { | ||
Receiver string `json:"receiver,omitempty"` | ||
From string `json:"from,omitempty"` | ||
MinAPIVersion uint `json:"min_api_version,omitempty"` | ||
Sender Sender `json:"sender"` | ||
Type MessageType `json:"type"` | ||
TrackingData string `json:"tracking_data,omitempty"` | ||
Text string `json:"text"` | ||
// "media": "http://www.images.com/img.jpg", | ||
// "thumbnail": "http://www.images.com/thumb.jpg" | ||
// "size": 10000, | ||
// "duration": 10 | ||
} | ||
|
||
// URLMessage structure | ||
type URLMessage struct { | ||
TextMessage | ||
Media string `json:"media"` | ||
} | ||
|
||
// PictureMessage structure | ||
type PictureMessage struct { | ||
TextMessage | ||
Media string `json:"media"` | ||
Thumbnail string `json:"thumbnail,omitempty"` | ||
} | ||
|
||
// VideoMessage structure | ||
type VideoMessage struct { | ||
TextMessage | ||
Media string `json:"media"` | ||
Thumbnail string `json:"thumbnail,omitempty"` | ||
Size uint `json:"size"` | ||
Duration uint `json:"duration,omitempty"` | ||
} | ||
|
||
// Button for carousel | ||
type Button struct { | ||
Columns int `json:"Columns"` | ||
Rows int `json:"Rows"` | ||
ActionType string `json:"ActionType"` | ||
ActionBody string `json:"ActionBody"` | ||
Image string `json:"Image,omitempty"` | ||
Text string `json:"Text,omitempty"` | ||
TextSize string `json:"TextSize,omitempty"` | ||
TextVAlign string `json:"TextVAlign,omitempty"` | ||
TextHAlign string `json:"TextHAlign,omitempty"` | ||
} | ||
|
||
// RichMedia for carousel | ||
type RichMedia struct { | ||
Type string `json:"Type"` | ||
ButtonsGroupColumns int `json:"ButtonsGroupColumns"` | ||
ButtonsGroupRows int `json:"ButtonsGroupRows"` | ||
BgColor string `json:"BgColor"` | ||
TrackingData string `json:"tracking_data,omitempty"` | ||
Buttons []Button `json:"Buttons"` | ||
} | ||
|
||
// RichMediaMessage / Carousel | ||
type RichMediaMessage struct { | ||
AuthToken string `json:"auth_token"` | ||
Receiver string `json:"receiver"` | ||
Type MessageType `json:"type"` | ||
MinAPIVersion int `json:"min_api_version"` | ||
RichMedia RichMedia `json:"rich_media"` | ||
} | ||
|
||
const ( | ||
TypeTextMessage = MessageType("text") | ||
TypeURLMessage = MessageType("url") | ||
TypePictureMessage = MessageType("picture") | ||
TypeVideoMessage = MessageType("video") | ||
TypeFileMessage = MessageType("file") | ||
TypeLocationMessage = MessageType("location") | ||
TypeContactMessage = MessageType("contact") | ||
TypeStickerMessage = MessageType("sticker") | ||
TypeRichMediaMessage = MessageType("rich_media") | ||
) | ||
|
||
//video, file, location, contact, sticker, carousel content | ||
|
||
func parseMsgResponse(b []byte) (token int64, err error) { | ||
var resp messageResponse | ||
if err := json.Unmarshal(b, &resp); err != nil { | ||
return 0, err | ||
} | ||
|
||
if resp.Status != 0 { | ||
return resp.MessageToken, Error{Status: resp.Status, StatusMessage: resp.StatusMessage} | ||
} | ||
|
||
return resp.MessageToken, nil | ||
} | ||
|
||
func (v *Viber) sendMessage(url string, m interface{}) (token int64, err error) { | ||
b, err := v.PostData(url, m) | ||
if err != nil { | ||
return 0, err | ||
} | ||
fmt.Println(string(b)) | ||
return parseMsgResponse(b) | ||
} | ||
|
||
// NewTextMessage viber | ||
func (v *Viber) NewTextMessage(msg string) *TextMessage { | ||
return &TextMessage{ | ||
Sender: v.Sender, | ||
Type: TypeTextMessage, | ||
Text: msg, | ||
} | ||
} | ||
|
||
// SendTextMessage to reciever, returns message token | ||
func (v *Viber) SendTextMessage(receiver string, msg string) (token int64, err error) { | ||
m := v.NewTextMessage(msg) | ||
m.Receiver = receiver | ||
return v.sendMessage("https://chatapi.viber.com/pa/send_message", m) | ||
} | ||
|
||
func (v *Viber) NewURLMessage(msg string, url string) *URLMessage { | ||
return &URLMessage{ | ||
TextMessage: TextMessage{ | ||
Sender: v.Sender, | ||
Type: TypeURLMessage, | ||
Text: msg, | ||
}, | ||
Media: url, | ||
} | ||
} | ||
|
||
// SendURLMessage to receiver, return message token | ||
func (v *Viber) SendURLMessage(receiver string, s Sender, msg string, url string) (token int64, err error) { | ||
m := v.NewURLMessage(msg, url) | ||
return v.sendMessage("https://chatapi.viber.com/pa/send_message", m) | ||
} | ||
|
||
// NewPictureMessage for viber | ||
func (v *Viber) NewPictureMessage(msg string, url string, thumbURL string) *PictureMessage { | ||
return &PictureMessage{ | ||
TextMessage: TextMessage{ | ||
Sender: v.Sender, | ||
Type: TypePictureMessage, | ||
Text: msg, | ||
}, | ||
Media: url, | ||
Thumbnail: thumbURL, | ||
} | ||
} | ||
|
||
// SendPictureMessage to receiver, returns message token | ||
// func (v *Viber) SendPictureMessage(receiver string, s Sender, msg string, url string, thumbURL string) (token int64, err error) { | ||
// m := PictureMessage{ | ||
// Message: Message{ | ||
// Receiver: receiver, | ||
// Sender: s, | ||
// Type: TypePictureMessage, | ||
// Text: msg, | ||
// }, | ||
// Media: url, | ||
// Thumbnail: thumbURL, | ||
// } | ||
|
||
// return v.sendMessage("https://chatapi.viber.com/pa/send_message", m) | ||
// } | ||
|
||
func (v *Viber) SendCarousel(receiver string) { | ||
r := RichMediaMessage{ | ||
MinAPIVersion: 2, | ||
Receiver: receiver, | ||
AuthToken: v.AppKey, | ||
Type: TypeRichMediaMessage, | ||
RichMedia: RichMedia{ | ||
Type: "rich_media", | ||
ButtonsGroupColumns: 6, | ||
ButtonsGroupRows: 6, | ||
BgColor: "#FFFFFF", | ||
}, | ||
} | ||
|
||
b1 := Button{ | ||
Columns: 6, | ||
Rows: 3, | ||
ActionType: "open-url", | ||
ActionBody: "https://aviokarte.rs/", | ||
Image: "http://nstatic.net/beta/2b5b3ff1972f61d9bcfaaddd061aa1b9.jpg", | ||
} | ||
r.RichMedia.Buttons = append(r.RichMedia.Buttons, b1) | ||
|
||
b1 = Button{ | ||
Columns: 6, | ||
Rows: 2, | ||
ActionType: "open-url", | ||
ActionBody: "https://aviokarte.rs/", | ||
Text: "Košarkaši Crvene zvezde odbranili titulu šampiona Srbije", | ||
} | ||
r.RichMedia.Buttons = append(r.RichMedia.Buttons, b1) | ||
|
||
b1 = Button{ | ||
Columns: 3, | ||
Rows: 1, | ||
ActionType: "reply", | ||
ActionBody: "ID: 21432323", | ||
Text: "<font color=#ffffff>Otvori</font>", | ||
TextSize: "large", | ||
TextVAlign: "middle", | ||
TextHAlign: "middle", | ||
Image: "https://s14.postimg.org/4mmt4rw1t/Button.png", | ||
} | ||
r.RichMedia.Buttons = append(r.RichMedia.Buttons, b1) | ||
|
||
// b2 := Button{ | ||
|
||
// Columns: 6, | ||
// Rows: 6, | ||
// ActionType: "reply", | ||
// ActionBody: "https://aviokarte.rs/", | ||
// Image: "https://aviokarte.rs/images/logo.png", | ||
// Text: "Drugi tekst", | ||
// TextSize: "large", | ||
// TextVAlign: "middle", | ||
// TextHAlign: "left", | ||
// } | ||
// r.RichMedia.Buttons = append(r.RichMedia.Buttons, b2) | ||
|
||
resp, err := v.PostData("https://chatapi.viber.com/pa/send_message", r) | ||
fmt.Println(string(resp), err) | ||
|
||
} | ||
|
||
func (v *Viber) SendPublicMessage(from string, m Message) (token int64, err error) { | ||
m.SetFrom(from) | ||
return v.sendMessage("https://chatapi.viber.com/pa/post", m) | ||
} | ||
|
||
func (v *Viber) SendMessage(to string, m Message) (token int64, err error) { | ||
m.SetReceiver(to) | ||
return v.sendMessage("https://chatapi.viber.com/pa/send_message", m) | ||
} | ||
|
||
func (m *TextMessage) SetReceiver(r string) { | ||
m.Receiver = r | ||
} | ||
|
||
func (m *URLMessage) SetReceiver(r string) { | ||
m.Receiver = r | ||
} | ||
|
||
func (m *PictureMessage) SetReceiver(r string) { | ||
m.Receiver = r | ||
} | ||
|
||
func (m *VideoMessage) SetReceiver(r string) { | ||
m.Receiver = r | ||
} | ||
|
||
func (m *TextMessage) SetFrom(from string) { | ||
m.From = from | ||
} | ||
|
||
func (m *URLMessage) SetFrom(from string) { | ||
m.From = from | ||
} | ||
|
||
func (m *PictureMessage) SetFrom(from string) { | ||
m.From = from | ||
} | ||
|
||
func (m *VideoMessage) SetFrom(from string) { | ||
m.From = from | ||
} |
Oops, something went wrong.