messenger

package
v0.26.4 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2026 License: MIT Imports: 40 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CalendarAPILimit    = 20 // 20 req/min per user
	CalendarWindow      = 1 * time.Minute
	CalendarMinDelay    = CalendarWindow / CalendarAPILimit
	CalendarMaxResults  = 100
	CalendarCredentials = "assets/calendar_credentials.json" // embedded Google Calendar credentials file
	CalendarQueue       = "calendar-queue"

	CalendarExamSep = " - Ispit iz: "
)
View Source
const (
	DiscordAPILimit = 10 // 10 API req/min per user/IP
	DiscordWindow   = 1 * time.Minute
	DiscordMinDelay = DiscordWindow / DiscordAPILimit
	DiscordQueue    = "discord-queue"
)
View Source
const (
	MailSendLimit = 20 // 20 emails per 1 hour
	MailWindow    = 1 * time.Hour
	MailMinDelay  = MailWindow / MailSendLimit
	MailSubject   = "Nova ocjena/ispit iz e-Dnevnika"
	MailQueue     = "mail-queue"
)
View Source
const (
	SlackAPILImit = 20 // typically 20 req/min per user
	SlackWindow   = 1 * time.Minute
	SlackMinDelay = SlackWindow / SlackAPILImit
	SlackQueue    = "slack-queue"
)
View Source
const (
	TelegramAPILimit = 20 // 20 API req/min per user
	TelegramWindow   = 1 * time.Minute
	TelegramMinDelay = TelegramWindow / TelegramAPILimit
	TelegramQueue    = "telegram-queue"
)
View Source
const (
	WhatsAppDBName       = ".e-dnevnik.sqlite"
	WhatsAppDBConnstring = "file:%v?_pragma=foreign_keys(1)&_pragma=busy_timeout=10000"
	WhatsAppDisplayName  = "Chrome (Linux)"
	WhatsAppOS           = "Linux"
	WhatsAppAPILimit     = 10 // 10 req/min per user/IP
	WhatsAppWindow       = 1 * time.Minute
	WhatsAppMinDelay     = WhatsAppWindow / WhatsAppAPILimit
	WhatsAppQueue        = "whatsapp-queue"
)

Variables

View Source
var (
	ErrCalendarReadingCreds = errors.New("unable to read credentials file")
	ErrCalendarParsingCreds = errors.New("unable to parse credentials file")
	ErrCalendarNotFound     = errors.New("unable to find Google Calendar ID")

	CalendarQueueName = []byte(CalendarQueue)
	CalendarVersion   = version.ReadVersion("google.golang.org/api")
)
View Source
var (
	ErrDiscordEmptyAPIKey     = errors.New("empty Discord API key")
	ErrDiscordEmptyUserIDs    = errors.New("empty list of Discord User IDs")
	ErrDiscordCreatingSession = errors.New("error creating Discord session")
	ErrDiscordCreatingChannel = errors.New("error creating Discord channel")
	ErrDiscordSendingMessage  = errors.New("error sending Discord message")

	DiscordQueueName = []byte(DiscordQueue)

	DiscordVersion = version.ReadVersion("github.com/bwmarrin/discordgo")
)
View Source
var (
	ErrMailInvalidPort     = errors.New("invalid or missing SMTP port, will try with default 587/tcp")
	ErrMailDialer          = errors.New("failed to create mail delivery client")
	ErrMailSendingMessages = errors.New("error sending mail messages")

	MailQueueName = []byte(MailQueue)
	MailVersion   = version.ReadVersion("github.com/wneessen/go-mail")
)
View Source
var (
	ErrSlackEmptyAPIKey    = errors.New("empty Slack API key")
	ErrSlackEmptyUserIDs   = errors.New("empty list of Slack Chat IDs")
	ErrSlackSendingMessage = errors.New("error sending Slack message")
	ErrSlackConnect        = errors.New("unable to connect to Slack API, retrying")
	ErrSlackInvalidAuth    = errors.New("invalid Slack auth token")
	ErrSlackDisconnect     = errors.New("disconnected from Slack API socket")
	ErrSlackWrite          = errors.New("error while sending a message")

	SlackQueueName = []byte(SlackQueue)

	SlackVersion = version.ReadVersion("github.com/slack-go/slack")
)
View Source
var (
	ErrTelegramSession        = errors.New("error creating Telegram session")
	ErrTelegramEmptyAPIKey    = errors.New("empty Telegram API key")
	ErrTelegramEmptyUserIDs   = errors.New("empty list of Telegram Chat IDs")
	ErrTelegramInvalidChatID  = errors.New("invalid Telegram Chat ID")
	ErrTelegramSendingMessage = errors.New("error sending Telegram message")

	TelegramQueueName = []byte(TelegramQueue)

	TelegramVersion = version.ReadVersion("github.com/go-telegram/bot")
)
View Source
var (
	ErrWhatsAppEmptyUserIDs   = errors.New("empty list of WhatsApp UserIDs (JIDs)")
	ErrWhatsAppUnableConnect  = errors.New("unable to connect to WhatsApp database")
	ErrWhatsAppUnableUpgrade  = errors.New("unable to upgrade WhatsApp database")
	ErrWhatsAppUnableDeviceID = errors.New("unable to get WhatsApp device ID")
	ErrWhatsAppUnableGroups   = errors.New("unable to list WhatsApp groups")
	ErrWhatsAppFailConnect    = errors.New("failed to connect to WhatsApp")
	ErrWhatsAppFailQR         = errors.New("failed to get WhatsApp QR link channel")
	ErrWhatsAppFailLink       = errors.New("failed to link with WhatsApp")
	ErrWhatsAppFailLinkDevice = errors.New("linking to WhatsApp is successful, but device ID is missing")
	ErrWhatsAppLoggedout      = errors.New("logged out from WhatsApp, please link again")
	ErrWhatsAppInvalidJID     = errors.New("cannot parse recipient JID")
	ErrWhatsAppSendingMessage = errors.New("error sending WhatsApp message")
	ErrWhatsAppDisconnected   = errors.New("WhatsApp client disconnected, will auto-reconnect")
	ErrWhatsAppOutdated       = errors.New("WhatsApp Go library version is outdated, please update")
	ErrWhatsAppBan            = errors.New("WhatsApp device is temporarily banned")

	WhatsAppQueueName = []byte(WhatsAppQueue)

	WhatsAppVersion = version.ReadVersion("go.mau.fi/whatsmeow")
)

