instabot

package module
v0.0.1-alpha Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 15, 2021 License: MIT Imports: 9 Imported by: 0

README

Instagram Messaging API GO SDK

Build Status codecov GoDoc Go Report Card CodeQL

Run on Repl.it

Introduction

Instabot, Instagram Messaging API GO SDK makes it easy to work with instagram messaging API. It uses Instagram messaging API latest version - v11.0

Requirements

Instabot requires Go 1.13 or later.

Installation

$ go get -u github.com/BackAged/instabot

Instabot configuration

import (
	"context"
	"fmt"
	"log"

	"github.com/BackAged/instabot"
)

func main() {
    // instantiating instabot.
	bot, err := instabot.New("your_instagram_business_account_page_access_token")
    ...

    // instantiating with http.Client
    bot, err := instabot.New(
        "your_instagram_business_account_page_access_token",
        instabot.WithHTTPClient(yourHttpClient)
    )
    ...

    // instantiating with mock api server
    bot, err := instabot.New(
        "your_instagram_business_account_page_access_token",
        instabot.APIEndpointBase("http://your_mock_api_server.com")
    )
    ...

}

Example

import (
	"context"
	"fmt"
	"log"

	"github.com/BackAged/instabot"
)

func main() {
    // See examples directory for more example.
    
    // instantiating instabot.
	bot, err := instabot.New("your_instagram_business_account_page_access_token")
    ...

    
    // Send text message.
	_, err = bot.SendMessage(
		context.Background(),
		"instagram_user_id_you_want_to_send_message_to",
		instabot.NewTextMessage("hello"),
	)
    ...

    // Set icebreakers
    _, err = bot.SetIceBreakers(
		context.Background(),
		[]*instabot.IceBreaker{
			instabot.NewIceBreaker("frequently asked question 1", "user payload"),
			instabot.NewIceBreaker("frequently asked question 2", "user payload"),
			instabot.NewIceBreaker("frequently asked question 3", "user payload"),
			instabot.NewIceBreaker("frequently asked question 4", "user payload"),
		},
	)
    ...

    // Get user profile.
	profile, err := bot.GetUserProfile(
		context.Background(),
		"instagram_user_id_you_want_to_get_profile",
	)
    ...
}

Documentation

Index

Constants

View Source
const (
	Platform   string = "instagram"
	APIVersion string = "v11.0"
)

instabot const values.

Variables

View Source
var (
	APIEndpointBase             = "https://graph.facebook.com"
	APIEndpointSendMessage      = fmt.Sprintf("/%s/me/messages", APIVersion)
	APIEndpointMessengerProfile = fmt.Sprintf("/%s/me/messenger_profile", APIVersion)
	GetAPIEndpointUserProfile   = func(instagramUserID string) string {
		return fmt.Sprintf("/%s/%s", APIVersion, instagramUserID)
	}
)

instagram messaging api endpoints.

View Source
var (
	// ErrMissingPageAccessToken happens when instantiating instabot
	// with empty page access token.
	ErrMissingPageAccessToken = errors.New("missing page access token")
)

Functions

This section is empty.

Types

type APIError

type APIError struct {
	Message   string `json:"message"`
	Type      string `json:"type"`
	Code      int32  `json:"code"`
	SubCode   int32  `json:"error_subcode"`
	FbTraceID string `json:"fbtrace_id"`
}

APIError defines error received from the api.

type Button

type Button interface {
	Type() ButtonType
}

Button defines button type.

type ButtonType

type ButtonType string

ButtonType defines button type.

const (
	ButtonTypeURL      ButtonType = ButtonType("web_url")
	ButtonTypePostBack ButtonType = ButtonType("postback")
	ButtonTypeCall     ButtonType = ButtonType("phone_number")
	ButtonTypeLogin    ButtonType = ButtonType("account_link")
	ButtonTypeLogOut   ButtonType = ButtonType("account_unlink")
	ButtonTypeGamePlay ButtonType = ButtonType("game_play")
)

all button type. https://developers.facebook.com/docs/messenger-platform/send-messages/buttons

