robot

package
v0.0.0-...-3581dc8 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

README

robot package

This package contains core functionality for building your bot.

Create a bot

To create and start a bot the only function needed is Start that you can find on this package. This function will load the configuration, retrieve the token and save given commands and create the menu on the chat.

For this reason if you need to change your configuration do it before you call this function

As previously mentioned this function will also allow to set your commands. There are some important notions to keep in mind when you create a command: If present the Trigger MUST start with a "/". When empty or not given the command will reply at every updates of all the types included in the ReplyAt field. The field Description, if present, will generate an actual description of the command in the menu usable inside on the chat but only if ReplyAt includes also message.MESSAGE.

API Token

The Telegram Bot API TOKEN is normally given in input as a program argument of your application like this: $<EXECUTABLE> <TOKEN>

If you don't want to insert your API TOKEN in the command line all the time you can save it on a file (the file must contains only the bot token) and you can use the readfrom command followed by the path of your file, like this: $<EXECUTABLE> --readfrom <PATH> This command is case insensitive and can work with none, one or two "-".

There is also the possibility to inserting the TOKEN directly in your application using the configuration. To do so just use the variable Config already present in this package and use the SetAPIToken method. All configurations will be loaded on the Start command so edit the configurations (including the mentioned method) before.


Part of the Parr(B)ot framework.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Config = ParrbotConfig{
	DeleteSessionTimer: time.Hour * 2,
}

Config contains all the default Parrbot configurations. Edit them before robot.Start

Functions

func LoadCommands

func LoadCommands(commandList []Command)

LoadCommands saves the given commandList in a form that is more efficenct for the bot to retrive. Use this function one time only, is necessary for Select to work. If robot.Start is used (as racommanded), probably, there is no need to use this function

func Start

func Start(commandList ...Command)

Start give life to your amazing robo-parrot. It accepts the commands that the bot will need to handle. This function will also load all configuartion available int the Confing variable, so if you want to change them, do it before calling this function. Start will also stop the flow of execution (unless bot crash) if any error happens it will be logged on screen and progrm will terminate

Types

type Bot

type Bot struct {
	ChatID int64 // ChatID of the user who is using the bot on a private chat
}

Bot structure

func (*Bot) Update

func (b *Bot) Update(u *echotron.Update)

Update is used internally to manage the incoming inputs from Telegram

type Command

type Command struct {
	Description string             // A description of the command that will be displayed on the "/" menu if the ReplyAt includes MESSAGE
	Trigger     string             // Needs to start with the '/' character. Is the string that if contained at the start of the update would run the Scope
	ReplyAt     message.UpdateType // Tells witch UpdateType(s) the bot will reply at, sum them to put more
	CallFunc    CommandFunc        // The actual function that the bot will run
}

Command is a bot's command declaration that compose the command list

type CommandFunc

type CommandFunc func(*Bot, *message.Update) message.Any

CommandFunc is a custom type that rapresent a command that the bot should be able to run

func Select

func Select(update *message.Update) CommandFunc

Select take an update and verify it's type and then trigger in order to return the appropriate function (or nil). If robot.Start is used (as racommanded), probably, there is no need to use this function

type ParrbotConfig

type ParrbotConfig struct {
	DeleteSessionTimer time.Duration // time after witch the bot session will self distruct by the dispatcher
	// contains filtered or unexported fields
}

ParrbotConfig defines all the possible configurations of your parr-bot

func (*ParrbotConfig) KeepActiveSessions

func (c *ParrbotConfig) KeepActiveSessions()

KeepActiveSessions sets the DeleteSessionTimer = 0 causing all session to stay active

func (*ParrbotConfig) SetAPIToken

func (c *ParrbotConfig) SetAPIToken(token string) error

SetAPIToken sets the Telegram Bot API token to the given one if valid

Jump to

Keyboard shortcuts

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