Functions

func Calendar added in v0.10.0

func Calendar(ctx context.Context, eDB *db.Edb, ch <-chan msgtypes.Message, name, tokFile string, retries uint) error

Calendar sends messages through the Google Calendar API to the specified calendar.

It takes the following parameters: - ctx: the context.Context object for handling deadlines and cancellations. - eDB: the database instance for checking failed messages. - ch: a channel for receiving messages to be sent. - name: the name of the calendar to be used. - tokFile: the path to the file containing the OAuth2 token. - retries: the number of times to retry sending a message in case of failure.

It returns an error indicating any failures that occurred during the process.

func Discord

func Discord(ctx context.Context, eDB *db.Edb, ch <-chan msgtypes.Message, token string, userIDs []string, retries uint) error

Discord sends messages through the Discord API.

It takes the following parameters: - ctx: the context.Context object for handling deadlines and cancellations. - eDB: the database instance for checking failed messages. - ch: a channel for receiving messages to be sent. - token: the Discord API key. - userIDs: a slice of strings containing the IDs of the recipients. - retries: the number of times to retry sending a message in case of failure.

It returns an error indicating any failures that occurred during the process.

func InitCalendar added in v0.14.0

func InitCalendar(ctx context.Context, tokFile, name string) (*calendar.Service, string, error)

InitCalendar initializes a Google Calendar service and retrieves the calendar ID.

ctx: The context.Context for the function. tokFile: The path to the token file. name: The name of the calendar. returns: - *calendar.Service: A pointer to the calendar.Service. - string: The calendar ID. - error: Any error that occurred during initialization.

func Mail

func Mail(ctx context.Context, eDB *db.Edb, ch <-chan msgtypes.Message, server, port, username, password, from, subject string, to []string, retries uint) error

Mail sends messages through the mail service to the specified recipients.

It takes the following parameters: - ctx: the context.Context object for cancellation and timeouts. - eDB: the database instance for checking and storing failed messages. - ch: the channel from which to receive messages. - server: the address of the mail server. - port: the port number for the mail server as a string. - username: the username for authentication. - password: the password for authentication. - from: the email address of the sender. - subject: the subject of the email. - to: a slice of email addresses of the recipients. - retries: the number of retry attempts for sending the message.

The function processes all failed messages and new messages, sending them through the mail service. It uses a rate limiter to control the message sending rate and supports retry attempts for sending failures. It logs invalid ports and sets a default port if necessary.

func Slack

func Slack(ctx context.Context, eDB *db.Edb, ch <-chan msgtypes.Message, token string, chatIDs []string, retries uint) error

Slack sends messages through the Slack API.

ctx: the context in which the function is executed. eDB: the database instance for checking failed messages. ch: the channel from which messages are received. token: the Slack API key. chatIDs: the IDs of the recipients. retries: the number of retries in case of failure. error: an error if there was a problem sending the message.

func Telegram

func Telegram(ctx context.Context, eDB *db.Edb, ch <-chan msgtypes.Message, apiKey string, chatIDs []string, retries uint) error

Telegram sends messages through the Telegram API to the specified Telegram chat IDs.

It takes the following parameters: - ctx: the context.Context object for managing the execution of the function. - eDB: the database instance for checking and storing failed messages. - ch: the channel from which to receive messages. - apiKey: the Telegram API key. - chatIDs: a slice of strings containing the Telegram chat IDs to send the message to. - retries: the number of retry attempts for sending the message.

The function formats the message as HTML and attempts to send it to each chat ID. It logs errors for invalid chat IDs, sending failures, and stores failed messages for retry. It uses rate limiting and supports retries with delay.

func WhatsApp added in v0.19.0

func WhatsApp(ctx context.Context, eDB *db.Edb, ch <-chan msgtypes.Message, userIDs, groups []string, retries uint) error

WhatsApp sends messages through the WhatsApp API to the specified user IDs or groups.

It takes the following parameters: - ctx: the context.Context object for managing the execution of the function. - eDB: the database instance for checking and storing failed messages. - ch: the channel from which to receive messages. - userIDs: a slice of strings containing the WhatsApp user IDs (JIDs) to send the message to. - groups: a slice of strings containing the names of the WhatsApp groups to send the message to. - retries: the number of retry attempts for sending the message.

The function processes all failed messages, finds named groups and appends them to the userIDs, processes all new messages and sends them to the specified user IDs or groups. It logs errors for invalid chat IDs, sending failures, and stores failed messages for retry. It uses rate limiting and supports retries with delay.

Types

This section is empty.

Jump to

Keyboard shortcuts

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