messagecard

package
v2.10.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2024 License: MIT Imports: 6 Imported by: 15

Documentation

Overview

Package messagecard provides support for generating Microsoft Teams messages using the legacy "actionable message card" (aka, "MessageCard") format.

See the provided examples in this repo, the Godoc generated documentation at https://pkg.golang.ir/github.com/atc0005/go-teams-notify/v2 and the following resources for more information:

Index

Constants

View Source
const (
	// PotentialActionOpenURIType is the type that must be used for OpenUri
	// potential action.
	PotentialActionOpenURIType = "OpenUri"

	// PotentialActionHTTPPostType is the type that must be used for HttpPOST
	// potential action.
	PotentialActionHTTPPostType = "HttpPOST"

	// PotentialActionActionCardType is the type that must be used for
	// ActionCard potential action.
	PotentialActionActionCardType = "ActionCard"

	// PotentialActionInvokeAddInCommandType is the type that must be used for
	// InvokeAddInCommand potential action.
	PotentialActionInvokeAddInCommandType = "InvokeAddInCommand"

	// PotentialActionActionCardInputTextInputType is the type that must be
	// used for ActionCard TextInput type.
	PotentialActionActionCardInputTextInputType = "TextInput"

	// PotentialActionActionCardInputDateInputType is the type that must be
	// used for ActionCard DateInput type.
	PotentialActionActionCardInputDateInputType = "DateInput"

	// PotentialActionActionCardInputMultichoiceInputType is the type that
	// must be used for ActionCard MultichoiceInput type.
	PotentialActionActionCardInputMultichoiceInputType = "MultichoiceInput"
)
View Source
const PotentialActionMaxSupported = 4

PotentialActionMaxSupported is the maximum number of actions allowed in a PotentialAction collection.

https://docs.microsoft.com/en-us/outlook/actionable-messages/message-card-reference#actions

Variables

View Source
var ErrPotentialActionsLimitReached = errors.New("potential actions collection limit reached")

ErrPotentialActionsLimitReached indicates that the maximum supported number of potentialAction collection values has been reached for either a MessageCard or a Section.

Functions

func ConvertEOLToBreak

func ConvertEOLToBreak(s string) string

ConvertEOLToBreak converts \r\n (windows), \r (mac) and \n (unix) into <br> statements.

This function is intended for processing text intended for a MessageCard. Using this helper function for text intended for an Adaptive Card is unsupported and unlikely to produce the desired results.

func FormatAsCodeBlock

func FormatAsCodeBlock(input string) (string, error)

FormatAsCodeBlock accepts an arbitrary string, quoted or not, and calls a helper function which attempts to format as a valid Markdown code block for submission to Microsoft Teams.

This function is intended for processing text intended for a MessageCard. Using this helper function for text intended for an Adaptive Card is unsupported and unlikely to produce the desired results.

func FormatAsCodeSnippet

func FormatAsCodeSnippet(input string) (string, error)

FormatAsCodeSnippet accepts an arbitrary string, quoted or not, and calls a helper function which attempts to format as a single-line valid Markdown code snippet for submission to Microsoft Teams.

This function is intended for processing text intended for a MessageCard. Using this helper function for text intended for an Adaptive Card is unsupported and unlikely to produce the desired results.

func TryToFormatAsCodeBlock

func TryToFormatAsCodeBlock(input string) string

TryToFormatAsCodeBlock acts as a wrapper for FormatAsCodeBlock. If an error is encountered in the FormatAsCodeBlock function, this function will return the original string, otherwise if no errors occur the newly formatted string will be returned.

This function is intended for processing text intended for a MessageCard. Using this helper function for text intended for an Adaptive Card is unsupported and unlikely to produce the desired results.

func TryToFormatAsCodeSnippet

func TryToFormatAsCodeSnippet(input string) string

TryToFormatAsCodeSnippet acts as a wrapper for FormatAsCodeSnippet. If an error is encountered in the FormatAsCodeSnippet function, this function will return the original string, otherwise if no errors occur the newly formatted string will be returned.

This function is intended for processing text intended for a MessageCard. Using this helper function for text intended for an Adaptive Card is unsupported and unlikely to produce the desired results.

Types

type MessageCard

