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
- Variables
- func CleanUsername(s string) string
- type AgentContact
- type Ask
- type Contact
- type Kind
- type Person
- type Quiet
- type Refusal
- type Reply
- type Request
- type Service
- func (s *Service) AnswerFromWeb(id, choice, text string, by Person) (Ask, error)
- func (s *Service) Cancel(id string) (Ask, error)
- func (s *Service) Create(ctx context.Context, r Request) (Ask, error)
- func (s *Service) Get(id string) (Ask, error)
- func (s *Service) List() ([]Ask, error)
- func (s *Service) MarkSeen(id string) (Ask, error)
- func (s *Service) Start()
- func (s *Service) Stop()
- func (s *Service) Wait(ctx context.Context, id string) (Ask, error)
- type Settings
- type SettingsError
- type SettingsStore
- type Status
- type Store
- type View
Constants ¶
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.
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.
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.
const File = "asks.json"
File is the asks' file in the runtime's v2 data folder.
const Keep = 300
Keep is how many asks the file keeps: the oldest closed ones go first.
const (
MaxContacts = 100
)
Limits of the settings.
const SettingsFile = "asks-settings.json"
SettingsFile is the person's settings of asks, beside the asks.
Variables ¶
var ErrNotFound = errors.New("asks: no such ask")
ErrNotFound: no ask has that id.
Functions ¶
func CleanUsername ¶
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.
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.
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.
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.
type Refusal ¶
Refusal is a reason the caller is told, with Telegram's words when it has them.
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 ¶
AnswerFromWeb is the person's answer in the browser: a choice (approved, changes, an option) or words, or both.
func (*Service) MarkSeen ¶
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).
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 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`.
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.
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 )
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.
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.