asks

package
v0.16.399 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: AGPL-3.0 Imports: 18 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package asks is an agent asking a person: a question or an approval sent through one of the Telegram bots the person linked (internal/notifybots), with the person tagged, buttons to answer, and the answer handed back to the agent that waits for it. The person may also answer from the browser. The runtime holds every ask in a file of its data folder: the page lists them, the agent waits on one, the bot's poller closes them.

Index

Constants

View Source
const (
	MaxQuestion = 1500
	MaxDetails  = 2000
	MaxOptions  = 6
	MaxOption   = 40
	MaxLink     = 500
	// MaxReplies is how many messages one ask keeps.
	MaxReplies = 30
	// HourlyCap is how many asks the whole machine may send in an hour, whatever the conversations.
	HourlyCap = 60
)

Limits of what an agent sends.

View Source
const (
	ReasonDisabled    = "disabled"       // the person turned asks off
	ReasonNoBot       = "no-bot"         // no bot with a chat may carry asks
	ReasonBotNotFound = "bot-not-found"  // the named bot is not one of them
	ReasonUnknownTo   = "unknown-person" // not a contact, and only contacts may be tagged
	ReasonBadTo       = "bad-person"     // neither a contact nor an @username
	ReasonEmpty       = "empty"          // no question
	ReasonTooLong     = "too-long"       // question, details, an option or the link too long
	ReasonRateLimited = "rate-limited"   // too many asks in the last hour
	ReasonNotFound    = "not-found"      // no ask of that id
	ReasonClosed      = "closed"         // the ask is closed already
	ReasonBadChoice   = "bad-choice"     // not a choice of this ask
	ReasonFailed      = "failed"         // the file could not be read or written
)

Reasons of a refusal, as the agents and the page are told them.

View Source
const (
	DecisionApproved = "approved"
	DecisionChanges  = "changes"
	// DecisionComment is an approval answered in words, not with a button: the agent reads it, it does not approve.
	DecisionComment = "comment"
)

Decisions of an approval.

View Source
const File = "asks.json"

File is the asks' file in the runtime's v2 data folder.

View Source
const Keep = 300

Keep is how many asks the file keeps: the oldest closed ones go first.

View Source
const (
	MaxContacts = 100
)

Limits of the settings.

View Source
const SettingsFile = "asks-settings.json"

SettingsFile is the person's settings of asks, beside the asks.

Variables

View Source
var ErrNotFound = errors.New("asks: no such ask")

ErrNotFound: no ask has that id.

Functions

func CleanUsername

func CleanUsername(s string) string

CleanUsername is a pasted @username or t.me link as a bare username; "" when it is not one.

Types

type AgentContact

type AgentContact struct {
	Name     string `json:"name"`
	Username string `json:"username,omitempty"`
	Note     string `json:"note,omitempty"`
	Approver bool   `json:"approver,omitempty"`
}

AgentContact is a contact as an agent sees it.

type Ask

type Ask struct {
	ID       string   `json:"id"`
	Kind     Kind     `json:"kind"`
	Status   Status   `json:"status"`
	Question string   `json:"question"`
	Details  string   `json:"details,omitempty"`
	Link     string   `json:"link,omitempty"`
	Options  []string `json:"options,omitempty"`
	To       *Person  `json:"to,omitempty"`

	BotID     string `json:"botId"`
	BotName   string `json:"botName"`
	ChatTitle string `json:"chatTitle"`
	ChatID    int64  `json:"chatId"`
	MessageID int64  `json:"messageId"`
	// HTML is the sent text, kept to close the message in the chat with the outcome under it.
	HTML   string `json:"html"`
	Silent bool   `json:"silent,omitempty"`

	// Who asked: the agent's tmux session, its conversation, workspace and the conversation's title.
	Session      string `json:"session,omitempty"`
	Conversation string `json:"conversation,omitempty"`
	Workspace    string `json:"workspace,omitempty"`
	Agent        string `json:"agent,omitempty"`

	CreatedAt int64 `json:"createdAt"`
	Deadline  int64 `json:"deadline"`
	ClosedAt  int64 `json:"closedAt,omitempty"`

	Decision string  `json:"decision,omitempty"`
	Answer   *Reply  `json:"answer,omitempty"`
	Replies  []Reply `json:"replies,omitempty"`
	// SeenAt is when the agent took the outcome; SeenReplies how many replies it had read then.
	SeenAt      int64  `json:"seenAt,omitempty"`
	SeenReplies int    `json:"seenReplies,omitempty"`
	Error       string `json:"error,omitempty"`
}

Ask is one question or approval as it is stored.

func (Ask) View

func (a Ask) View() View

View is the ask for the page and the agent.

type Contact

type Contact struct {
	ID         string `json:"id"`
	Name       string `json:"name"`
	Username   string `json:"username,omitempty"`
	TelegramID int64  `json:"telegramId,omitempty"`
	// Me: the owner of this machine. An ask to them also reaches their browser.
	Me bool `json:"me,omitempty"`
	// CanApprove: may approve an approval that is not addressed to them.
	CanApprove bool `json:"canApprove,omitempty"`
	// Note is the person's hint to the agents: what to ask this person about.
	Note string `json:"note,omitempty"`
}