type MessageCard struct {
	// Required; must be set to "MessageCard"
	Type string `json:"@type" yaml:"@type"`

	// Required; must be set to "https://schema.org/extensions"
	Context string `json:"@context" yaml:"@context"`

	// Summary is required if the card does not contain a text property,
	// otherwise optional. The summary property is typically displayed in the
	// list view in Outlook, as a way to quickly determine what the card is
	// all about. Summary appears to only be used when there are sections defined
	Summary string `json:"summary,omitempty" yaml:"summary,omitempty"`

	// Title is the title property of a card. is meant to be rendered in a
	// prominent way, at the very top of the card. Use it to introduce the
	// content of the card in such a way users will immediately know what to
	// expect.
	Title string `json:"title,omitempty" yaml:"title,omitempty"`

	// Text is required if the card does not contain a summary property,
	// otherwise optional. The text property is meant to be displayed in a
	// normal font below the card's title. Use it to display content, such as
	// the description of the entity being referenced, or an abstract of a
	// news article.
	Text string `json:"text,omitempty" yaml:"text,omitempty"`

	// Specifies a custom brand color for the card. The color will be
	// displayed in a non-obtrusive manner.
	ThemeColor string `json:"themeColor,omitempty" yaml:"themeColor,omitempty"`

	// ValidateFunc is an optional user-specified validation function that is
	// responsible for validating a MessageCard. If not specified, default
	// validation is performed.
	ValidateFunc func() error `json:"-" yaml:"-"`

	// Sections is a collection of sections to include in the card.
	Sections []*Section `json:"sections,omitempty" yaml:"sections,omitempty"`

	// PotentialActions is a collection of actions for a MessageCard.
	PotentialActions []*PotentialAction `json:"potentialAction,omitempty" yaml:"potentialAction,omitempty"`
	// contains filtered or unexported fields
}

MessageCard represents a legacy actionable message card used via Office 365 or Microsoft Teams connectors.

func NewMessageCard

func NewMessageCard() *MessageCard

NewMessageCard creates a new legacy MessageCard with required fields predefined.

https://docs.microsoft.com/en-us/outlook/actionable-messages/message-card-reference#card-fields

func (*MessageCard) AddPotentialAction

func (mc *MessageCard) AddPotentialAction(actions ...*PotentialAction) error

AddPotentialAction adds one or many PotentialAction values to a PotentialActions collection on a MessageCard.

func (*MessageCard) AddSection

func (mc *MessageCard) AddSection(section ...*Section) error

AddSection adds one or many additional Section values to a MessageCard. Validation is performed to reject invalid values with an error message.

func (*MessageCard) Payload

func (mc *MessageCard) Payload() io.Reader

Payload returns the prepared MessageCard payload. The caller should call Prepare() prior to calling this method, results are undefined otherwise.

func (*MessageCard) Prepare

func (mc *MessageCard) Prepare() error

Prepare handles tasks needed to construct a payload from a MessageCard for delivery to an endpoint.

func (*MessageCard) PrettyPrint

func (mc *MessageCard) PrettyPrint() string

PrettyPrint returns a formatted JSON payload of the MessageCard if the Prepare() method has been called, or an empty string otherwise.

func (*MessageCard) Validate

func (mc *MessageCard) Validate() error

Validate performs validation for MessageCard using ValidateFunc if defined, otherwise applying default validation.

type PotentialAction

type PotentialAction struct {
	// Type of the potential action. Can be OpenUri, HttpPOST, ActionCard or
	// InvokeAddInCommand.
	Type string `json:"@type" yaml:"@type"`

	// Name property defines the text that will be displayed on screen for the
	// action.
	Name string `json:"name" yaml:"name"`

	// PotentialActionOpenURI is a set of options for openUri
	// potential action.
	PotentialActionOpenURI

	// PotentialActionHTTPPOST is a set of options for httpPOST
	// potential action.
	PotentialActionHTTPPOST

	// PotentialActionActionCard is a set of options for actionCard
	// potential action.
	PotentialActionActionCard

	// PotentialActionInvokeAddInCommand is a set of options for
	// invokeAddInCommand potential action.
	PotentialActionInvokeAddInCommand
}

PotentialAction represents potential actions an user can do in a message card. See Legacy actionable message card reference > Actions for more information.

func NewPotentialAction

func NewPotentialAction(potentialActionType string, name string) (*PotentialAction, error)

NewPotentialAction creates a new PotentialAction using the provided potential action type and name. The name value defines the text that will be displayed on screen for the action. An error is returned if invalid values are supplied.

type PotentialActionActionCard

type PotentialActionActionCard struct {
	// Inputs is a collection of inputs an user can provide before processing
	// the actions. Only used for ActionCard action type. Three types of
	// inputs are available: TextInput, DateInput and MultichoiceInput
	Inputs []PotentialActionActionCardInput `json:"inputs,omitempty" yaml:"inputs,omitempty"`

	// Actions are the available actions. Only used for ActionCard action
	// type.
	Actions []PotentialActionActionCardAction `json:"actions,omitempty" yaml:"actions,omitempty"`
}

PotentialActionActionCard represents an actionCard potential action.

type PotentialActionActionCardAction

type PotentialActionActionCardAction struct {
	// Type of the action. Can be OpenUri, HttpPOST, ActionCard or
	// InvokeAddInCommand.
	Type string `json:"@type" yaml:"@type"`

	// Name property defines the text that will be displayed on screen for the
	// action.
	Name string `json:"name" yaml:"name"`

	// PotentialActionOpenURI is used to specify a openUri action
	// card's action.
	PotentialActionOpenURI

	// PotentialActionHTTPPOST is used to specify a httpPOST action
	// card's action.
	PotentialActionHTTPPOST
}

PotentialActionActionCardAction is used for configuring ActionCard actions

type PotentialActionActionCardInput

type PotentialActionActionCardInput struct {
	// Type of the ActionCard input.
	// Must be either TextInput, DateInput or MultichoiceInput
	Type string `json:"@type" yaml:"@type"`

	// ID uniquely identifies the input so it is possible to reference it in
	// the URL or body of an HttpPOST action.
	ID string `json:"id,omitempty" yaml:"id,omitempty"`

	// Title defines a title for the input.
	Title string `json:"title,omitempty" yaml:"title,omitempty"`

	// Value defines the initial value of the input. For multi-choice inputs,
	// value must be equal to the value property of one of the input's
	// choices.
	Value string `json:"value,omitempty" yaml:"value,omitempty"`

	// MessageCardPotentialActionInputMultichoiceInput must be defined for
	// MultichoiceInput input type.
	PotentialActionActionCardInputMultichoiceInput

	// MessageCardPotentialActionInputTextInput must be defined for InputText
	// input type.
	PotentialActionActionCardInputTextInput

	// MessageCardPotentialActionInputDateInput must be defined for DateInput
	// input type.
	PotentialActionActionCardInputDateInput

	// IsRequired indicates whether users are required to type a value before
	// they are able to take an action that would take the value of the input
	// as a parameter.
	IsRequired bool `json:"isRequired,omitempty" yaml:"isRequired,omitempty"`
}

PotentialActionActionCardInput represents an ActionCard input.

type PotentialActionActionCardInputDateInput

type PotentialActionActionCardInputDateInput struct {
	// IncludeTime indicates whether the date input should allow for the
	// selection of a time in addition to the date.
	IncludeTime bool `json:"includeTime,omitempty" yaml:"includeTime,omitempty"`
}

PotentialActionActionCardInputDateInput represents a DateInput input used for potential action.

type PotentialActionActionCardInputMultichoiceInput

type PotentialActionActionCardInputMultichoiceInput struct {
	// Choices defines the values that can be selected for the multichoice
	// input.
	Choices []struct {
		Display string `json:"display,omitempty" yaml:"display,omitempty"`
		Value   string `json:"value,omitempty" yaml:"value,omitempty"`
	} `json:"choices,omitempty" yaml:"choices,omitempty"`

	// Style defines the style of the input. When IsMultiSelect is false,
	// setting the style property to expanded will instruct the host
	// application to try and display all choices on the screen, typically
	// using a set of radio buttons.
	Style string `json:"style,omitempty" yaml:"style,omitempty"`

	// IsMultiSelect indicates whether or not the user can select more than
	// one choice. The specified choices will be displayed as a list of
	// checkboxes. Default value is false.
	IsMultiSelect bool `json:"isMultiSelect,omitempty" yaml:"isMultiSelect,omitempty"`
}

PotentialActionActionCardInputMultichoiceInput represents a MultichoiceInput input used for potential action.

type PotentialActionActionCardInputTextInput

type PotentialActionActionCardInputTextInput struct {
	// MaxLength indicates the maximum number of characters that can be
	// entered.
	MaxLength int `json:"maxLength,omitempty" yaml:"maxLength,omitempty"`

	// IsMultiline indicates whether the text input should accept multiple
	// lines of text.
	IsMultiline bool `json:"isMultiline,omitempty" yaml:"isMultiline,omitempty"`
}

PotentialActionActionCardInputTextInput represents a TextInput input used for potential action.

type PotentialActionHTTPPOST

