/goRoxy

a Golang version of Roxy WhastApp Bot with Command Handler helper

Primary LanguageGoGNU General Public License v3.0GPL-3.0

GitHub Repo stars GitHub forks GitHub watchers GitHub code size in bytes

Roxy

Command Handler Abstraction for whatsmeow

Installation

go get github.com/itzngga/Roxy
  • You need ffmpeg binary for generate image/video thumbnail

Get Started

package main

import (
	_ "github.com/itzngga/Roxy/examples/cmd"
	"log"

	"github.com/itzngga/Roxy/core"
	"github.com/itzngga/Roxy/options"
	_ "github.com/mattn/go-sqlite3"

	"os"
	"os/signal"
	"syscall"
)

func main() {
	app, err := core.NewGoRoxyBase(options.NewDefaultOptions())
	if err != nil {
		log.Fatal(err)
	}

	c := make(chan os.Signal)
	signal.Notify(c, os.Interrupt, syscall.SIGTERM)
	<-c
	app.Shutdown()
}

Config

default

app := core.NewGoRoxyBase(options.NewDefaultOptions())

custom

type Options struct {
	// HostNumber will use the first available device when null
	HostNumber string

	// StoreMode can be "postgres" or "sqlite"
	StoreMode string

	// LogLevel: "INFO", "ERROR", "WARN", "DEBUG"
	LogLevel string

	// This PostgresDsn Must add when StoreMode equal to "postgres"
	PostgresDsn PostgresDSN

	// This SqliteFile Generate "ROXY.DB" when it null
	SqliteFile string

	WithCommandLog              bool
	CommandResponseCacheTimeout time.Duration
	SendMessageTimeout          time.Duration
}

PostgresSQL

options := options.Options{
	StoreMode: "postgres"
	PostgresDsn: options.NewPostgresDSN("username", "password", "dbname", "port", "disable", "Asia/Jakarta")
}
app := core.NewGoRoxyBase(options)

Sqlite

options := options.Options{
	StoreMode: "sqlite"
	SqliteFile: "store.db"
}
app := core.NewGoRoxyBase(options)

Add a Command

create a simple command with:

command/hello_world.go

package cmd

import (
	"fmt"
	"github.com/itzngga/Roxy/command"
	"github.com/itzngga/Roxy/embed"
	waProto "go.mau.fi/whatsmeow/binary/proto"
	"time"
)

var speed = &command.Command{
	Name:        "speed",
	Description: "Testing speed",
	RunFunc: func(ctx *command.RunFuncContext) *waProto.Message {
		t := time.Now()
		ctx.SendReplyMessage("wait...")
		return ctx.GenerateReplyMessage(fmt.Sprintf("Duration: %f seconds", time.Now().Sub(t).Seconds()))
	},
}

func init() {
	embed.Commands.Add(speed)
}

Create Question State

example with media question state

package media

import (
	"github.com/itzngga/Leficious/src/cmd/constant"
	"github.com/itzngga/Roxy/command"
	"github.com/itzngga/Roxy/embed"
	"github.com/itzngga/Roxy/util/cli"
	waProto "go.mau.fi/whatsmeow/binary/proto"
	"log"
)

func init() {
	embed.Commands.Add(ocr)
}

var ocr = &command.Command{
	Name:        "ocr",
	Category:    "media",
	Description: "Scan text on images",
	RunFunc: func(ctx *command.RunFuncContext) *waProto.Message {
		var captured *waProto.Message
		command.NewUserQuestion(ctx).
			CaptureMediaQuestion("Please send/reply a media message", &captured).
			Exec()

		result, err := ctx.Download(captured, false)
		if err != nil {
			log.Fatal(err)
		}
		res := cli.ExecPipeline("tesseract", result, "stdin", "stdout", "-l", "ind", "--oem", "1", "--psm", "3", "-c", "preserve_interword_spaces=1")

		return ctx.GenerateReplyMessage(string(res))
	},
}

Documentation

DOC

Example

Example

License

GNU

Contribute

Pull Request are pleased to