type CallButton

type CallButton struct {
	Title       string
	PhoneNumber string
	// contains filtered or unexported fields
}

CallButton defines call button. https://developers.facebook.com/docs/messenger-platform/send-messages/buttons#call

func NewCallButton

func NewCallButton(title string, PhoneNumber string) *CallButton

NewCallButton returns a new call button.

func (*CallButton) MarshalJSON

func (b *CallButton) MarshalJSON() ([]byte, error)

MarshalJSON returns json of the button.

func (*CallButton) Type

func (b *CallButton) Type() ButtonType

Type returns button type.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client defines instabot.

func New

func New(pageAccessToken string, options ...ClientOption) (*Client, error)

New returns a new bot client instance.

func (*Client) GetUserProfile

func (c *Client) GetUserProfile(ctx context.Context, instagramUserID string) (*GetUserProfileResponse, error)

GetUserProfile fetches user profile by instagram user id. https://developers.facebook.com/docs/messenger-platform/instagram/features/user-profile#user-profile-api

func (*Client) SendMessage

func (c *Client) SendMessage(ctx context.Context, recipient string, message Message) (*SendMessageResponse, error)

SendMessage sends message by calling instagram api. https://developers.facebook.com/docs/messenger-platform/instagram/features/send-message#send-api

func (*Client) SetIceBreakers

func (c *Client) SetIceBreakers(ctx context.Context, iceBreakers []*IceBreaker) (*SetIceBreakersResponse, error)

SetIceBreakers sets a instagram account ice breakers. https://developers.facebook.com/docs/messenger-platform/instagram/features/ice-breakers#setting-ice-breakers

type ClientOption

type ClientOption func(*Client) error

ClientOption defines optional argument for new client construction.

func WithEndpointBase

func WithEndpointBase(endpointBase string) ClientOption

WithEndpointBase sets client base endpoint.

func WithHTTPClient

func WithHTTPClient(c *http.Client) ClientOption

WithHTTPClient sets client http client.

type DeleteIceBreakersResponse

type DeleteIceBreakersResponse struct {
	Result string `json:"result"`
}

DeleteIceBreakersResponse defines delete ice breaker api success response.

type ErrorResponse

type ErrorResponse struct {
	StatusCode int      `json:"status_code,omitempty"`
	APIError   APIError `json:"error"`
}

ErrorResponse defines error response received from instagram api.

func (ErrorResponse) Error

func (e ErrorResponse) Error() string

Error return error messsage.

type GenericTemplateElement

type GenericTemplateElement struct {
	Title         string
	Subtitle      string
	ImageURL      string
	DefaultAction *TemplateDefaultAction
	Buttons       []Button
}

GenericTemplateElement defines generic template element. https://developers.facebook.com/docs/messenger-platform/instagram/features/generic-template#elements

func NewGenericTemplateElement

func NewGenericTemplateElement(title string, opts ...GenericTemplateElementOption) *GenericTemplateElement

NewGenericTemplateElement returns new GenericTemplateElement.

func (*GenericTemplateElement) MarshalJSON

func (e *GenericTemplateElement) MarshalJSON() ([]byte, error)

MarshalJSON marshal generic template element to JSON.

type GenericTemplateElementOption

type GenericTemplateElementOption func(*GenericTemplateElement)

GenericTemplateElementOption defines new GenericTemplateElement instantiation optional argument.

func WithTemplateButtons

func WithTemplateButtons(buttons []Button) GenericTemplateElementOption

WithTemplateButtons sets buttons of a GenericTemplateElement, a maximum of 3 buttons per element is supported.

func WithTemplateDefaultAction

func WithTemplateDefaultAction(URL string) GenericTemplateElementOption

WithTemplateDefaultAction sets default action of a GenericTemplateElement.

func WithTemplateImageURL

func WithTemplateImageURL(imageURL string) GenericTemplateElementOption

WithTemplateImageURL sets image url of a GenericTemplateElement.

func WithTemplateSubtitle

func WithTemplateSubtitle(subtitle string) GenericTemplateElementOption

