New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

github.com/unkeep/telegram-bot-dialog

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/unkeep/telegram-bot-dialog

  • v1.0.0
  • Source
  • Go
  • Socket score

Version published
Created
Source

Simple library for managing Telegram bot stateful dialogs

Go Reference

Example

First, ensure the library is installed and up to date by running go get -u github.com/unkeep/telegram-bot-dialog.

This is a very simple bot that asks username and password in a dialog manner

package main

import (
	"context"
	"log"

	tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
	"github.com/unkeep/telegram-bot-dialog/tgbotdlg"
)

func main() {
	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
	if err != nil {
		log.Panic(err)
	}

	var dialogsStorage tgbotdlg.Storage // TODO: provide dialogs storage

	dialogsDispatcher := tgbotdlg.NewDispatcher(dialogsStorage)
	dialogsDispatcher.RegisterDialogs(
		func() tgbotdlg.Dialog {
			return &rootDialog{bot: bot}
		},
		func() tgbotdlg.Dialog {
			return &enterUserNameDialog{bot: bot}
		},
		func() tgbotdlg.Dialog {
			return &enterPasswordDialog{bot: bot}
		},
	)

	u := tgbotapi.NewUpdate(0)
	u.Timeout = 60

	updates := bot.GetUpdatesChan(u)

	for update := range updates {
		if err := dialogsDispatcher.HandleUpdate(context.Background(), &update); err != nil {
			log.Panic(err)
		}
	}
}

type rootDialog struct {
	bot *tgbotapi.BotAPI
	IsLoggedIn bool `json:"is_logged_in,omitempty"`
}

func (d *rootDialog) Name() string {
	return "root"
}

func (d *rootDialog) HandleUpdate(ctx context.Context, upd tgbotdlg.Update) (tgbotdlg.Dialog, error) {
	msg := upd.Message
	if msg == nil {
		return d, nil
	}
	
	if msg.Text == "/start" {
		if _, err := d.bot.Send(tgbotapi.NewMessage(msg.Chat.ID, "Hi! I'm a bot")); err != nil {
			return nil, err
		}
		return d, nil // stay on the same dialog
	}

	if msg.Text == "/login" {
		if _, err := d.bot.Send(tgbotapi.NewMessage(msg.Chat.ID, "Enter a username")); err != nil {
			return nil, err
		}
		return &enterUserNameDialog{bot: d.bot}, nil
	}

	if _, err := d.bot.Send(tgbotapi.NewMessage(msg.Chat.ID, "Sorry, I don't understand you")); err != nil {
		return nil, err
	}
	return d, nil
}

type enterUserNameDialog struct {
	bot *tgbotapi.BotAPI
}

func (d *enterUserNameDialog) Name() string {
	return "enter_username"
}

func (d *enterUserNameDialog) HandleUpdate(ctx context.Context, upd tgbotdlg.Update) (tgbotdlg.Dialog, error) {
	msg := upd.Message
	if msg == nil {
		return d, nil
	}
	
	username := msg.Text
	
	if _, err := d.bot.Send(tgbotapi.NewMessage(msg.Chat.ID, "Enter a password")); err != nil {
		return nil, err
	}
	
	return &enterPasswordDialog{
		bot:      d.bot,
		Username: username,
	}, nil
}

type enterPasswordDialog struct {
	bot      *tgbotapi.BotAPI
	Username string `json:"username,omitempty"`
}

func (d *enterPasswordDialog) Name() string {
	return "enter_password"
}

func (d *enterPasswordDialog) HandleUpdate(ctx context.Context, upd tgbotdlg.Update) (tgbotdlg.Dialog, error) {
	msg := upd.Message
	if msg == nil {
		return d, nil
	}
	
	password := msg.Text
	login(d.Username, password)

	if _, err := d.bot.Send(tgbotapi.NewMessage(msg.Chat.ID, "You have successfully logged in")); err != nil {
		return nil, err
	}

	return &rootDialog{bot: d.bot, IsLoggedIn: true}, nil
}

func login(username string, password string) {}

FAQs

Package last updated on 11 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc