uchatbot

package module
v1.13.1 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2024 License: MIT Imports: 12 Imported by: 0

README

logo

Engine for creating chatbots for Utopia Messenger

Concept

You don't want to understand Utopia API, but you have an idea how to make a bot that works with users in private and public messages.

The engine can:

  1. process messages from contacts;
  2. process messages in channels (private & public messages);
  3. automatically logs into channels, can use the password for closed channels.

Using the engine

  1. Chatbots that raise and retain user activity in channels.
  2. Bots for performing services to users.
  3. Creating a bot constructor.

Install

go get github.com/Sagleft/uchatbot-engine

Example

package main

import (
	"fmt"
	"log"

	"github.com/Sagleft/uchatbot-engine"
	utopiago "github.com/Sagleft/utopialib-go/v2"
	"github.com/Sagleft/utopialib-go/v2/pkg/structs"
	"github.com/fatih/color"
)

const APIToken = "your-utopia-api-token"

func main() {
	bot, err := uchatbot.NewChatBot(uchatbot.ChatBotData{
		Config: utopiago.Config{
			Host:   "127.0.0.1",
			Token:  APIToken,
			Port:   20000,
			WsPort: 25000,
		},
		Chats: []uchatbot.Chat{
			{ID: "D53B4431FD604E2F0261792444797AA4"},
			{ID: "A59D8B62E1A59049564A4B0F8B457D45"},
		},
		Callbacks: uchatbot.ChatBotCallbacks{
			OnContactMessage:        OnContactMessage,
			OnChannelMessage:        OnChannelMessage,
			OnPrivateChannelMessage: OnPrivateChannelMessage,

			WelcomeMessage: OnWelcomeMessage,
		},
		UseErrorCallback: true,
		ErrorCallback:    onError,
	})
	if err != nil {
		log.Fatalln(err)
	}

	log.Println("bot started")
	bot.Wait()
}

func OnContactMessage(m structs.InstantMessage) {
	fmt.Printf("[CONTACT] %s: %s\n", m.Nick, m.Text)
}

func OnChannelMessage(m structs.WsChannelMessage) {
	fmt.Printf("[CHANNEL] %s: %s\n", m.Nick, m.Text)
}

func OnPrivateChannelMessage(m structs.WsChannelMessage) {
	fmt.Printf("[PRIVATE] %s: %s\n", m.Nick, m.Text)
}

func OnWelcomeMessage(userPubkey string) string {
	return fmt.Sprintf("Hello! Your pubkey is %s", userPubkey)
}

func onError(err error) {
	color.Red(err.Error())
}

If you have any questions

You can find me in Utopia: F50AF5410B1F3F4297043F0E046F205BCBAA76BEC70E936EB0F3AB94BF316804


You might also be interested in:

automate crypto trading

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Chat

type Chat struct {
	// required
	ID string `json:"id"`

	// optional
	Password string `json:"password"`
}

type ChatBot

type ChatBot struct {
	// contains filtered or unexported fields
}

func NewChatBot

func NewChatBot(data ChatBotData) (*ChatBot, error)

NewChatBot - create new chatbot and connect to Utopia. the bot will try to join the list of the specified chats and subscribe to messages

func (*ChatBot) GetClient added in v1.7.0

func (c *ChatBot) GetClient() utopiago.Client

GetClient - get Utopia client from chat bot

func (*ChatBot) GetOwnContact added in v1.6.0

func (c *ChatBot) GetOwnContact() (structs.OwnContactData, error)

GetOwnContact - get account data

func (*ChatBot) GetOwnPubkey added in v1.1.2

func (c *ChatBot) GetOwnPubkey() (string, error)

GetOwnPubkey - get account public key

func (*ChatBot) RequestDonate added in v1.10.0

func (c *ChatBot) RequestDonate(channelID string) *DonateService

RequestDonate - ask chat users to support the channel author

func (*ChatBot) SendChannelMessage added in v1.5.2

func (c *ChatBot) SendChannelMessage(channel, msgText string) error

SendChannelMessage - send message to channel

func (*ChatBot) SendChannelPrivateMessage added in v1.1.0