type PotentialActionHTTPPOST struct {
	// Target defines the URL endpoint of the service that implements the
	// action. Only used for HttpPOST action type.
	Target string `json:"target,omitempty" yaml:"target,omitempty"`

	// Headers is a collection of MessageCardPotentialActionHeader objects
	// representing a set of HTTP headers that will be emitted when sending
	// the POST request to the target URL. Only used for HttpPOST action type.
	Headers []PotentialActionHTTPPOSTHeader `json:"headers,omitempty" yaml:"headers,omitempty"`

	// Body is the body of the POST request. Only used for HttpPOST action
	// type.
	Body string `json:"body,omitempty" yaml:"body,omitempty"`

	// BodyContentType is optional and specifies the MIME type of the body in
	// the POST request. Only used for HttpPOST action type.
	BodyContentType string `json:"bodyContentType,omitempty" yaml:"bodyContentType,omitempty"`
}

PotentialActionHTTPPOST represents a HttpPOST potential action.

type PotentialActionHTTPPOSTHeader

type PotentialActionHTTPPOSTHeader struct {
	// Name is the header name.
	Name string `json:"name,omitempty" yaml:"name,omitempty"`

	// Value is the header value.
	Value string `json:"value,omitempty" yaml:"value,omitempty"`
}

PotentialActionHTTPPOSTHeader defines a HTTP header used for HttpPOST action type.

type PotentialActionInvokeAddInCommand

type PotentialActionInvokeAddInCommand struct {
	// AddInID specifies the add-in ID of the required add-in. Only used for
	// InvokeAddInCommand action type.
	AddInID string `json:"addInId,omitempty" yaml:"addInId,omitempty"`

	// DesktopCommandID specifies the ID of the add-in command button that
	// opens the required task pane. Only used for InvokeAddInCommand action
	// type.
	DesktopCommandID string `json:"desktopCommandId,omitempty" yaml:"desktopCommandId,omitempty"`

	// InitializationContext is an optional field which provides developers a
	// way to specify any valid JSON object. The value is serialized into a
	// string and made available to the add-in when the action is executed.
	// This allows the action to pass initialization data to the add-in. Only
	// used for InvokeAddInCommand action type.
	InitializationContext interface{} `json:"initializationContext,omitempty" yaml:"initializationContext,omitempty"`
}

PotentialActionInvokeAddInCommand represents an invokeAddInCommand potential action.

type PotentialActionOpenURI

type PotentialActionOpenURI struct {
	// Targets is a collection of name/value pairs that defines one URI per
	// target operating system. Only used for OpenUri action type.
	Targets []PotentialActionOpenURITarget `json:"targets,omitempty" yaml:"targets,omitempty"`
}

PotentialActionOpenURI represents a OpenUri potential action.

type PotentialActionOpenURITarget

type PotentialActionOpenURITarget struct {
	// OS defines the operating system the target uri refers to. Supported
	// operating system values are default, windows, iOS and android. The
	// default operating system will in most cases simply open the URI in a
	// web browser, regardless of the actual operating system.
	OS string `json:"os,omitempty" yaml:"os,omitempty"`

	// URI defines the URI being called.
	URI string `json:"uri,omitempty" yaml:"uri,omitempty"`
}

PotentialActionOpenURITarget is used for OpenUri action type. It defines one URI per target operating system.

type Section

