|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +from typing import List, Union |
| 5 | +from botbuilder.schema import (ActivityTypes, Activity, Attachment, |
| 6 | + AttachmentLayoutTypes, CardAction, CardImage, SuggestedActions, InputHints) |
| 7 | + |
| 8 | + |
| 9 | +def attachment_activity(attachment_layout: AttachmentLayoutTypes, attachments: List[Attachment], text: str = None, |
| 10 | + speak: str = None, input_hint: Union[InputHints, str] = InputHints.accepting_input) -> Activity: |
| 11 | + message = Activity(type=ActivityTypes.message, attachment_layout=attachment_layout, attachments=attachments, |
| 12 | + input_hint=input_hint) |
| 13 | + if text: |
| 14 | + message.text = text |
| 15 | + if speak: |
| 16 | + message.speak = speak |
| 17 | + return message |
| 18 | + |
| 19 | + |
| 20 | +class MessageFactory: |
| 21 | + """ |
| 22 | + A set of utility functions designed to assist with the formatting of the various message types a |
| 23 | + bot can return. |
| 24 | + """ |
| 25 | + |
| 26 | + @staticmethod |
| 27 | + def text(text: str, speak: str = None, input_hint: Union[InputHints, str] = InputHints.accepting_input) -> Activity: |
| 28 | + """ |
| 29 | + Returns a simple text message. |
| 30 | +
|
| 31 | + :Example: |
| 32 | + message = MessageFactory.text('Greetings from example message') |
| 33 | + await context.send_activity(message) |
| 34 | +
|
| 35 | + :param text: |
| 36 | + :param speak: |
| 37 | + :param input_hint: |
| 38 | + :return: |
| 39 | + """ |
| 40 | + message = Activity(type=ActivityTypes.message, text=text, input_hint=input_hint) |
| 41 | + if speak: |
| 42 | + message.speak = speak |
| 43 | + |
| 44 | + return message |
| 45 | + |
| 46 | + @staticmethod |
| 47 | + def suggested_actions(actions: List[CardAction], text: str = None, speak: str = None, |
| 48 | + input_hint: Union[InputHints, str] = InputHints.accepting_input) -> Activity: |
| 49 | + """ |
| 50 | + Returns a message that includes a set of suggested actions and optional text. |
| 51 | +
|
| 52 | + :Example: |
| 53 | + message = MessageFactory.suggested_actions([CardAction(title='a', type=ActionTypes.im_back, value='a'), |
| 54 | + CardAction(title='b', type=ActionTypes.im_back, value='b'), |
| 55 | + CardAction(title='c', type=ActionTypes.im_back, value='c')], 'Choose a color') |
| 56 | + await context.send_activity(message) |
| 57 | +
|
| 58 | + :param actions: |
| 59 | + :param text: |
| 60 | + :param speak: |
| 61 | + :param input_hint: |
| 62 | + :return: |
| 63 | + """ |
| 64 | + actions = SuggestedActions(actions=actions) |
| 65 | + message = Activity(type=ActivityTypes.message, input_hint=input_hint, suggested_actions=actions) |
| 66 | + if text: |
| 67 | + message.text = text |
| 68 | + if speak: |
| 69 | + message.speak = speak |
| 70 | + return message |
| 71 | + |
| 72 | + @staticmethod |
| 73 | + def attachment(attachment: Attachment, text: str = None, speak: str = None, |
| 74 | + input_hint: Union[InputHints, str] = None): |
| 75 | + """ |
| 76 | + Returns a single message activity containing an attachment. |
| 77 | +
|
| 78 | + :Example: |
| 79 | + message = MessageFactory.attachment(CardFactory.hero_card(HeroCard(title='White T-Shirt', |
| 80 | + images=[CardImage(url='https://example.com/whiteShirt.jpg')], |
| 81 | + buttons=[CardAction(title='buy')]))) |
| 82 | + await context.send_activity(message) |
| 83 | +
|
| 84 | + :param attachment: |
| 85 | + :param text: |
| 86 | + :param speak: |
| 87 | + :param input_hint: |
| 88 | + :return: |
| 89 | + """ |
| 90 | + return attachment_activity(AttachmentLayoutTypes.list, [attachment], text, speak, input_hint) |
| 91 | + |
| 92 | + @staticmethod |
| 93 | + def list(attachments: List[Attachment], text: str = None, speak: str = None, |
| 94 | + input_hint: Union[InputHints, str] = None) -> Activity: |
| 95 | + """ |
| 96 | + Returns a message that will display a set of attachments in list form. |
| 97 | +
|
| 98 | + :Example: |
| 99 | + message = MessageFactory.list([CardFactory.hero_card(HeroCard(title='title1', |
| 100 | + images=[CardImage(url='imageUrl1')], |
| 101 | + buttons=[CardAction(title='button1')])), |
| 102 | + CardFactory.hero_card(HeroCard(title='title2', |
| 103 | + images=[CardImage(url='imageUrl2')], |
| 104 | + buttons=[CardAction(title='button2')])), |
| 105 | + CardFactory.hero_card(HeroCard(title='title3', |
| 106 | + images=[CardImage(url='imageUrl3')], |
| 107 | + buttons=[CardAction(title='button3')]))]) |
| 108 | + await context.send_activity(message) |
| 109 | +
|
| 110 | + :param attachments: |
| 111 | + :param text: |
| 112 | + :param speak: |
| 113 | + :param input_hint: |
| 114 | + :return: |
| 115 | + """ |
| 116 | + return attachment_activity(AttachmentLayoutTypes.list, attachments, text, speak, input_hint) |
| 117 | + |
| 118 | + @staticmethod |
| 119 | + def carousel(attachments: List[Attachment], text: str = None, speak: str = None, |
| 120 | + input_hint: Union[InputHints, str] = None) -> Activity: |
| 121 | + """ |
| 122 | + Returns a message that will display a set of attachments using a carousel layout. |
| 123 | +
|
| 124 | + :Example: |
| 125 | + message = MessageFactory.carousel([CardFactory.hero_card(HeroCard(title='title1', |
| 126 | + images=[CardImage(url='imageUrl1')], |
| 127 | + buttons=[CardAction(title='button1')])), |
| 128 | + CardFactory.hero_card(HeroCard(title='title2', |
| 129 | + images=[CardImage(url='imageUrl2')], |
| 130 | + buttons=[CardAction(title='button2')])), |
| 131 | + CardFactory.hero_card(HeroCard(title='title3', |
| 132 | + images=[CardImage(url='imageUrl3')], |
| 133 | + buttons=[CardAction(title='button3')]))]) |
| 134 | + await context.send_activity(message) |
| 135 | +
|
| 136 | + :param attachments: |
| 137 | + :param text: |
| 138 | + :param speak: |
| 139 | + :param input_hint: |
| 140 | + :return: |
| 141 | + """ |
| 142 | + return attachment_activity(AttachmentLayoutTypes.carousel, attachments, text, speak, input_hint) |
| 143 | + |
| 144 | + @staticmethod |
| 145 | + def content_url(url: str, content_type: str, name: str = None, text: str = None, speak: str = None, |
| 146 | + input_hint: Union[InputHints, str] = None): |
| 147 | + """ |
| 148 | + Returns a message that will display a single image or video to a user. |
| 149 | +
|
| 150 | + :Example: |
| 151 | + message = MessageFactory.content_url('https://example.com/hawaii.jpg', 'image/jpeg', |
| 152 | + 'Hawaii Trip', 'A photo from our family vacation.') |
| 153 | + await context.send_activity(message) |
| 154 | +
|
| 155 | + :param url: |
| 156 | + :param content_type: |
| 157 | + :param name: |
| 158 | + :param text: |
| 159 | + :param speak: |
| 160 | + :param input_hint: |
| 161 | + :return: |
| 162 | + """ |
| 163 | + attachment = Attachment(content_type=content_type, content_url=url) |
| 164 | + if name: |
| 165 | + attachment.name = name |
| 166 | + return attachment_activity(AttachmentLayoutTypes.list, [attachment], text, speak, input_hint) |
0 commit comments