Git Product home page Git Product logo

messenger-platform-go-sdk's Introduction

Messenger Platform Go SDK

Build Status Coverage Status

A Go SDK for the Facebook Messenger Platform.

Installation

go get gopkg.in/maciekmm/messenger-platform-go-sdk.v4

Usage

The main package has been named messenger for convenience.

Your first step is to create Messenger instance.

import "gopkg.in/maciekmm/messenger-platform-go-sdk.v4"

//...

messenger := &messenger.Messenger {
	VerifyToken: "VERIFY_TOKEN/optional",
	AppSecret: "APP_SECRET/optional",
	AccessToken: "PAGE_ACCESS_TOKEN",
	Debug: messenger.DebugAll, //All,Info,Warning
}

Parameters

  • VerifyToken is the token needed for a verification process facebook performs. It's only required once. Optional.
  • AppSecret is the Application Secret token. It's used for message integrity check. Optional.
  • AccessToken is required to send messages. You can find this token in your app developer dashboard under Messenger tab.
  • Debug is used for setting debug mode type as described on https://developers.facebook.com/docs/graph-api/using-graph-api#debugging. Optional.

The next step is to hook up the handler to your HTTP server.

//hook up
http.HandleFunc("/webhook", messenger.Handler)
//start the server
http.ListenAndServe(":5646", nil)

The next step is to subscribe to an event, to do that you have to hook up your own handler.

messenger.MessageReceived = MessageReceived

//...

func MessageReceived(event messenger.Event, opts messenger.MessageOpts, msg messenger.ReceivedMessage) {
//do stuff
}

Sending messages

Example

Check more examples in examples folder.

var mess = &messenger.Messenger{
	AccessToken: "ACCESS_TOKEN",
}

func main() {
	mess.MessageReceived = MessageReceived
	http.HandleFunc("/webhook", mess.Handler)
	log.Fatal(http.ListenAndServe(":5646", nil))
}

func MessageReceived(event messenger.Event, opts messenger.MessageOpts, msg messenger.ReceivedMessage) {
	profile, err := mess.GetProfile(opts.Sender.ID)
	if err != nil {
		fmt.Println(err)
		return
	}
	resp, err := mess.SendSimpleMessage(opts.Sender.ID, fmt.Sprintf("Hello, %s %s, %s", profile.FirstName, profile.LastName, msg.Text))
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("%+v", resp)
}

messenger-platform-go-sdk's People

Contributors

baptistejamin avatar catoaune avatar connyay avatar harrisonzhao avatar husafan avatar maciekmm avatar marselester avatar orsonwang avatar stanleyye avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

messenger-platform-go-sdk's Issues

Send Attachment Example

Good afternoon.

Could someone be so kind as to show an example for sending attachments?

All of my files are local HD as *.jpeg, *.mp4 and *.doc

Event ID can be string or int

Hi Maciej,

Unfortunately Facebook is not consistent with entry IDs. For messages it uses strings {"object":"page","entry":[{"id":"123",, but for postbacks ints {"object":"page","entry":[{"id":123,.

Not sure what would be a proper way to handle that, I use json.Number and hope Facebook migrates to strings eventually.

type Event struct {
    ID   json.Number `json:"id"`
    Time int64       `json:"time"`
}

ButtonTemplate not working

Users are forced to use GenericTemplate instead of ButtonTemplate because ButtonTemplate requires a 'buttons' field, not 'elements' field.

mq.Template(template.GenericTemplate{Title: title,
		Buttons: []template.Button{
			{
				Type:    template.ButtonTypePostback,
				Payload: postbackPayload,
				Title:   buttonTitle,
			},
		},
	})

Implement payments

SetWelcomeMessage returns a 400

Hi there! Me again :)

When I attempt to set a welcome message, like...

err := mess.SetWelcomeMessage(&messenger.Message{
    Text: "Hello!",
})

if err != nil {
    log.Fatal(err)
}

I get an error Invalid status code.

So I had a little dig into the code and I've noticed in welcomemessage.go

var welcomeMessage = ctaBase{
    SettingType: "setting_type",
    ThreadState: "new_thread",
}

According to the docs, that should be...

var welcomeMessage = ctaBase{
    SettingType: "call_to_actions",
    ThreadState: "new_thread",
}

However I tried changing this and got the same result.

Issue getting an attachment

Hi there,

I'm attempting to get file attachments, but it doesn't seem to be present in the message event.

// MessageHandler - Handles messaged from Facebook page and returns response with template
func MessageHandler(event messenger.Event, opts messenger.MessageOpts, msg messenger.ReceivedMessage) {

    log.Println(msg.Message.Attachment)

        // ... Redacted code
}

The trouble is... msg.Message.Attachment is always nil

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.