telegram

package module
v0.13.8 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2024 License: MIT Imports: 21 Imported by: 6

README

bots-fw-telegram

Telegram module for Strongo bots framework

Structure & key concepts

The tgWebhookHandler struct is implementing botsfw.WebhookHandler interface and is an entry point for all incoming requests from Telegram. To create it you need to call NewTgWebhookHandler() function.

Registering records maker

Setting up dev environment for Telegram bots development

Tunneling to local development environment

To expose local server to the Internet we use ngrok.

ngrok http 4300

Make sure that you have started local GAE server & Firestore emulators - follow instructions from README.md.

After ngrok started you will see something like:

Forwarding    https://****-***-**.ngrok-free.app -> http://localhost:4300

You would need to register the forwarding URL for bot you are testing with Telegram by calling this url:

https://****-***-**.ngrok-free.app/bot/tg/set-webhook?code=BOT_CODE

where ****-***-** is the forwarding URL from ngrok output and BOT_CODE is the code of the bot you are testing.

The bot will be registered using secret tokens that you should set using environment variables:

TELEGRAM_BOT_TOKEN_<BOT_CODE>=<TELEGRAM_BOT_TOKEN>

You can create a personal bot for testing purposes using BotFather.

The bot with the given code should be registered in your app and the value is CASE SENSITIVE.

Documentation

Index

Constants

View Source
const (
	// TgMessageTypeRegular is 'message'
	TgMessageTypeRegular = "message"

	// TgMessageTypeEdited is 'edited_message'
	TgMessageTypeEdited = "edited_message"

	// TgMessageTypeChannelPost is 'channel_post'
	TgMessageTypeChannelPost = "channel_post"

	// TgMessageTypeEditedChannelPost is 'edited_channel_post'
	TgMessageTypeEditedChannelPost = "edited_channel_post"
)
View Source
const PlatformID = "telegram"

PlatformID is 'telegram'

Variables

View Source
var CallbackCurrent botsfw.MessageUID = &callbackCurrent{}

CallbackCurrent is what?

View Source
var Platform botsfw.BotPlatform = platform{}

Platform is a bots platform descriptor (in this case - for Telegram)

Functions

func BaseTgChatDtoMaker added in v0.6.0

func BaseTgChatDtoMaker(botID string) (botChat botsfwmodels.BotChatData, err error)

func BaseTgUserDtoMaker added in v0.6.0

func BaseTgUserDtoMaker(botID string) (botsfwmodels.PlatformUserData, error)

func EditMessageOnCallbackQuery

func EditMessageOnCallbackQuery(whcbq botinput.WebhookCallbackQuery, parseMode, text string) *tgbotapi.EditMessageTextConfig

EditMessageOnCallbackQuery creates edit message

func NewBotRecordsFieldsSetter added in v0.6.0

func NewBotRecordsFieldsSetter(
	setAppUserFields func(appUser botsfwmodels.AppUserData, sender botinput.WebhookSender) error,
) botsfw.BotRecordsFieldsSetter

func NewTelegramBot

func NewTelegramBot(
	environment string,
	profile botsfw.BotProfile,
	code, token, paymentTestToken, paymentToken, gaToken string,
	locale i18n.Locale,
	getDatabase botsfw.DbGetter,
	getAppUser botsfw.AppUserGetter,
) botsfw.BotSettings

NewTelegramBot creates definition of new telegram bot

func NewTelegramWebhookHandler

func NewTelegramWebhookHandler(
	botContextProvider botsfw.BotContextProvider,
	translatorProvider botsfw.TranslatorProvider,
	setAppUserFields func(botsfwmodels.AppUserData, botinput.WebhookSender) error,
) botsfw.WebhookHandler

NewTelegramWebhookHandler creates new Telegram webhooks handler

func NewTelegramWebhookInput

func NewTelegramWebhookInput(update *tgbotapi.Update, logRequest func()) (botinput.WebhookInput, error)

NewTelegramWebhookInput maps telegram update struct to bots framework interface

Types

type CallbackAnswer

