1
+ # -------------------------------------------------------------------------
2
+ # Copyright (c) Microsoft Corporation. All rights reserved.
3
+ # Licensed under the MIT License. See License.txt in the project root for
4
+ # license information.
5
+ # --------------------------------------------------------------------------
6
+
7
+ import os
8
+ from typing import List
9
+ from devtools_testutils import recorded_by_proxy
10
+ from _decorators import MessagesPreparers
11
+ from azure .core .credentials import AccessToken
12
+ from azure .communication .messages import NotificationMessagesClient
13
+ from azure .communication .messages .models import (
14
+ TextNotificationContent ,
15
+ ImageNotificationContent ,
16
+ TemplateNotificationContent ,
17
+ MessageReceipt ,
18
+ MessageTemplate ,
19
+ MessageTemplateText ,
20
+ MessageTemplateBindings ,
21
+ MessageTemplateValue ,
22
+ WhatsAppMessageTemplateBindings ,
23
+ WhatsAppMessageTemplateBindingsComponent
24
+ )
25
+ from _shared .utils import get_http_logging_policy
26
+ from _messages_test_case import MessagesRecordedTestCase
27
+ from azure .communication .messages ._shared .utils import parse_connection_str
28
+
29
+ class TestNotificationMessageClientForText (MessagesRecordedTestCase ):
30
+
31
+ @MessagesPreparers .messages_test_decorator
32
+ @recorded_by_proxy
33
+ def test_text_send_message (self ):
34
+ phone_number : str = "+14254360097"
35
+ raised = False
36
+
37
+ text_options = TextNotificationContent (
38
+ channel_registration_id = "b045be8c-45cd-492a-b2a2-47bae7c36959" ,
39
+ to = [phone_number ],
40
+ content = "Thanks for your feedback Hello." )
41
+
42
+ message_response : MessageReceipt = None
43
+ message_client : NotificationMessagesClient = self .create_notification_message_client ()
44
+
45
+ try :
46
+ with message_client :
47
+ message_responses = message_client .send (text_options )
48
+ message_response = message_responses .receipts [0 ]
49
+ except :
50
+ raised = True
51
+ raise
52
+ assert raised is False
53
+ assert message_response .message_id is not None
54
+ assert message_response .to is not None
55
+
56
+
57
+ @MessagesPreparers .messages_test_decorator
58
+ @recorded_by_proxy
59
+ def test_template_send_message (self ):
60
+ phone_number : str = "+14254360097"
61
+ input_template : MessageTemplate = MessageTemplate (
62
+ name = "gathering_invitation" ,
63
+ language = "ca" )
64
+ raised = False
65
+
66
+ message_client : NotificationMessagesClient = self .create_notification_message_client ()
67
+
68
+ template_options = TemplateNotificationContent (
69
+ channel_registration_id = "b045be8c-45cd-492a-b2a2-47bae7c36959" ,
70
+ to = [phone_number ],
71
+ template = input_template )
72
+
73
+ message_response : MessageReceipt = None
74
+
75
+ try :
76
+ with message_client :
77
+ message_responses = message_client .send (template_options )
78
+ message_response = message_responses .receipts [0 ]
79
+ except :
80
+ raised = True
81
+ raise
82
+ assert raised is False
83
+ assert message_response .message_id is not None
84
+ assert message_response .to is not None
85
+
86
+
87
+ @MessagesPreparers .messages_test_decorator
88
+ @recorded_by_proxy
89
+ def test_template_with_parameters_send_message (self ):
90
+
91
+ phone_number : str = "+14254360097"
92
+ parammeter1 = MessageTemplateText (
93
+ name = "first" ,
94
+ text = "11-18-2024"
95
+ )
96
+
97
+ input_template : MessageTemplate = MessageTemplate (
98
+ name = "gathering_invitation" ,
99
+ language = "en_US" ,
100
+ template_values = [parammeter1 ],
101
+ bindings = WhatsAppMessageTemplateBindings
102
+ (
103
+ body = [ WhatsAppMessageTemplateBindingsComponent (ref_value = "first" )]
104
+ )
105
+ )
106
+ raised = False
107
+
108
+ template_options = TemplateNotificationContent (
109
+ channel_registration_id = "b045be8c-45cd-492a-b2a2-47bae7c36959" ,
110
+ to = [phone_number ],
111
+ template = input_template )
112
+
113
+ message_response : MessageReceipt = None
114
+ message_client : NotificationMessagesClient = self .create_notification_message_client ()
115
+
116
+ try :
117
+ with message_client :
118
+ message_responses = message_client .send (template_options )
119
+ message_response = message_responses .receipts [0 ]
120
+ except :
121
+ raised = True
122
+ raise
123
+ assert raised is False
124
+ assert message_response .message_id is not None
125
+ assert message_response .to is not None
126
+
127
+ @MessagesPreparers .messages_test_decorator
128
+ @recorded_by_proxy
129
+ def test_image_send_message (self ):
130
+ phone_number : str = "+14254360097"
131
+ input_media_uri : str = "https://aka.ms/acsicon1"
132
+ raised = False
133
+
134
+ template_options = ImageNotificationContent (
135
+ channel_registration_id = "b045be8c-45cd-492a-b2a2-47bae7c36959" ,
136
+ to = [phone_number ],
137
+ media_uri = input_media_uri )
138
+
139
+ message_response : MessageReceipt = None
140
+ message_client : NotificationMessagesClient = self .create_notification_message_client ()
141
+
142
+ try :
143
+ with message_client :
144
+ message_responses = message_client .send (template_options )
145
+ message_response = message_responses .receipts [0 ]
146
+ except :
147
+ raised = True
148
+ raise
149
+ assert raised is False
150
+ assert message_response .message_id is not None
151
+ assert message_response .to is not None
152
+
153
+
154
+ @MessagesPreparers .messages_test_decorator
155
+ @recorded_by_proxy
156
+ def test_download_media (self ):
157
+ phone_number : str = "+14254360097"
158
+ input_media_id : str = "8f8c29b2-c2e4-4340-bb28-3009c8a57283"
159
+ raised = False
160
+ message_client : NotificationMessagesClient = self .create_notification_message_client ()
161
+ try :
162
+ with message_client :
163
+ media_stream = message_client .download_media (input_media_id )
164
+ except :
165
+ raised = True
166
+ raise
167
+ assert raised is False
168
+ assert media_stream is not None
0 commit comments