type Section struct {
	// Title is the title property of a section. This property is displayed
	// in a font that stands out, while not as prominent as the card's title.
	// It is meant to introduce the section and summarize its content,
	// similarly to how the card's title property is meant to summarize the
	// whole card.
	Title string `json:"title,omitempty" yaml:"title,omitempty"`

	// Text is the section's text property. This property is very similar to
	// the text property of the card. It can be used for the same purpose.
	Text string `json:"text,omitempty" yaml:"text,omitempty"`

	// ActivityImage is a property used to display a picture associated with
	// the subject of a message card. For example, this might be the portrait
	// of a person who performed an activity that the message card is
	// associated with.
	ActivityImage string `json:"activityImage,omitempty" yaml:"activityImage,omitempty"`

	// ActivityTitle is a property used to summarize the activity associated
	// with a message card.
	ActivityTitle string `json:"activityTitle,omitempty" yaml:"activityTitle,omitempty"`

	// ActivitySubtitle is a property used to show brief, but extended
	// information about an activity associated with a message card. Examples
	// include the date and time the associated activity was taken or the
	// handle of a person associated with the activity.
	ActivitySubtitle string `json:"activitySubtitle,omitempty" yaml:"activitySubtitle,omitempty"`

	// ActivityText is a property used to provide details about the activity.
	// For example, if the message card is used to deliver updates about a
	// topic, then this property would be used to hold the bulk of the content
	// for the update notification.
	ActivityText string `json:"activityText,omitempty" yaml:"activityText,omitempty"`

	// HeroImage is a property that allows for setting an image as the
	// centerpiece of a message card. This property can also be used to add a
	// banner to the message card.
	// Note: heroImage is not currently supported by Microsoft Teams
	// https://stackoverflow.com/a/45389789
	// We use a pointer to this type in order to have the json package
	// properly omit this field if not explicitly set.
	// https://github.com/golang/go/issues/11939
	// https://stackoverflow.com/questions/18088294/how-to-not-marshal-an-empty-struct-into-json-with-go
	// https://stackoverflow.com/questions/33447334/golang-json-marshal-how-to-omit-empty-nested-struct
	HeroImage *SectionImage `json:"heroImage,omitempty" yaml:"heroImage,omitempty"`

	// Facts is a collection of SectionFact values. A section entry
	// usually is displayed in a two-column key/value format.
	Facts []SectionFact `json:"facts,omitempty" yaml:"facts,omitempty"`

	// Images is a property that allows for the inclusion of a photo gallery
	// inside a section.
	// We use a slice of pointers to this type in order to have the json
	// package properly omit this field if not explicitly set.
	// https://github.com/golang/go/issues/11939
	// https://stackoverflow.com/questions/18088294/how-to-not-marshal-an-empty-struct-into-json-with-go
	// https://stackoverflow.com/questions/33447334/golang-json-marshal-how-to-omit-empty-nested-struct
	Images []*SectionImage `json:"images,omitempty" yaml:"images,omitempty"`

	// PotentialActions is a collection of actions for a Section.
	// This is separate from the actions collection for the MessageCard.
	PotentialActions []*PotentialAction `json:"potentialAction,omitempty" yaml:"potentialAction,omitempty"`

	// Markdown represents a toggle to enable or disable Markdown formatting.
	// By default, all text fields in a card and its sections can be formatted
	// using basic Markdown.
	Markdown bool `json:"markdown,omitempty" yaml:"markdown,omitempty"`

	// StartGroup is the section's startGroup property. This property marks
	// the start of a logical group of information. Typically, sections with
	// startGroup set to true will be visually separated from previous card
	// elements.
	StartGroup bool `json:"startGroup,omitempty" yaml:"startGroup,omitempty"`
}

Section represents a section to include in a message card.

func NewSection

func NewSection() *Section

NewSection creates an empty message card section

func (*Section) AddFact

func (mcs *Section) AddFact(fact ...SectionFact) error

AddFact adds one or many additional SectionFact values to a Section

func (*Section) AddFactFromKeyValue

func (mcs *Section) AddFactFromKeyValue(key string, values ...string) error

AddFactFromKeyValue accepts a key and slice of values and converts them to SectionFact values

func (*Section) AddHeroImage

func (mcs *Section) AddHeroImage(heroImage SectionImage) error

AddHeroImage adds a Hero Image to a MessageCard section using a SectionImage argument. This image is used as the centerpiece or banner of a message card.

func (*Section) AddHeroImageStr

func (mcs *Section) AddHeroImageStr(imageURL string, imageTitle string) error

AddHeroImageStr adds a Hero Image to a MessageCard section using string arguments. This image is used as the centerpiece or banner of a message card.

func (*Section) AddImage

func (mcs *Section) AddImage(sectionImage ...SectionImage) error

AddImage adds an image to a MessageCard section. These images are used to provide a photo gallery inside a MessageCard section.

func (*Section) AddPotentialAction

func (mcs *Section) AddPotentialAction(actions ...*PotentialAction) error

AddPotentialAction adds one or many PotentialAction values to a PotentialActions collection on a Section. This is separate from the actions collection for the MessageCard.

type SectionFact

type SectionFact struct {

	// Name is the key for an associated value in a key/value pair
	Name string `json:"name" yaml:"name"`

	// Value is the value for an associated key in a key/value pair
	Value string `json:"value" yaml:"value"`
}

SectionFact represents a section fact entry that is usually displayed in a two-column key/value format.

func NewSectionFact

func NewSectionFact() *SectionFact

NewSectionFact creates an empty message card section fact

type SectionImage

type SectionImage struct {

	// Image is the URL to the image.
	Image string `json:"image" yaml:"image"`

	// Title is a short description of the image. Typically, this description
	// is displayed in a tooltip as the user hovers their mouse over the
	// image.
	Title string `json:"title" yaml:"title"`
}

SectionImage represents an image as used by the heroImage and images properties of a section.

func NewSectionImage

func NewSectionImage() *SectionImage

NewSectionImage creates an empty image for use with message card section

Jump to

Keyboard shortcuts

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