echokit

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

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

Go to latest
Published: Aug 26, 2022 License: GPL-3.0 Imports: 5 Imported by: 0

README

echotron logo in a cardbox

Language Go Report Card Go Reference

echokit

A Parr(B)ot inspired toolkit for working with the echotron library

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Arrange

func Arrange[AnyButton InlineButton | KeyButton](columns int, fromList ...AnyButton) (keyboard [][]AnyButton)

Arrange the layout of given buttons in a given number of columns

Example
package main

import (
	"fmt"

	"github.com/DazFather/echokit"
)

func main() {
	var buttons [][]echokit.KeyButton = echokit.Arrange(2, // Arrange following buttons in 2 columns
		echokit.KeyButton{Text: "Hello World!"},
		echokit.KeyButton{Text: "Send your location", RequestLocation: true},
		echokit.KeyButton{Text: "Send me your contact infos", RequestContact: true},
	)

	// First row
	fmt.Println(buttons[0][0].Text)
	fmt.Println(buttons[0][1].Text)

	// Second row
	fmt.Println(buttons[1][0].Text)

}
Output:

Hello World!
Send your location
Send me your contact infos

func DownloadDocument

func DownloadDocument(api echotron.API, document echotron.Document, basePath string) (content []byte, err error)

DownloadDocument downloads a given document and store it in given directory

func DownloadFile

func DownloadFile(api echotron.API, fileID string, path string) (content []byte, err error)

DownloadFile downloads a file with given ID at the given path

func ExtractEntities

func ExtractEntities(message echotron.Message, filter EntityFilter) (extracted []string)

ExtractEntities extracts the content of the Entities contained inside given message and filtered by given filter function

Example
var (
	filterBig echokit.EntityFilter = func(entity echotron.MessageEntity) bool {
		return entity.Length > 6
	}

	message = echotron.Message{
		Text: "Hi @DazFather! Did you ever hear about the #DigitalResistance ?",
		Entities: []*echotron.MessageEntity{
			&echotron.MessageEntity{
				Offset: 3,
				Length: 10,
				Type:   echotron.MentionEntity,
			},
			&echotron.MessageEntity{
				Offset: 43,
				Length: 18,
				Type:   echotron.HashtagEntity,
			},
		},
	}
)

fmt.Println(echokit.ExtractEntities(message, filterBig)) 
Output:

