twipi

package
v0.0.0-...-002d596 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2022 License: LGPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package twipi contains basic abstractions around some Twilio APIs. It's designed to easily allow Twilio to be used in a Go application by providing high-level abstractions around the API.

If you're making a Twilio application that can handle replying to commands, also consider using the twicli package.

Index

Constants

View Source
const RFC2822Date = "Mon, 02 Jan 2006 15:04:05 -0700"

RFC2822Date is the RFC 2822 date format.

Variables

View Source
var ErrNoAccount = errors.New("no account found associated with given input")

ErrNoAccount is returned when no account is found for a given phone number.

Functions

This section is empty.

Types

type Account

type Account struct {
	PhoneNumber PhoneNumber
	AccountSID  string
	AuthToken   string
}

Account represents a Twilio account.

type AccountClient

type AccountClient struct {
	*twilio.RestClient
	Account Account
}

type Client

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

Client is a Twilio client. A zero-value client is ready to use.

func NewClient

func NewClient() *Client

NewClient creates a new Twilio client.

func (*Client) AddAccount

func (c *Client) AddAccount(account Account)

AddAccount adds a Twilio account to the client.

func (*Client) FromPhone

func (c *Client) FromPhone(number PhoneNumber) *AccountClient

FromPhone returns the account associated with the given phone number.

func (*Client) ReplySMS

func (c *Client) ReplySMS(ctx context.Context, msg Message, body string) error

ReplySMS replies to the given message.

func (*Client) SendSMS

func (c *Client) SendSMS(ctx context.Context, msg Message) error

SendSMS sends an SMS message to the given recipient. There is no guarantee that the message will be delivered.

type Config

type Config struct {
	Accounts []ConfigAccount
	Message  struct {
		Enable  bool `toml:"enable" json:"enable"`
		Webhook struct {
			IncomingEndpoint string `toml:"incoming_endpoint" json:"incoming_endpoint"`
			DeliveryEndpoint string `toml:"delivery_endpoint" json:"delivery_endpoint"`
		} `toml:"webhook" json:"webhook"`
	} `toml:"message" json:"message"`
}

Config is the primary config for Twipi webhook handlers. Pair it with a configuration file of choice. The primary supported languages are JSON and TOML.

type ConfigAccount

type ConfigAccount struct {
	PhoneNumber cfgutil.Env[PhoneNumber] `toml:"phone_number" json:"phone_number"`
	AccountSID  cfgutil.EnvString        `toml:"account_sid" json:"account_sid"`
	AuthToken   cfgutil.EnvString        `toml:"auth_token" json:"auth_token"`
	BaseURL     cfgutil.EnvString        `json:"base_url" toml:"base_url"`
	Override    bool                     `json:"override" toml:"override"`
}

ConfigAccount is an account config block.

func (ConfigAccount) Value

func (c ConfigAccount) Value() Account

Value returns c as the Account type.

type ConfiguredServer

type ConfiguredServer struct {
	*WebhookRouter
	Config  Config
	Client  *Client // API client
	Message *MessageHandler
}

ConfiguredServer contains servers initialized from a Config. Handlers that are disabled will be nil. The WebhookServer will always be non-nil.

func NewConfiguredServer

func NewConfiguredServer(c Config) (*ConfiguredServer, error)

NewConfiguredServer creates a new ConfiguredServer from a Config.

func NewConfiguredServerFromPath

func NewConfiguredServerFromPath(path string) (*ConfiguredServer, error)

NewConfiguredServerFromPath creates a new ConfiguredServer from a config file path. The file extension is used to determine the config format.

func (*ConfiguredServer) UpdateTwilio

func (c *ConfiguredServer) UpdateTwilio(ctx context.Context)

UpdateTwilio updates the Twilio Messaging services to work with Twipi. This function does not return any errors; they will simply be logged.

type Message

type Message struct {
	From PhoneNumber
	To   PhoneNumber
	Body string
	// contains filtered or unexported fields
}

Message describes an SMS message.

type MessageHandler

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

MessageHandler is a handler for incoming messages.

func NewMessageHandler

func NewMessageHandler(incomingPath, deliveryPath string) *MessageHandler

NewMessageHandler creates a new MessageHandler. The user should give the constructor the paths for the incoming webhook and the delivery webhook. If any of the paths are empty, the corresponding webhook will be disabled.

func (*MessageHandler) Close

func (l *MessageHandler) Close() error

Close implements WebhookRegisterer.

func (*MessageHandler) Mount

func (l *MessageHandler) Mount(r chi.Router)

Mount implements WebhookRegisterer.

func (*MessageHandler) SubscribeMessages

func (l *MessageHandler) SubscribeMessages(recipient PhoneNumber, ch chan<- Message)

SubscribeMessages subscribes the given channel to incoming messages. If recipient is not empty, only messages sent to that recipient will be published to the channel.

func (*MessageHandler) UnsubscribeMessages

func (l *MessageHandler) UnsubscribeMessages(ch chan<- Message)

UnsubscribeMessages unsubscribes the given channel from incoming messages.

type PhoneNumber

type PhoneNumber string

PhoneNumber is a phone number.

type WebhookRegisterer

type WebhookRegisterer interface {
	Mount(r chi.Router)
	io.Closer
}

WebhookRegisterer is a type that can register a webhook handler into a router.

type WebhookRouter

type WebhookRouter struct {
	chi.Mux
	// contains filtered or unexported fields
}

WebhookRouter is a router that can register a webhook handler.

func NewWebhookRouter

func NewWebhookRouter() *WebhookRouter

NewWebhookRouter creates a new WebhookRouter.

func (*WebhookRouter) Close

func (r *WebhookRouter) Close() error

Close closes all the registered webhook registers.

func (*WebhookRouter) RegisterWebhook

func (r *WebhookRouter) RegisterWebhook(registerer WebhookRegisterer)

RegisterWebhook registers a webhook handler into the server.

Jump to

Keyboard shortcuts

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