11from flask import json , make_response , current_app
2- from . import logger
2+ from flask_assistant import logger
3+ from flask_assistant .response import actions , dialogflow , hangouts , df_messenger
34
45
56class _Response (object ):
@@ -11,6 +12,8 @@ def __init__(self, speech, display_text=None, is_ssml=False):
1112 self ._display_text = display_text
1213 self ._integrations = current_app .config .get ("INTEGRATIONS" , [])
1314 self ._messages = [{"text" : {"text" : [speech ]}}]
15+ self ._platform_messages = {}
16+ self ._render_func = None
1417 self ._is_ssml = is_ssml
1518 self ._response = {
1619 "fulfillmentText" : speech ,
@@ -27,12 +30,16 @@ def __init__(self, speech, display_text=None, is_ssml=False):
2730 "followupEventInput" : None , # TODO
2831 }
2932
33+ for i in self ._integrations :
34+ self ._platform_messages [i ] = []
35+
3036 if "ACTIONS_ON_GOOGLE" in self ._integrations :
3137 self ._set_user_storage ()
3238 self ._integrate_with_actions (self ._speech , self ._display_text , is_ssml )
3339
3440 def add_msg (self , speech , display_text = None , is_ssml = False ):
3541 self ._messages .append ({"text" : {"text" : [speech ]}})
42+
3643 if "ACTIONS_ON_GOOGLE" in self ._integrations :
3744 self ._integrate_with_actions (speech , display_text , is_ssml )
3845
@@ -55,9 +62,32 @@ def _set_user_storage(self):
5562
5663 self ._response ["payload" ]["google" ]["userStorage" ] = user_storage
5764
65+ def _integrate_with_df_messenger (self , speech = None , display_text = None ):
66+
67+ logger .debug ("Integrating with dialogflow messenger" )
68+
69+ content = {"richContent" : [[]]}
70+ for m in self ._platform_messages .get ("DIALOGFLOW_MESSENGER" , []):
71+ content ["richContent" ][0 ].append (m )
72+
73+ payload = {"payload" : content }
74+
75+ self ._messages .append (payload )
76+
77+ def _integrate_with_hangouts (self , speech = None , display_text = None , is_ssml = False ):
78+ if display_text is None :
79+ display_text = speech
80+
81+ self ._messages .append (
82+ {"platform" : "GOOGLE_HANGOUTS" , "text" : {"text" : [display_text ]},}
83+ )
84+ for m in self ._platform_messages .get ("GOOGLE_HANGOUTS" , []):
85+ self ._messages .append (m )
86+
5887 def _integrate_with_actions (self , speech = None , display_text = None , is_ssml = False ):
5988 if display_text is None :
6089 display_text = speech
90+
6191 if is_ssml :
6292 ssml_speech = "<speak>" + speech + "</speak>"
6393 self ._messages .append (
@@ -90,6 +120,11 @@ def _include_contexts(self):
90120
91121 def render_response (self ):
92122 self ._include_contexts ()
123+ if self ._render_func :
124+ self ._render_func ()
125+
126+ self ._integrate_with_df_messenger ()
127+ self ._integrate_with_hangouts (self ._speech , self ._display_text )
93128 logger .debug (json .dumps (self ._response , indent = 2 ))
94129 resp = make_response (json .dumps (self ._response ))
95130 resp .headers ["Content-Type" ] = "application/json"
@@ -102,21 +137,14 @@ def suggest(self, *replies):
102137 for r in replies :
103138 chips .append ({"title" : r })
104139
105- # NOTE: both of these formats work in the dialogflow console,
106- # but only the first (suggestions) appears in actual Google Assistant
107-
108140 # native chips for GA
109141 self ._messages .append (
110142 {"platform" : "ACTIONS_ON_GOOGLE" , "suggestions" : {"suggestions" : chips }}
111143 )
112144
113- # # quick replies for other platforms
114- # self._messages.append(
115- # {
116- # "platform": "ACTIONS_ON_GOOGLE",
117- # "quickReplies": {"title": None, "quickReplies": replies},
118- # }
119- # )
145+ if "DIALOGFLOW_MESSENGER" in self ._integrations :
146+ chip_resp = df_messenger ._build_suggestions (* replies )
147+ self ._platform_messages ["DIALOGFLOW_MESSENGER" ].append (chip_resp )
120148
121149 return self
122150
@@ -142,32 +170,33 @@ def card(
142170 link_title = None ,
143171 buttons = None ,
144172 ):
145- """
146- :param: link and :param: link_title supports only one button per card
147- in future versions should be deleted
148- and from now deprecated
173+ df_card = dialogflow . build_card (
174+ text , title , img_url , img_alt , subtitle , link , link_title
175+ )
176+ self . _messages . append ( df_card )
149177
150- :param link:
151- :param link_title:
152- :return:
153- """
178+ # df_messengar car is a combo of description + button
179+ if "DIALOGFLOW_MESSENGER" in self ._integrations :
154180
155- card_payload = {"title" : title , "subtitle" : subtitle , "formattedText" : text }
181+ description = df_messenger ._build_description_response (text , title )
182+ self ._platform_messages ["DIALOGFLOW_MESSENGER" ].append (description )
156183
157- if buttons :
158- card_payload ["buttons" ] = buttons
159- elif link and link_title :
160- logger .info ('use button parameter instead of link and link_title' )
161- buttons = [build_button (title = link_title , link = link )]
162- card_payload ["buttons" ] = buttons
184+ if link :
185+ btn = df_messenger ._build_button (link , link_title )
186+ self ._platform_messages ["DIALOGFLOW_MESSENGER" ].append (btn )
163187
164- if img_url :
165- img_payload = {"imageUri" : img_url , "accessibilityText" : img_alt or img_url }
166- card_payload ["image" ] = img_payload
188+ if "GOOGLE_HANGOUTS" in self ._integrations :
189+ hangouts_card = hangouts .build_card (
190+ text , title , img_url , img_alt , subtitle , link , link_title
191+ )
192+ self ._platform_messages ["GOOGLE_HANGOUTS" ].append (hangouts_card )
167193
168- self ._messages .append (
169- {"platform" : "ACTIONS_ON_GOOGLE" , "basicCard" : card_payload }
170- )
194+ if "ACTIONS_ON_GOOGLE" in self ._integrations :
195+ actions_card = actions .build_card (
196+ text , title , img_url , img_alt , subtitle , link , link_title , buttons
197+ )
198+
199+ self ._messages .append (actions_card )
171200
172201 return self
173202
@@ -255,10 +284,7 @@ def add_media(self, url, name, description=None, icon_url=None, icon_alt=None):
255284
256285
257286def build_button (title , link ):
258- return {
259- "title" : title ,
260- "openUriAction" : {"uri" : link }
261- }
287+ return {"title" : title , "openUriAction" : {"uri" : link }}
262288
263289
264290def build_item (
@@ -290,7 +316,7 @@ class _CardWithItems(_Response):
290316 def __init__ (self , speech , display_text = None , items = None ):
291317 super (_CardWithItems , self ).__init__ (speech , display_text )
292318 self ._items = items or list ()
293- self ._add_message () # possibly call this later?
319+ self ._render_func = self . _add_message
294320
295321 def _add_message (self ):
296322 raise NotImplementedError
@@ -332,12 +358,20 @@ def __init__(self, speech, display_text=None, title=None, items=None):
332358 super (_ListSelector , self ).__init__ (speech , display_text , items )
333359
334360 def _add_message (self ):
361+
335362 self ._messages .append (
336363 {
337364 "platform" : "ACTIONS_ON_GOOGLE" ,
338365 "listSelect" : {"title" : self ._title , "items" : self ._items },
339366 }
340367 )
368+ self ._add_platform_msgs ()
369+
370+ def _add_platform_msgs (self ):
371+
372+ if "DIALOGFLOW_MESSENGER" in self ._integrations :
373+ list_resp = df_messenger ._build_list (self ._title , self ._items )
374+ self ._platform_messages ["DIALOGFLOW_MESSENGER" ].extend (list_resp )
341375
342376
343377class _CarouselCard (_ListSelector ):
@@ -453,5 +487,3 @@ def __init__(self, reason=None):
453487 }
454488 }
455489 }
456-
457-
0 commit comments