type CallbackAnswer tgbotapi.AnswerCallbackQueryConfig

CallbackAnswer is callback answer message

func (CallbackAnswer) BotMessageType

func (CallbackAnswer) BotMessageType() botsfw.BotMessageType

BotMessageType returns BotMessageTypeCallbackAnswer

type ChatMessageUID

type ChatMessageUID struct {
	ChatID    int64
	MessageID int
}

ChatMessageUID is what?

func NewChatMessageUID

func NewChatMessageUID(chatID int64, messageID int) *ChatMessageUID

NewChatMessageUID create new ChatMessageUID

func (ChatMessageUID) UID

func (m ChatMessageUID) UID() string

UID return unique ID of the message

type DataStore added in v0.5.0

type DataStore interface {
	TgChatInstanceDal
}
type ExportChatInviteLink tgbotapi.ExportChatInviteLink

ExportChatInviteLink is TG message

func (ExportChatInviteLink) BotMessageType

func (ExportChatInviteLink) BotMessageType() botsfw.BotMessageType

BotMessageType returns BotMessageTypeExportChatInviteLink

type InlineBotMessage

type InlineBotMessage tgbotapi.InlineConfig

InlineBotMessage is wrapper for Telegram bot message

func (InlineBotMessage) BotMessageType

func (InlineBotMessage) BotMessageType() botsfw.BotMessageType

BotMessageType returns BotMessageTypeInlineResults

type InlineMessageUID

type InlineMessageUID struct {
	InlineMessageID string
}

InlineMessageUID is inline message UID

func NewInlineMessageUID

func NewInlineMessageUID(inlineMessageID string) *InlineMessageUID

NewInlineMessageUID creates new inline message UID

func (InlineMessageUID) UID

func (m InlineMessageUID) UID() string

UID is unique ID of the message

type LeaveChat

type LeaveChat tgbotapi.LeaveChatConfig

LeaveChat is leave chat message from bot

func (LeaveChat) BotMessageType

func (LeaveChat) BotMessageType() botsfw.BotMessageType

BotMessageType return BotMessageTypeLeaveChat

type TgChatInstanceDal

type TgChatInstanceDal interface {
	GetTelegramChatInstanceByID(c context.Context, id string) (tgChatInstance botsfwtgmodels.TgChatInstanceData, err error)
	NewTelegramChatInstance(chatInstanceID string, chatID int64, preferredLanguage string) (tgChatInstance botsfwtgmodels.TgChatInstanceData)
	SaveTelegramChatInstance(c context.Context, id string, tgChatInstance botsfwtgmodels.TgChatInstanceData) (err error)
}

TgChatInstanceDal is Data Access Layer for telegram chat instance Data

type TgMessageType

type TgMessageType string

TgMessageType represents tpye of Telegram message

type TgWebhookCallbackQuery

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

TgWebhookCallbackQuery is wrapper on callback query

func (TgWebhookCallbackQuery) BotChatID

func (twhcbq TgWebhookCallbackQuery) BotChatID() (string, error)

BotChatID returns bot chat ID

func (TgWebhookCallbackQuery) Chat

func (whi TgWebhookCallbackQuery) Chat() botinput.WebhookChat

func (TgWebhookCallbackQuery) GetData

func (twhcbq TgWebhookCallbackQuery) GetData() string

GetData returns callback query data

func (TgWebhookCallbackQuery) GetFrom

GetFrom returns sender

func (TgWebhookCallbackQuery) GetID

func (twhcbq TgWebhookCallbackQuery) GetID() string

GetID returns update ID

func (TgWebhookCallbackQuery) GetInlineMessageID

func (twhcbq TgWebhookCallbackQuery) GetInlineMessageID() string

GetInlineMessageID returns callback query inline message ID

func (TgWebhookCallbackQuery) GetMessage

func (twhcbq TgWebhookCallbackQuery) GetMessage() botinput.WebhookMessage

GetMessage returns message

func (TgWebhookCallbackQuery) GetRecipient

func (whi TgWebhookCallbackQuery) GetRecipient() botinput.WebhookRecipient

