Git Product home page Git Product logo

go-messenger-bot's Introduction

Golang bindings for the Messenger Bot API

The scope of this project is just to provide a wrapper around the API without any additional features.

Example

This is a very simple bot that just displays any gotten updates, then replies it to it.

package main

import (
	"log"
    "net/http"
	"github.com/abhinavdahiya/go-messenger-bot"
)

func main() {
	bot := mbotapi.NewBotAPI("ACCESS_TOKEN", "VERIFY_TOKEN")

	callbacks, mux := bot.SetWebhook("/webhook")
	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", mux)

    for callback := range callbacks {
        log.Printf("[%#v] %s", callback.Sender, callback.Message.Text)

        msg := mbotapi.NewMessage(callback.Message.Text)
        bot.Send(callback.Sender, msg, mbotapi.RegularNotif)
    }
}

Facebook messenger webhook needs a certificate certified by known CA, Now that Let's Encrypt has entered public beta, you may wish to generate your free TLS certificate there.

Inspiration

Messenger takes design cues from:

go-messenger-bot's People

Contributors

abhinavdahiya avatar keeth avatar kelonye 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

go-messenger-bot's Issues

Ability to serve multiple pages from the same webhook

I want to create a bot that multiple people could add to their pages. I like your idea of returning a channel of Callbacks, but it might need pulling apart once I add multiple pages.

Sorry if this feels a bit stream-of-consciousness: I'm mostly just trying to organise my thoughts (this is my first Go project, but I've written messenger bots in other languages).

I think that it might be enough to copy Entry.PageID into the Callback struct, and then make the Callback channel consumer maintain a mapping from PageID to BotAPI. This feels ugly though.

Alternatively we could remove BotAPI.Token, and replace it with a mapping from PageID to Page (might want renaming) which would look something like:

type Page struct {
	PageID    int64
        Token     string
        channel   chan<- Callback
}

A bunch of methods would move from being on BotAPI to being on Page.

We could then create a function like:

func (bot *BotAPI) AddPage(pageID int64, token string) (*Page, <-chan Callback)

which adds a Page to the BotAPI and then returns it along with channel of Callbacks.

Any Entry that doesn't belong to a Page could then be sent to a callback function:

func HandleUnknownPage(pageID int64, entry Entry) bool 

which would return true if it called AddPage(), to make the BotAPI retry the Entry.

SetWebhook() could then lose the <-chan Callback part of its return value.

In a world where you want to add a page to a live load-balanced website, you would need to:

  • Write the PageID and Token to a Database somewhere.
  • Tell Facebook to subscribe to the webhooks for that page.
  • When webhooks start coming in, make your HandleUnknownPage() callback pick the token out of your Database and call AddPage().

Of course, if you're load-balancing your webhook requests onto random servers then they won't be showing up in a stream anymore, so the <-chan Callback interface stops making sense, and you might as well just use callback functions ๐Ÿ˜ž . Also, if your HandleUnknownPage() callback is expensive then you might end up in trouble if you accidentally subscribe to webhooks without writing the Token to the DB.

Is this an approach that's worth pursuing, or should I just look for a callback-function-based messenger lib that's already capable of handling multiple pages?

SendFile doesn't work

I get:

2017/07/20 09:03:32 {0  {(#100) The parameter recipient is required OAuthException 100  }} Bad Request

When I try use it.

Goroutine use for listen

Hi

Is there any reason in your example code you are using a goroutine for the listen?

go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", mux)

I have only ever seen using goroutines like this when one program has to listen on multiple ports.

Interest to see why you chose to do this this way?

Alan

Example code doesn't compile assignment count mismatch: 2 = 1

example code examines err on NewBotAPI

but has no error return

func NewBotAPI(token string, vtoken string) *BotAPI {

  • even correcting this I'm not getting your library to work
    my own test code / certs etc are working OK

is it still work in progress or have you got the example operational?

setup on Appengine Standard Environment

On GAE Standard Environment I encountered this error:

fatal error: all goroutines are asleep - deadlock!

Setup in init function:

`func init() {
bot := mbotapi.NewBotAPI(PAGE_ACCESS_TKN, "whk", "sekret")

callbacks, mux := bot.SetWebhook("/webhook")

var gaeCtx context.Context

var handlerFunc http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
	r = r.WithContext(appengine.NewContext(r))
	gaeCtx = r.Context()
	mux.ServeHTTP(w, r)
}

http.Handle("/webhook", handlerFunc)

for callback := range callbacks {
	log.Debugf(gaeCtx, "[%#v] %s", callback.Sender, callback.Message.Text)
	msg := mbotapi.NewMessage(callback.Message.Text)
	bot.Send(callback.Sender, msg, mbotapi.RegularNotif)
}

}`

I see that otherwise mbotapi would work by setting GAE's urlfetch.Client pointer.

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.