machaao

package module
v0.0.0-...-e06ced0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2020 License: MIT Imports: 8 Imported by: 0

README

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
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MachaaoAPIToken string = os.Getenv("MachaaoAPIToken")

Get MachaaoAPIToken from https://portal.messengerx.io

View Source
var MachaaoBaseURL string = os.Getenv("MachaaoBaseURL")

for dev, use https://ganglia-dev.machaao.com

View Source
var WitAPIToken string = os.Getenv("WitAPIToken")

Get WitAPIToken from https://wit.ai

Functions

func AddContent

func AddContent(body interface{}) (*http.Response, error)

Insert or update content for your bot

func GetPort

func GetPort() string

Set PORT as env var or leave it to use 4747

func GetUserProfile

func GetUserProfile(userID string) (*http.Response, error)

Get basic profile of the user

func GetUserTag

func GetUserTag(userID string) (*http.Response, error)

GetUserTag Get all tags for a specific userID

func SearchContent

func SearchContent(query string) (*http.Response, error)

SearchContent Search content on your bot

func SearchContentViaSlug

func SearchContentViaSlug(slug string) (*http.Response, error)

Get content for your bot via an unique slug

func SendAnnouncement

func SendAnnouncement(body interface{}) (*http.Response, error)

Send a subscription message announcement to a tag or a timezone

func SendGetReq

func SendGetReq(apiURL string) (*http.Response, error)

Sends get request to MessengerX.io API

func SendMessage

func SendMessage(body interface{}) (*http.Response, error)

Send a standard message to a single user id or multiple user id

func SendPostReq

func SendPostReq(apiURL string, body interface{}) (*http.Response, error)

Sends post request to MessengerX.io API

func Server

func Server(handler MessageHandler)

Starts server at given PORT. WebHook is machaao_hook input message handler type function(http.ResponseWriter, *http.Request)

func TagUser

func TagUser(userID string, body interface{}) (*http.Response, error)

Tag or Un-tag a specific userId

func WitAIResponse

func WitAIResponse(message string) (*witai.MessageResponse, error)

WitAIResponse Get response for the input message

Types

type MessageHandler

type MessageHandler func(http.ResponseWriter, *http.Request)

This function handles messages Input parameters (http.ResponseWriter, *http.Request)

Jump to

Keyboard shortcuts

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