Skip to content

machaao/machaao-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

machaao-go

MessengerX logo

GoDoc Go Report Card Gitter

machaao-go is a go (or 'golang' for search engine friendliness) based library for building chatbot using MessengerX APIs.

This repository is community-maintained. We gladly accept pull requests. Please see the Machaao API Docs for all supported endpoints.

The API docs for the MessengerX.io is availabe at MessengerX API Docs and Machaao API Docs.

Install

go get github.com/machaao/machaao-go

Usage

  1. Visit MessengerX Docs and Wit.ai to get FREE API Token.

  2. Put MachaaoAPIToken, MachaaoBaseURL and WitAPIToken on path. Follow this article for more details on Environment Variable.

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"

	"github.com/dgrijalva/jwt-go"
	"github.com/machaao/machaao-go"
)

func main() {
	machaao.Server(messageHandler)
}

func messageHandler(w http.ResponseWriter, r *http.Request) {

	if r.Method != "POST" {
		http.Error(w, "Method is not supported.", http.StatusNotFound)
		return
	}

	//This function reads the request Body and saves to body as bytes.
	body, err := ioutil.ReadAll(r.Body)

	if err != nil {
		log.Printf("Error reading body: %v", err)
		return
	}

	//converts bytes to string
	var bodyData string = string(body)

	//incoming JWT Token
	var tokenString string = bodyData[8:(len(bodyData) - 2)]

	claims := jwt.MapClaims{}
	token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
		return []byte(machaao.MachaaoAPIToken), nil
	})

	_ = token

	if err != nil {
		fmt.Println(err)
	}

	//Do Something
}