telegram

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2023 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bot

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

Bot represents a Telegram bot that integrates advanced features such as session handling, user permissions, and administrative controls. The bot provides a mechanism for sending messages, manages sessions for different chats, enforces user access permissions, administers privilege controls, and supports localization of messages.

func NewBot

func NewBot(
	name string,
	sender Sender,
	sessionProvider chat.SessionProvider,
	model string,
	allowedUsers, adminUsers []int64,
	language language.Tag,
	adminContact string,
	currency string,
	rate float64,
	prompt string,
) *Bot

NewBot creates and initializes a new instance of Bot with the necessary dependencies. It configures the bot with mechanisms for message sending, session management, and optional user whitelists for access and administrative permissions. The printer for localized messages is also initialized based on the specified language.

Parameters:

  • name: Name of the chat bot.
  • sender: The message sending mechanism complying with the Sender interface.
  • sessionProvider: The provider for managing chat session states.
  • model: The name of the model used for sessions.
  • allowedUsers: An optional slice of user IDs permitted to interact with the bot.
  • adminUsers: An optional slice of user IDs granted administrative privileges.
  • language: The language.Tag used for localized message printing.
  • adminContact: Contact information for the bot administrator.
  • currency: A string representing the currency code (e.g., "$", "¥") used for statistics reporting.
  • rate: A float64 value representing the exchange rate or a conversion factor used for financial statistics.
  • prompt: Bot's default prompt.

Returns:

  • A pointer to the newly created Bot instance.

func (*Bot) IsUserAdmin

func (b *Bot) IsUserAdmin(userID int64) bool

IsUserAdmin checks if the user with the given ID has administrative privileges.

userID: The Telegram user ID to check for administrative status.

Returns: - true if the user is an admin, false otherwise.

func (*Bot) IsUserAllowed

func (b *Bot) IsUserAllowed(userID int64) bool

IsUserAllowed checks if the user with the given ID is allowed to interact with the bot.

userID: The Telegram user ID to check for permission.

Returns: - true if the user is allowed, false otherwise.

func (*Bot) ProcessUpdates

func (b *Bot) ProcessUpdates(ctx context.Context, updates tgbotapi.UpdatesChannel) error

ProcessUpdates listens for incoming updates from the Telegram bot API and processes each message update asynchronously.

ctx: The context to control the lifecycle of the update processing. If the context is canceled, the method will stop processing updates and return.

updates: A channel through which the Telegram bot API sends updates.

Returns: - An error if the context is canceled, otherwise runs indefinitely without returning.

func (*Bot) Reply

func (b *Bot) Reply(to *tgbotapi.Message, with string)

Reply sends a textual reply to a specific message within a Telegram chat. It constructs a message configuration targeting the original message and uses the Bot's sender to dispatch the reply. If an error occurs during the sending process, it is logged with the context of the chat and the message that was attempted to be replied to.

Parameters:

to   - A pointer to the Message instance to which the reply is directed.
       This must include the necessary identifiers to target the correct message.
with - The text content to be sent as the reply message.

No return values, but errors during message sending are logged.

func (*Bot) Send

func (b *Bot) Send(chat int64, message string)

Send dispatches a non-reply message to a specified chat in Telegram. It creates a new message configuration with the designated chat ID and message content, then sends it using the Bot's sender. If the message fails to Send, an error is logged including the chat ID and the error message.

Parameters:

chat    - The chat ID to which the message should be sent.
message - The text content of the message to be sent.

No return values, but errors during message sending are logged.

func (*Bot) Typing

func (b *Bot) Typing(ctx context.Context, chatID int64)

Typing simulates typing activity in a chat until the provided context is cancelled.

Parameters:

ctx    - The context that controls the cancellation of the typing action.
chatID - The ID of the chat where the typing action will be shown.

The function does not return a value. Errors encountered during sending the typing action are logged but not returned.

type Sender

type Sender interface {
	// Send transmits the provided chattable content to a Telegram chat.
	// This method should implement the necessary logic to handle the transmission
	// of different types of messages encapsulated by the Chattable interface.
	//
	// Parameters:
	//   - c: A Chattable instance containing the message or content to be sent.
	//        Chattable is an interface that can represent any sendable content.
	//
	// Returns:
	//   - Message: The Telegram Message object that was sent. This object contains
	//              details about the message, such as its ID, the chat it was sent to,
	//              and the time it was sent.
	//   - error: An error encountered during the sending process. If the message was
	//            sent successfully, this will be nil.
	Send(c tgbotapi.Chattable) (tgbotapi.Message, error)

	// Request sends the chattable content to Telegram's API and returns the raw API response.
	// This method is typically used for sending messages and receiving the direct response
	// from the API without any additional processing.
	//
	// Parameters:
	//   - c: A Chattable instance containing the message or content to be sent to the Telegram API.
	//        As with Send, this represents any type of content that conforms to the Chattable interface.
	//
	// Returns:
	//   - APIResponse: A pointer to the APIResponse from Telegram. This response includes
	//                  the raw status, the result, and any error message from the API.
	//   - error: An error encountered while making the request to Telegram's API. If the
	//            request was successful, this will be nil.
	Request(c tgbotapi.Chattable) (*tgbotapi.APIResponse, error)
}

Sender defines an interface for sending messages through Telegram's API. Implementers of Sender must be able to handle the sending of various types of chattable content, such as text messages, photos, audio, etc.

Jump to

Keyboard shortcuts

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