Hey there, fellow Go enthusiast! Ready to dive into the world of Facebook Messenger bots? You're in for a treat. We'll be using the awesome messenger-platform-go-sdk
package to make our lives easier. Let's get cracking!
Before we jump in, make sure you've got:
Got all that? Great! Let's move on to the fun stuff.
First things first, let's get our project structure sorted:
mkdir messenger-bot && cd messenger-bot go mod init github.com/yourusername/messenger-bot go get github.com/maciekmm/messenger-platform-go-sdk
Easy peasy, right? Now we're ready to start coding!
Let's import the necessary packages and set up our Messenger client:
package main import ( "log" "net/http" "github.com/maciekmm/messenger-platform-go-sdk" ) func main() { client := messenger.New(messenger.Options{ Verify: true, AppSecret: "YOUR_APP_SECRET", AccessToken: "YOUR_ACCESS_TOKEN", VerifyToken: "YOUR_VERIFY_TOKEN", }) // We'll add more code here soon! }
Remember to replace those placeholder values with your actual Facebook App credentials!
Now, let's set up our webhook to receive messages:
http.HandleFunc("/webhook", client.Handler) log.Println("Server is running at :8080") http.ListenAndServe(":8080", nil)
But wait, we need to actually do something when we receive a message, right? Let's add a callback:
client.MessageReceived = func(m messenger.Message, r *messenger.Response) { log.Printf("Message received: %v", m.Text) r.Text("Hello! I got your message: " + m.Text) }
Sending a simple text message is easy, as you saw above. But let's get fancy with some rich media:
// Sending an image r.Attachment(messenger.AttachmentType.Image, "https://example.com/cool-image.jpg", nil) // Sending a button template r.ButtonTemplate("What would you like to do?", []messenger.TemplateButton{ messenger.NewURLButton("Visit our website", "https://example.com"), messenger.NewPostbackButton("Start over", "START_OVER"), }) // Sending quick replies r.QuickReplies("Choose an option:", []messenger.QuickReply{ {Title: "Option 1", Payload: "OPTION_1"}, {Title: "Option 2", Payload: "OPTION_2"}, })
When users interact with your buttons or quick replies, you'll want to handle those events:
client.PostbackReceived = func(p messenger.Postback, r *messenger.Response) { switch p.Payload { case "START_OVER": r.Text("Let's start over!") default: r.Text("I'm not sure what to do with that postback.") } }
Want to get user info or set up a persistent menu? Here's how:
// Get user profile profile, err := client.GetProfile(userID) if err != nil { log.Printf("Error getting profile: %v", err) } else { log.Printf("User name: %s %s", profile.FirstName, profile.LastName) } // Set up persistent menu client.SetPersistentMenu([]messenger.LocalizedMenu{ { Locale: "default", ComposerInputDisabled: false, Buttons: []messenger.MenuItem{ messenger.NewPostbackButton("Start Over", "START_OVER"), messenger.NewWebURLButton("Visit Website", "https://example.com"), }, }, }) // Set up Get Started button client.SetGetStartedButton("GET_STARTED")
Ready to test? Use ngrok to expose your local server:
ngrok http 8080
Then update your webhook URL in the Facebook Developer Console with the ngrok URL.
When you're ready to go live, make sure you:
And there you have it! You've just built a Facebook Messenger bot in Go. Pretty cool, right? Remember, this is just the beginning. There's so much more you can do with the Messenger Platform. Keep exploring, keep coding, and most importantly, have fun!
Want to learn more? Check out the official Facebook Messenger Platform documentation and the messenger-platform-go-sdk GitHub repo.
Now go forth and build some awesome bots!