WithTemplateSubtitle sets subtitle of a GenericTemplateElement.

type GenericTemplateMessage

type GenericTemplateMessage struct {
	Elements []*GenericTemplateElement
	// contains filtered or unexported fields
}

GenericTemplateMessage defines generic template message. A generic template message could have maximum of 10 elements.

func NewGenericTemplateMessage

func NewGenericTemplateMessage(elements []*GenericTemplateElement) *GenericTemplateMessage

NewGenericTemplateMessage returns generic template element. A generic template message could have maximum of 10 elements.

func (*GenericTemplateMessage) MarshalJSON

func (m *GenericTemplateMessage) MarshalJSON() ([]byte, error)

MarshalJSON returns json of the message.

func (*GenericTemplateMessage) TemplateType

func (m *GenericTemplateMessage) TemplateType() TemplateType

TemplateType returns template type.

func (*GenericTemplateMessage) Type

Type returns message type.

type GetIceBreakersResponse

type GetIceBreakersResponse struct {
	Data []IceBreakers `json:"data"`
}

GetIceBreakersResponse defines get ice breaker api success response.

type GetUserProfileResponse

type GetUserProfileResponse struct {
	ID         string `json:"id"`
	Name       string `json:"name"`
	ProfilePic string `json:"profile_pic"`
}

GetUserProfileResponse defines instagram get user profile response.

type IceBreaker

type IceBreaker struct {
	Question string
	Payload  string
}

IceBreaker defines Ice Breaker. frequently asked question. https://developers.facebook.com/docs/messenger-platform/instagram/features/ice-breakers

func NewIceBreaker

func NewIceBreaker(question string, payload string) *IceBreaker

NewIceBreaker returns a ice breaker.

func (*IceBreaker) MarshalJSON

func (i *IceBreaker) MarshalJSON() ([]byte, error)

MarshalJSON returns json of the ice breaker.

type IceBreakers

type IceBreakers struct {
	IceBreakers []IceBreaker `json:"ice_breakers"`
}

IceBreakers holds list of ice breakers.

type ImageMessage

type ImageMessage struct {
	ImageURL string
	// contains filtered or unexported fields
}

ImageMessage defines image message.

func NewImageMessage

func NewImageMessage(imageURL string) *ImageMessage

NewImageMessage returns a new image message.

func (*ImageMessage) MarshalJSON

func (m *ImageMessage) MarshalJSON() ([]byte, error)

MarshalJSON returns json of the message.

func (*ImageMessage) Type

func (m *ImageMessage) Type() MessageType

Type returns message type.

type InstaBot

type InstaBot interface {
	SendMessage(ctx context.Context, recipient string, message Message) (*SendMessageResponse, error)
	SetIceBreakers(ctx context.Context, iceBreakers []*IceBreaker) (*SetIceBreakersResponse, error)
	GetIceBreakers(ctx context.Context) (*GetIceBreakersResponse, error)
	DeleteIceBreakers(ctx context.Context) (*DeleteIceBreakersResponse, error)
	GetUserProfile(ctx context.Context, instagramUserID string) (*GetUserProfileResponse, error)
}

InstaBot defines InstaBot client interface.

type LogInButton

type LogInButton struct {
	URL string
	// contains filtered or unexported fields
}

LogInButton defines log in button. https://developers.facebook.com/docs/messenger-platform/send-messages/buttons#login

func NewLogInButton

func NewLogInButton(URL string) *LogInButton

NewLogInButton returns a new log in button.

func (*LogInButton) MarshalJSON

func (b *LogInButton) MarshalJSON() ([]byte, error)

MarshalJSON returns json of the button.

func (*LogInButton) Type

func (b *LogInButton) Type() ButtonType

Type returns button type.

type LogOutButton

type LogOutButton struct {
	// contains filtered or unexported fields
}

LogOutButton defines log out button. https://developers.facebook.com/docs/messenger-platform/send-messages/buttons#logout

func NewLogOutButton

func NewLogOutButton() *LogOutButton

NewLogOutButton returns a new log out button.

func (*LogOutButton) MarshalJSON

func (b *LogOutButton) MarshalJSON() ([]byte, error)

MarshalJSON returns json of the button.

func (*LogOutButton) Type

func (b *LogOutButton) Type() ButtonType

Type returns button type.

type MediaShareMessage

type MediaShareMessage struct {
	MediaID string
	// contains filtered or unexported fields
}

MediaShareMessage defines media share message.

func NewMediaShareMessage

func NewMediaShareMessage(mediaID string) *MediaShareMessage

NewMediaShareMessage returns new media share message.

func (*MediaShareMessage) MarshalJSON

func (m *MediaShareMessage) MarshalJSON() ([]byte, error)

MarshalJSON returns json of the message.

func (*MediaShareMessage) Type

func (m *MediaShareMessage) Type() MessageType

Type returns message type.

type Message

type Message interface {
	Type() MessageType
}

Message type interface.

type MessageType

type MessageType string

MessageType defines instagram message type.

const (
	MessageTypeText       MessageType = MessageType("text")
	MessageTypeImage      MessageType = MessageType("image")
	MessageTypeSticker    MessageType = MessageType("sticker")
	MessageTypeMediaShare MessageType = MessageType("media_share")
	MessageTypeReacton    MessageType = MessageType("reaction")
	MessageTypeTemplate   MessageType = MessageType("template")
)

all message type. https://developers.facebook.com/docs/messenger-platform/instagram/features/send-message#supported-messages

type PostBackButton

type PostBackButton struct {
	Title   string
	Payload string
	// contains filtered or unexported fields
}

PostBackButton defines post back button. https://developers.facebook.com/docs/messenger-platform/send-messages/buttons#postback

func NewPostBackButton

func NewPostBackButton(title string, payload string) *PostBackButton

NewPostBackButton returns a new post back button.

func (*PostBackButton) MarshalJSON

func (b *PostBackButton) MarshalJSON() ([]byte, error)

MarshalJSON returns json of the button.

func (*PostBackButton) Type

func (b *PostBackButton) Type() ButtonType

Type returns button type.

type ProductTemplateElement

type ProductTemplateElement struct {
	ProductID string
}

ProductTemplateElement defines product template element. https://developers.facebook.com/docs/messenger-platform/send-messages/template/product

func NewProductTemplateElement

func NewProductTemplateElement(productID string) *ProductTemplateElement

NewProductTemplateElement returns a new product template element.

func (*ProductTemplateElement) MarshalJSON

func (e *ProductTemplateElement) MarshalJSON() ([]byte, error)

MarshalJSON marshal product template element to JSON.

type ProductTemplateMessage

type ProductTemplateMessage struct {
	Elements []*ProductTemplateElement
	// contains filtered or unexported fields
}

ProductTemplateMessage defines product template message. A product template message could have maximum of 10 elements.

func NewProductTemplateMessage

func NewProductTemplateMessage(elements []*ProductTemplateElement) *ProductTemplateMessage

NewProductTemplateMessage returns product template element. A product template message could have maximum of 10 elements.

func (*ProductTemplateMessage) MarshalJSON

func (m *ProductTemplateMessage) MarshalJSON() ([]byte, error)

MarshalJSON returns json of the message.

func (*ProductTemplateMessage) TemplateType

func (m *ProductTemplateMessage) TemplateType() TemplateType

TemplateType returns template type.

func (*ProductTemplateMessage) Type

Type returns message type.

type QuickReply

type QuickReply struct {
	Title   string
	Payload string
	// contains filtered or unexported fields
}

QuickReply defines quick reply. quick reply is only supported for text message on instagram platform. https://developers.facebook.com/docs/messenger-platform/instagram/features/quick-replies

func NewTextQuickReply

func NewTextQuickReply(title string, payload string) *QuickReply

NewTextQuickReply returns Text type quick reply.

func (*QuickReply) MarshalJSON

func (q *QuickReply) MarshalJSON() ([]byte, error)