[@DazFather #DigitalResistance]

func ExtractEntitiesOfType

func ExtractEntitiesOfType(message echotron.Message, allowedTypes ...echotron.MessageEntityType) []string

ExtractEntitiesOfType extracts the content of the Entities contained filtered by their type

Example
var message = echotron.Message{
	Text: "Hi @DazFather! Did you ever hear about the #DigitalResistance ?",
	Entities: []*echotron.MessageEntity{
		&echotron.MessageEntity{
			Offset: 3,
			Length: 10,
			Type:   echotron.MentionEntity,
		},
		&echotron.MessageEntity{
			Offset: 43,
			Length: 18,
			Type:   echotron.HashtagEntity,
		},
	},
}

tags := echokit.ExtractEntitiesOfType(message, echotron.HashtagEntity)
extracted := echokit.ExtractEntitiesOfType(message, echotron.MentionEntity, echotron.HashtagEntity)

fmt.Println(tags)         

func ExtractEntity

func ExtractEntity(fromText string, entity echotron.MessageEntity) string

ExtractEntity extract the entity content from a given text

Example
var entity = echotron.MessageEntity{
	Offset: 3,
	Length: 10,
	Type:   echotron.MentionEntity,
}

fmt.Println(echokit.ExtractEntity("Hi @DazFather!", entity)) 
Output:

@DazFather

func ExtractIDOpt

func ExtractIDOpt[IDExtractor IDOptionsExtractor](from IDExtractor) *echotron.MessageIDOptions

ExtractIDOpt extract a MessageIDOptions from different data structures (IDOptionsExtractor), nil if can't

Example
var (
	message = echotron.Message{
		ID:   1234,
		Chat: echotron.Chat{ID: 5678},
	}

	callback = echotron.CallbackQuery{InlineMessageID: "Hello"}

	update = echotron.Update{ChannelPost: &message}
)

fmt.Println(*echokit.ExtractIDOpt(message) == echotron.NewMessageID(message.Chat.ID, message.ID))                      

func FetchFile

func FetchFile(api echotron.API, fileID string) (content []byte, err error)

FetchFile gets the content of a file with given ID

func GenInlineKeyboard

func GenInlineKeyboard(columns int, fromList ...InlineButton) echotron.InlineKeyboardMarkup

GenInlineKeyboard allows to generate an echotron.ReplyMarkup from a list of inline buttons dividing them into columns. Is the equivalent of doing: InlineKeyboard(Arrange(columns, fromList...))

Example
// Arrange following buttons in 2 columns and generate an InlineKeyboardMarkup
var markup echotron.InlineKeyboardMarkup = echokit.GenInlineKeyboard(2,
	echokit.InlineButton{Text: "Click me", CallbackData: "/command"},
	echokit.InlineButton{Text: "Golang", URL: "https://go.dev"},
	echokit.InlineButton{Text: "I'm on the 2nd row", CallbackData: "/lol"},
)

// First row
fmt.Println(markup.InlineKeyboard[0][0].Text)
fmt.Println(markup.InlineKeyboard[0][1].Text)

// Second row
fmt.Println(markup.InlineKeyboard[1][0].Text)
Output:

Click me
Golang
I'm on the 2nd row

func InlineKeyboard

func InlineKeyboard(kbd [][]InlineButton) echotron.InlineKeyboardMarkup

InlineKeyboard allows to quickly cast a matrix of inline buttons into a ReplyMarkup

Example
var markup echotron.InlineKeyboardMarkup = echokit.InlineKeyboard([][]echokit.InlineButton{
	// First row
	{
		{Text: "Click me", CallbackData: "/command"},
		{Text: "Golang", URL: "https://go.dev"},
	},
	// Second row
	{{Text: "I'm on the 2nd row", CallbackData: "/lol"}},
})

fmt.Println(markup.InlineKeyboard[0][0].Text) 
Output:

Click me

func Keyboard

func Keyboard(disposable bool, inputPlaceholder string, kbd [][]KeyButton) echotron.ReplyKeyboardMarkup

Keyboard allows to quickly cast a matrix of buttons into a ReplyMarkup setting some useful options. Arguments: disposable - after user tap on any button the keyboard will be removed, inputPlaceholder - a text (max 64 chars) that will apper os placeholder on input field kbd - the matrix of buttons that compose the keyboard Resulting ReplyKeyboardMarkup will also have ResizeKeyboard and Selective set to true

Example
var (
	// After using it keyboard will be removed
	disposable bool = true
	// A text that will appear as placeholder on the message input box
	inputPlaceholder string = "Use the keyboard down below:"
	buttons                 = [][]echokit.KeyButton{
		{{Text: "Hello World!"}, {Text: "Send your location", RequestLocation: true}},
		{{Text: "Send me your contact infos", RequestContact: true}},
	}
)

var markup echotron.ReplyKeyboardMarkup = echokit.Keyboard(disposable, inputPlaceholder, buttons)
fmt.Println(markup.Keyboard[0][0].Text)
// These will be set to: true
fmt.Println(markup.ResizeKeyboard, markup.Selective)
// These will be set to the firsts 2 arguments (disposable and inputPlaceholder)
fmt.Println(markup.OneTimeKeyboard, markup.InputFieldPlaceholder)
Output:

Hello World!
true true
true Use the keyboard down below:

func KeyboardRemover

func KeyboardRemover(globally bool) echotron.ReplyKeyboardRemove

KeyboardRemover allows to remove a previouly sent keyboard. If globally is false then kwyboard will be removed for user mentioned on the message or on the reply

Example
var (
	globally   bool                         = true
	kbdRemover echotron.ReplyKeyboardRemove = echokit.KeyboardRemover(globally)
)

fmt.Println(kbdRemover.RemoveKeyboard) 

func StirngToUft16

func StirngToUft16(toEncode string) []uint16

StirngToUft16 encode a given string into Utf16 format

Example
package main

import (
	"fmt"

	"github.com/DazFather/echokit"
)

func main() {
	var encoded []uint16 = echokit.StirngToUft16("Hello, 世界")
	fmt.Println(encoded)
}
Output:

[72 101 108 108 111 44 32 19990 30028]

func Uft16ToStirng

func Uft16ToStirng(encodedText []uint16) string

Uft16ToStirng decode a given encoded text in Utf16 format into a string

Example
package main

import (
	"fmt"

	"github.com/DazFather/echokit"
)

func main() {
	var encoded = []uint16{72, 101, 108, 108, 111, 44, 32, 19990, 30028}
	fmt.Println(echokit.Uft16ToStirng(encoded))
}
Output:

Hello, 世界

func Wrap

func Wrap[T any](elem T) []T

Wrap anything into a slice of the same type. Especially useful when dealing with buttons

Example
package main

import (
	"fmt"

	"github.com/DazFather/echokit"
)

func main() {
	var (
		strings   []string                 = echokit.Wrap("Hello")
		buttonRow []echokit.InlineButton   = echokit.Wrap(echokit.InlineButton{Text: "Hello!"})
		buttons   [][]echokit.InlineButton = echokit.Wrap(buttonRow)
	)

	fmt.Println(strings)   

Types

type EntityFilter

type EntityFilter func(entity echotron.MessageEntity) bool

EntityFilter is a function checks an entity, returning true if pass false otherwise

func FilterEntityByType

func FilterEntityByType(allowedTypes ...echotron.MessageEntityType) EntityFilter

FilterEntityByType generates an EntityFilter that will return true only if entity's type match at least one of the given ones

Example
var (
	filter echokit.EntityFilter = echokit.FilterEntityByType(echotron.HashtagEntity, echotron.CashtagEntity)

	hashtag = echotron.MessageEntity{Type: echotron.HashtagEntity}
	cashtag = echotron.MessageEntity{Type: echotron.CashtagEntity}
	link    = echotron.MessageEntity{Type: echotron.UrlEntity}
)

fmt.Println(filter(hashtag)) 

type InlineButton

type InlineButton = echotron.InlineKeyboardButton

InlineButton is a shorter way to indicate an echotron.InlineKeyboardButton

func InlineCaller

func InlineCaller(caption, trigger string, payload ...string) InlineButton

InlineCaller creates an inline button that will call the trigger-related command

Example
package main

import (
	"fmt"

	"github.com/DazFather/echokit"
)

func main() {
	var button echokit.InlineButton = echokit.InlineCaller("Click me", "/command", "first", "second")

	fmt.Println(button.Text)         

type KeyButton

type KeyButton = echotron.KeyboardButton

KeyButton is a shorter way to indicate echotron.KeyboardButton

Jump to

Keyboard shortcuts

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