Contact is a person the agents may tag, by the name the agents use.

func (Contact) Person

func (c Contact) Person() Person

Person is the contact as an addressee.

type Kind

type Kind string

Kind is what the agent wants.

const (
	KindQuestion Kind = "question" // an answer in words (or one of the offered options)
	KindApproval Kind = "approval" // approve or ask for changes
)

type Person

type Person struct {
	Name       string `json:"name,omitempty"`
	Username   string `json:"username,omitempty"` // Telegram @username without the @
	TelegramID int64  `json:"telegramId,omitempty"`
}

Person is someone an ask is addressed to or a message is from.

func (Person) Label

func (p Person) Label() string

Label is how the person is shown: @username, else the name.

type Quiet

type Quiet struct {
	Enabled bool   `json:"enabled"`
	From    string `json:"from"` // "22:00", the runtime's local time
	To      string `json:"to"`   // "09:00"
}

Quiet are the hours when an ask is delivered without a sound.

func (Quiet) InQuiet

func (q Quiet) InQuiet(t time.Time) bool

InQuiet says whether t falls into the quiet hours (which may run over midnight).

type Refusal

type Refusal struct {
	Reason string
	Detail string
}

Refusal is a reason the caller is told, with Telegram's words when it has them.

func RefusalOf

func RefusalOf(err error) Refusal

RefusalOf is what to tell the caller of any error of the service.

func (*Refusal) Error

func (r *Refusal) Error() string

type Reply

type Reply struct {
	From Person `json:"from"`
	Text string `json:"text"`
	// Choice is the button pressed (an approval's decision or an option's text); "" for a message in words.
	Choice string `json:"choice,omitempty"`
	Via    string `json:"via"` // telegram | web
	At     int64  `json:"at"`
}

Reply is a message of a person that belongs to an ask: the answer, or what was written after it.

type Request

type Request struct {
	Kind     Kind     `json:"kind"`
	Question string   `json:"question"`
	Details  string   `json:"details"`
	Link     string   `json:"link"`
	Options  []string `json:"options"`
	// To is a contact's name or id, or a Telegram @username; "" asks the chat as a whole.
	To string `json:"to"`
	// Bot is a bot's name or id; "" is the default one.
	Bot string `json:"bot"`
	// TimeoutSec is how long the agent waits; 0 is the person's default.
	TimeoutSec int `json:"timeoutSec"`

	Session      string `json:"session"`
	Conversation string `json:"-"`
	Workspace    string `json:"-"`
	Agent        string `json:"-"`
}

Request is what an agent asks.

type Service

type Service struct {
	Store    *Store
	Settings *SettingsStore
	Bots     *notifybots.Store
	Telegram *notifybots.Client
	Now      func() time.Time
	// Changed is told of every change of an ask (new, answered, closed): the browser notification hangs here.
	Changed func(before *Ask, after Ask)
	// PollWait is the long poll of a bot's poller (25 s when zero); tests shorten it.
	PollWait time.Duration
	// ErrPause is the pause after a failed or empty poll (2 s when zero); tests shorten it.
	ErrPause time.Duration
	// contains filtered or unexported fields
}

Service is the asks of this runtime.

func (*Service) AnswerFromWeb

func (s *Service) AnswerFromWeb(id, choice, text string, by Person) (Ask, error)

AnswerFromWeb is the person's answer in the browser: a choice (approved, changes, an option) or words, or both.

func (*Service) Cancel

func (s *Service) Cancel(id string) (Ask, error)

Cancel calls an ask off.

func (*Service) Create

func (s *Service) Create(ctx context.Context, r Request) (Ask, error)

Create sends an ask and stores it; the agent then waits on its id.

func (*Service) Get

func (s *Service) Get(id string) (Ask, error)

Get is one ask, closed by its deadline when it passed.

func (*Service) List

func (s *Service) List() ([]Ask, error)

List is every ask, newest first, the overdue ones closed.

func (*Service) MarkSeen

func (s *Service) MarkSeen(id string) (Ask, error)

MarkSeen records that the agent read the ask's outcome and its replies so far.

func (*Service) Start

func (s *Service) Start()

Start resumes the pollers of the asks that wait (after a restart of the runtime).

func (*Service) Stop

func (s *Service) Stop()

Stop ends every poller and waits until they are gone, so nothing writes to the store after it returns (tests; the process ends with the runtime otherwise).

func (*Service) Wait

func (s *Service) Wait(ctx context.Context, id string) (Ask, error)

Wait returns the ask once it is closed, or as it is when ctx ends first (a client's long poll).

type Settings

type Settings struct {
	// Enabled: agents may ask people. On by default; nothing is sent while no bot has a chat.
	Enabled bool `json:"enabled"`
	// DefaultBot is the bot (its id) an ask goes through when the agent names none; "" is the first bot with a chat.
	DefaultBot string `json:"defaultBot"`
	// Bots are the bots asks may go through; empty is every bot with a chat.
	Bots []string `json:"bots"`
	// OnlyContacts: an agent may tag only the contacts below, not any @username.
	OnlyContacts bool      `json:"onlyContacts"`
	Contacts     []Contact `json:"contacts"`
	// DefaultTimeoutMin is how long an agent waits when it says nothing; MaxTimeoutMin the longest it may.
	DefaultTimeoutMin int `json:"defaultTimeoutMin"`
	MaxTimeoutMin     int `json:"maxTimeoutMin"`
	// PerHour is how many asks one conversation may send in an hour.
	PerHour int   `json:"perHour"`
	Quiet   Quiet `json:"quiet"`
	// ShowSource: the message names the conversation that asks.
	ShowSource bool `json:"showSource"`
	// BrowserNotify: an ask to the owner (a contact marked Me) also comes as a browser notification.
	BrowserNotify bool `json:"browserNotify"`
}

Settings are the person's rules for asks on this machine.

func Check

func Check(in Settings) (Settings, error)

Check is the settings as they are stored, or what is wrong with them.

func Defaults

func Defaults() Settings

Defaults are the settings of a machine where the person changed nothing.

func (Settings) AgentContacts

func (s Settings) AgentContacts() []AgentContact

AgentContacts are the contacts for `harnessly ask people`.

func (Settings) FindContact

func (s Settings) FindContact(ref string) (Contact, bool)

FindContact is a contact by its name (any case), its @username or its id.

type SettingsError

type SettingsError struct{ Field string }

SettingsError names the field the page shows the refusal at.

func (*SettingsError) Error

func (e *SettingsError) Error() string

type SettingsStore

type SettingsStore struct {
	Path string
	// contains filtered or unexported fields
}

SettingsStore reads and writes the settings file.

func OpenSettings

func OpenSettings(dataDir string) *SettingsStore

OpenSettings is the process's one SettingsStore of a data folder.

func (*SettingsStore) Get

func (s *SettingsStore) Get() (Settings, error)

Get is the settings, the defaults under what the person changed.

func (*SettingsStore) Learn

func (s *SettingsStore) Learn(p Person)

Learn fills in a contact's Telegram id once the person wrote under their @username, so they can be tagged even after they change it.

func (*SettingsStore) Put

func (s *SettingsStore) Put(in Settings) (Settings, error)

Put checks and writes the settings; the stored ones come back.

type Status

type Status string

Status is where an ask is.

const (
	StatusWaiting   Status = "waiting"   // sent, no answer yet
	StatusAnswered  Status = "answered"  // the person answered
	StatusTimeout   Status = "timeout"   // no answer in time
	StatusCancelled Status = "cancelled" // the agent or the person called it off
	StatusFailed    Status = "failed"    // could not be sent
)

func (Status) Final

func (s Status) Final() bool

Final says the ask will not change any more (late replies are still recorded).

type Store

type Store struct {
	Path string
	// contains filtered or unexported fields
}

Store reads and writes the asks' file; every writer of this process goes through one Store.

func Open

func Open(dataDir string) *Store

Open is the process's one Store of a data folder.

func (*Store) Add

func (s *Store) Add(a Ask) error

Add stores a new ask.

func (*Store) Get

func (s *Store) Get(id string) (Ask, error)

Get is one ask by id.

func (*Store) List

func (s *Store) List() ([]Ask, error)

List is every ask, newest first.

func (*Store) Offset

func (s *Store) Offset(bot string) int64

Offset is the next update a bot's poller asks for.

func (*Store) SetOffset

func (s *Store) SetOffset(bot string, offset int64) error

SetOffset keeps a bot's next update.

func (*Store) Update

func (s *Store) Update(id string, change func(*Ask) error) (Ask, error)

Update changes one ask under the lock; the changed ask is returned. A change that returns errUnchanged writes nothing.

type View

type View struct {
	ID           string   `json:"id"`
	Kind         Kind     `json:"kind"`
	Status       Status   `json:"status"`
	Question     string   `json:"question"`
	Details      string   `json:"details"`
	Link         string   `json:"link"`
	Options      []string `json:"options"`
	To           *Person  `json:"to"`
	Bot          string   `json:"bot"`
	Chat         string   `json:"chat"`
	Silent       bool     `json:"silent"`
	Conversation string   `json:"conversation"`
	Agent        string   `json:"agent"`
	CreatedAt    int64    `json:"createdAt"`
	Deadline     int64    `json:"deadline"`
	ClosedAt     int64    `json:"closedAt"`
	Decision     string   `json:"decision"`
	Answer       *Reply   `json:"answer"`
	Replies      []Reply  `json:"replies"`
	SeenAt       int64    `json:"seenAt"`
	SeenReplies  int      `json:"seenReplies"`
	Error        string   `json:"error"`
}

View is an ask as the page and the agent see it: without the chat's id, the sent HTML and the agent's session.

func Views

func Views(asks []Ask) []View

Views are asks for the page.

Jump to

Keyboard shortcuts

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