MarshalJSON returns json of the quick reply item.

type QuickReplyType

type QuickReplyType string

QuickReplyType defines quick reply content type.

const (
	QuickReplyTypeText QuickReplyType = QuickReplyType("text")
)

all quick reply content type. Only text type quick reply is supported for instagram.

type Recipient

type Recipient struct {
	ID string `json:"id"`
}

Recipient defines instagram user with instagram_user_id. Recipient of a message or action.

type SendMessageResponse

type SendMessageResponse struct {
	RecipientID string `json:"recipient_id"`
	MessageID   string `json:"message_id"`
}

SendMessageResponse send message api success response.

type Sender

type Sender struct {
	ID string `json:"id"`
}

Sender defines instagram user with instagram_user_id. Sender of a message or action.

type SetIceBreakersResponse

type SetIceBreakersResponse struct {
	Result string `json:"result"`
}

SetIceBreakersResponse defines set ice breaker api success response.

type StickerMessage

type StickerMessage struct {
	Sticker StickerType
	// contains filtered or unexported fields
}

StickerMessage defines sticker message.

func NewStickerMessage

func NewStickerMessage(sticker StickerType) *StickerMessage

NewStickerMessage returns sticker message.

func (*StickerMessage) MarshalJSON

func (m *StickerMessage) MarshalJSON() ([]byte, error)

MarshalJSON returns json of the message.

func (*StickerMessage) Type

func (m *StickerMessage) Type() MessageType

Type returns message type.

type StickerType

type StickerType string

StickerType defines sticker type.

const (
	StickerTypeHeart StickerType = StickerType("like_heart")
)

all sticker type.

type Template

type Template interface {
	TemplateType() TemplateType
}

Template defines template.

type TemplateDefaultAction

type TemplateDefaultAction struct {
	URL string
	// contains filtered or unexported fields
}

TemplateDefaultAction template default action.

func NewTemplateDefaultAction

func NewTemplateDefaultAction(URL string) *TemplateDefaultAction

NewTemplateDefaultAction returns new TemplateDefaultAction.

func (*TemplateDefaultAction) MarshalJSON

func (d *TemplateDefaultAction) MarshalJSON() ([]byte, error)

MarshalJSON returns json of the button.

type TemplateType

type TemplateType string

TemplateType defines available template type.

const (
	TemplateTypeProduct TemplateType = TemplateType("product")
	TemplateTypeGeneric TemplateType = TemplateType("generic")
)

all template type. generic, product template are available on instagram.

type TextMessage

type TextMessage struct {
	Text string
	// contains filtered or unexported fields
}

TextMessage defines text message.

func NewTextMessage

func NewTextMessage(text string, options ...TextMessageOption) *TextMessage

NewTextMessage returns a new text message.

func (*TextMessage) AttachQuickReplies

func (m *TextMessage) AttachQuickReplies(quickReplies []*QuickReply)

AttachQuickReplies attach quick reply items to text message

func (*TextMessage) MarshalJSON

func (m *TextMessage) MarshalJSON() ([]byte, error)

MarshalJSON returns json of the message.

func (*TextMessage) Type

func (m *TextMessage) Type() MessageType

Type returns message type.

type TextMessageOption

type TextMessageOption func(*TextMessage)

TextMessageOption defines optional argument for new text message construction.

func WithQuickReplies

func WithQuickReplies(quickReplyItems []*QuickReply) TextMessageOption

WithQuickReplies adds quick reply items to text message. Quick reply is only supported with text message.

type URLButton

type URLButton struct {
	Title string
	URL   string
	// contains filtered or unexported fields
}

URLButton defines URL button. https://developers.facebook.com/docs/messenger-platform/send-messages/buttons#url

func NewURLButton

func NewURLButton(title string, URL string) *URLButton

NewURLButton returns a new url button.

func (*URLButton) MarshalJSON

func (b *URLButton) MarshalJSON() ([]byte, error)

MarshalJSON returns json of the button.

func (*URLButton) Type

func (b *URLButton) Type() ButtonType

Type returns button type.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL