Git Product home page Git Product logo

botstate's Introduction

Botstate

The easy way to manage bot states.

Build Status GoDoc

Simple Bot Flow with Botstate

Botstate

Examples

Installation

botstate requires a Go version with Modules support and uses import versioning. So please make sure to initialize a Go module before installing botstate:

go mod init
go get github.com/gucastiliao/botstate

Import:

import "github.com/gucastiliao/botstate"

Manage state

To manage states, botstate have a default client that uses go-redis, but you can make your own client to save state the way you prefer.

You need to initialize storage with your client. This example is using go-redis with botstate.DefaultStorage:

r := redis.NewClient(&redis.Options{
    Addr: mr.Addr(),
})

botstate.SetStorageClient(botstate.DefaultStorage(r))

If you have your own client:

var myClient botstate.Storager

...

botstate.SetStorageClient(myClient)

Simple Example

package main

import (
	"fmt"

	"github.com/go-redis/redis/v7"
	"github.com/gucastiliao/botstate"
)

func main() {
	redis := redis.NewClient(&redis.Options{
		Addr:     fmt.Sprintf("%s:%s", "127.0.0.1", "6379"),
		Password: "",
	})

	botstate.SetStorageClient(
		botstate.DefaultStorage(redis),
	)

	states := []botstate.State{
		{
			Name:     "start",
			Executes: Start,
			Next:     "add_product",
		},
		{
			Name:     "add_product",
			Executes: AddProduct,
			Callback: CallbackAddProduct,
			Next:     "confirmation",
		},
		{
			Name:     "confirmation",
			Executes: Confirmation,
		},
	}

	bot := botstate.New(states)
	bot.Data.User(111)

	bot.ExecuteState("start")
	msg := bot.GetMessages()
	fmt.Println(msg)
	// This print [Hello! Starting...]

	current, _ := bot.Data.GetCurrentState()
	// Current state now is -> add_product
	// Because value of State.Next

	bot.ExecuteState(current)
	msg = bot.GetMessages()
	fmt.Println(msg)
	// This print [Add Product...]

	current, _ = bot.Data.GetCurrentState()
	// Current state now is -> confirmation

	bot.ExecuteState(current)
	msg = bot.GetMessages()
	fmt.Println(msg)
	// This print I'm in callback now and [Confirmation ...]
	// The callback is defined in the previous state
	// And in this execution is called
	// If the callback return true, the execution continue and call Confirmation method
}

// botstate.Bot have access to current user's data
// and can manipulate it
func Start(bot *botstate.Bot) bool {
	bot.AddMessage([]string{
		"Hello!",
		"Starting...",
	})

	return true
}

func AddProduct(bot *botstate.Bot) bool {
	bot.AddMessage([]string{
		"Add Product...",
	})

	return true
}

func CallbackAddProduct(bot *botstate.Bot) bool {
	fmt.Println("I'm in callback now!")

	return true
}

func Confirmation(bot *botstate.Bot) bool {
	bot.AddMessage([]string{
		"Confirmation...",
	})

	bot.Data.ResetCurrentState()
	return true
}

See more

botstate's People

Contributors

gustv000 avatar

Stargazers

Reginaldo Junior avatar

Watchers

 avatar

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.