func (c *ChatBot) SendChannelPrivateMessage(channel, userPubkeyHash, msgText string)

SendChannelPrivateMessage - send message to contact in channel (in private chat). it works with queue (buffer)

func (*ChatBot) SendCoins added in v1.4.0

func (c *ChatBot) SendCoins(currency CurrencyType, pubkey string, amount float64)

SendCoins from main account

func (*ChatBot) SendCoinsFromCard added in v1.4.0

func (c *ChatBot) SendCoinsFromCard(currency CurrencyType, pubkey string, amount float64, fromCard string)

SendCoins from crypto card

func (*ChatBot) SendContactMessage added in v1.0.5

func (c *ChatBot) SendContactMessage(userPubkey string, msgText string)

SendContactMessage - send message to contact. it works with queue (buffer)

func (*ChatBot) SendWelcomeMessage added in v1.0.5

func (c *ChatBot) SendWelcomeMessage(userPubkey string)

func (*ChatBot) SetAccountNickname added in v1.6.0

func (c *ChatBot) SetAccountNickname(nick string) error

SetAccountNickname - set account new nickname

func (*ChatBot) SetReadonly added in v1.0.1

func (c *ChatBot) SetReadonly(channelID string, readOnly bool) error

SetReadonly - enable or disable channel readonly mode

func (*ChatBot) Wait added in v1.12.0

func (c *ChatBot) Wait()

Wait - blocking method

type ChatBotCallbacks

type ChatBotCallbacks struct {
	// required
	OnContactMessage        func(structs.InstantMessage)
	OnChannelMessage        func(structs.WsChannelMessage)
	OnPrivateChannelMessage func(structs.WsChannelMessage)

	// optional
	WelcomeMessage func(userPubkey string) string
}

type ChatBotData

type ChatBotData struct {
	// required
	Config    utopiago.Config `json:"client"`
	Chats     []Chat          `json:"chats"` // channel ids
	Callbacks ChatBotCallbacks

	// optional
	SkipConnectionCheck bool                 `json:"skipConnCheck"`
	Notifications       string               `json:"notifications"` // by default: all
	UseErrorCallback    bool                 `json:"useErrorCallback"`
	EnableWsSSL         bool                 `json:"enableSSL"` // SSL for websocket connection
	BuffersCapacity     EventBuffersCapacity `json:"buffersCapacity"`
	RateLimiters        EventBufferLimiters  `json:"rateLimiters"`
	DisableEvents       bool                 `json:"disableEvents"`

	ErrorCallback func(err error) `json:"-"`
}

type CurrencyType added in v1.4.0

type CurrencyType string
const (
	CurrencyCrypton CurrencyType = "CRP"
	CurrencyUUSD    CurrencyType = "UUSD"
)

type DonateService added in v1.10.0

type DonateService struct {
	// contains filtered or unexported fields
}

func (*DonateService) Do added in v1.10.0

func (srv *DonateService) Do() error

func (*DonateService) EnableUCode added in v1.10.0

func (srv *DonateService) EnableUCode(enabled bool, comment string) *DonateService

EnableUCode - add uCode with address to message

func (*DonateService) GetDonateMessage added in v1.11.0

func (srv *DonateService) GetDonateMessage() (string, string, error)

GetDonateMessage returns ownerPubkey, message, error

func (*DonateService) SetMessage added in v1.10.0

func (srv *DonateService) SetMessage(newMessage string) *DonateService

SetMessage - set a custom message that will appear before the payment address

type EventBufferLimiters added in v1.0.6

type EventBufferLimiters struct {
	InstantMessages        int `json:"instantMessages"`
	ChannelPrivateMessages int `json:"channelPrivateMessages"`
}

for limit max events per second

type EventBuffersCapacity added in v1.0.4

type EventBuffersCapacity struct {
	Auth                  int `json:"auth"`
	ContactMessage        int `json:"contactMessage"`
	ChannelMessage        int `json:"channelMessage"`
	PrivateChannelMessage int `json:"privateChannelMessage"`
	InstantMessages       int `json:"instantMessages"`
}

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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