func (TgWebhookCallbackQuery) GetSender

func (whi TgWebhookCallbackQuery) GetSender() botinput.WebhookUser

func (TgWebhookCallbackQuery) GetTime

func (whi TgWebhookCallbackQuery) GetTime() time.Time

func (TgWebhookCallbackQuery) InputType

InputType return WebhookInputCallbackQuery

func (TgWebhookCallbackQuery) LogRequest

func (whi TgWebhookCallbackQuery) LogRequest()

func (TgWebhookCallbackQuery) Sequence

func (twhcbq TgWebhookCallbackQuery) Sequence() int

Sequence returns update ID

func (TgWebhookCallbackQuery) StringID

func (whi TgWebhookCallbackQuery) StringID() string

func (TgWebhookCallbackQuery) TelegramCallbackMessage

func (twhcbq TgWebhookCallbackQuery) TelegramCallbackMessage() *tgbotapi.Message

TelegramCallbackMessage returns message

func (TgWebhookCallbackQuery) TelegramChatID

func (whi TgWebhookCallbackQuery) TelegramChatID() int64

func (TgWebhookCallbackQuery) TgUpdate

func (whi TgWebhookCallbackQuery) TgUpdate() *tgbotapi.Update

type TgWebhookChat

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

TgWebhookChat is wrapper for Telegram chat

func (TgWebhookChat) GetID

func (wh TgWebhookChat) GetID() string

GetID returns telegram chat ID

func (TgWebhookChat) GetType

func (wh TgWebhookChat) GetType() string

GetType returns telegram chat type

func (TgWebhookChat) IsGroupChat

func (wh TgWebhookChat) IsGroupChat() bool

IsGroupChat indicates type of chat (group or private)

type TgWebhookInlineQuery

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

TgWebhookInlineQuery is wrapper

func (TgWebhookInlineQuery) BotChatID

func (iq TgWebhookInlineQuery) BotChatID() (string, error)

BotChatID returns bot chat ID

func (TgWebhookInlineQuery) Chat

func (whi TgWebhookInlineQuery) Chat() botinput.WebhookChat

func (TgWebhookInlineQuery) GetFrom

GetFrom returns recipient

func (TgWebhookInlineQuery) GetID

func (whi TgWebhookInlineQuery) GetID() interface{}

func (TgWebhookInlineQuery) GetInlineQueryID

func (iq TgWebhookInlineQuery) GetInlineQueryID() string

GetInlineQueryID return inline query ID

func (TgWebhookInlineQuery) GetOffset

func (iq TgWebhookInlineQuery) GetOffset() string

GetOffset returns offset

func (TgWebhookInlineQuery) GetQuery

func (iq TgWebhookInlineQuery) GetQuery() string

GetQuery returns query string

func (TgWebhookInlineQuery) GetRecipient

func (whi TgWebhookInlineQuery) GetRecipient() botinput.WebhookRecipient

func (TgWebhookInlineQuery) GetSender

func (whi TgWebhookInlineQuery) GetSender() botinput.WebhookUser

func (TgWebhookInlineQuery) GetTime

func (whi TgWebhookInlineQuery) GetTime() time.Time

func (TgWebhookInlineQuery) InputType

InputType returns WebhookInputInlineQuery

func (TgWebhookInlineQuery) LogRequest

func (whi TgWebhookInlineQuery) LogRequest()

func (TgWebhookInlineQuery) StringID

func (whi TgWebhookInlineQuery) StringID() string

func (TgWebhookInlineQuery) TelegramChatID

func (whi TgWebhookInlineQuery) TelegramChatID() int64

func (TgWebhookInlineQuery) TgUpdate

func (whi TgWebhookInlineQuery) TgUpdate() *tgbotapi.Update

type TgWebhookInput

type TgWebhookInput interface {
	TgUpdate() *tgbotapi.Update
}

TgWebhookInput is a wrapper of telegram update struct to bots framework interface

Directories

Path Synopsis
hosts

Jump to

Keyboard shortcuts

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