Skip to content

Commit

Permalink
CodeGen from PR 31549 in Azure/azure-rest-api-specs
Browse files Browse the repository at this point in the history
Merge 7341b64583279c88dec819b92a3df2b55cf979e0 into 6282f2be60dca94b71b1e32cfe6efa9f965b6d1d
  • Loading branch information
SDKAuto committed Nov 27, 2024
1 parent aedb647 commit f9d0a20
Show file tree
Hide file tree
Showing 87 changed files with 1,351 additions and 4,813 deletions.
979 changes: 567 additions & 412 deletions common/config/rush/pnpm-lock.yaml

Large diffs are not rendered by default.

49 changes: 0 additions & 49 deletions sdk/communication/communication-messages-rest/CHANGELOG.md

This file was deleted.

21 changes: 21 additions & 0 deletions sdk/communication/communication-messages-rest/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 Microsoft

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
218 changes: 17 additions & 201 deletions sdk/communication/communication-messages-rest/README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
# Azure CommunicationMessages REST client library for JavaScript

This package contains a JavaScript SDK for Azure Communication Messages Services.
Azure Communication Messages Service

**Please rely heavily on our [REST client docs](https://github.com/Azure/azure-sdk-for-js/blob/main/documentation/rest-clients.md) to use this library**

Key links:

- [Quick Start][azure_communication_messaging_qs]
- [Package (NPM)](https://www.npmjs.com/package/@azure-rest/communication-messages)
- [API reference documentation](https://docs.microsoft.com/javascript/api/@azure-rest/communication-messages?view=azure-node-preview)

## Getting started

### Currently supported environments

- [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule)
- LTS versions of Node.js

### Prerequisites

- You must have an [Azure subscription][azure_sub] to use this package.
- An existing Communication Services resource. If you need to create the resource, you can use the [Azure Portal][azure_portal], the [Azure PowerShell][azure_powershell], or the [Azure CLI][azure_cli].
- See [how to register whatsapp business account & create a channel][register_whatsapp_business_account] for registering whatsapp channel to your Communication Services resource.
- You must have an [Azure subscription](https://azure.microsoft.com/free/) to use this package.

### Install the `@azure-rest/communication-messages` package

Expand All @@ -26,180 +27,20 @@ Install the Azure CommunicationMessages REST client REST client library for Java
npm install @azure-rest/communication-messages
```

## Authentication

You can get a key and/or connection string from your Communication Services resource in [Azure Portal][azure_portal]. Once you have a key, you may authenticate with any of the following methods:

### Using a connection string

```typescript
import MessageClient, { MessagesServiceClient } from "@azure-rest/communication-messages";

const connectionString = `endpoint=https://<resource-name>.communication.azure.com/;accessKey=<Base64-Encoded-Key>`;
const client:MessagesServiceClient = MessageClient(connectionString);
```

### Using `AzureKeyCredential`

```typescript
import { AzureKeyCredential } from "@azure/core-auth";
import MessageClient, { MessagesServiceClient } from "@azure-rest/communication-messages";

const endpoint = "https://<resource-name>.communication.azure.com";
const credential = new AzureKeyCredential("<Base64-Encoded-Key>");
const client:MessagesServiceClient = MessageClient(endpoint, credential);
```

### Using Azure Active Directory managed identity

Client API key authentication is used in most of the examples, but you can also authenticate with Azure Active Directory using the [Azure Identity library][azure_identity]. To use the [DefaultAzureCredential][defaultazurecredential] provider shown below, or other credential providers provided with the Azure SDK, please install the [`@azure/identity`][azure_identity] package:

```bash
npm install @azure/identity
```

The [`@azure/identity`][azure_identity] package provides a variety of credential types that your application can use to do this. The README for @azure/identity provides more details and samples to get you started.
AZURE_CLIENT_SECRET, AZURE_CLIENT_ID and AZURE_TENANT_ID environment variables are needed to create a DefaultAzureCredential object.

```typescript
import { DefaultAzureCredential } from "@azure/identity";
import MessageClient, { MessagesServiceClient } from "@azure-rest/communication-messages";
### Create and authenticate a `MessagesServiceClient`

const endpoint = "https://<resource-name>.communication.azure.com";
const credential = new DefaultAzureCredential();
const client:MessagesServiceClient = MessageClient(endpoint, credential);
```
To use an [Azure Active Directory (AAD) token credential](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/identity/identity/samples/AzureIdentityExamples.md#authenticating-with-a-pre-fetched-access-token),
provide an instance of the desired credential type obtained from the
[@azure/identity](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#credentials) library.

## Send a Template Message with WhatsApp Channel
To authenticate with AAD, you must first `npm` install [`@azure/identity`](https://www.npmjs.com/package/@azure/identity)

`Note: Business always starts the conversation with a template message.`

To send an Template Message, you need add template to your WhatsApp Bussiness Account. For more detail on WhatsApp Template, refer [Create and Manage Templates][create-manage-whatsapp-template]. In below example, we are using

```
Template Name: sample_issue_resolution
Template Language: en_US
Template Body: "Hi {{1}}, were we able to solve the issue that you were facing?"
With Quick Action Button (Yes, No)
```

```typescript
const nameValue:MessageTemplateValue = {
kind: "text",
name: "name",
text: "Arif"
};

const yesAction: MessageTemplateValue = {
kind: "quickAction",
name: "Yes",
payload: "Yes"
};

const noAction: MessageTemplateValue = {
kind: "quickAction",
name: "No",
payload: "No"
};

const templateBindings:MessageTemplateBindings = {
kind: "whatsApp",
body: [
{
refValue: "name"
}
],
buttons: [
{
subType: "quickReply",
refValue: "Yes"
},
{
subType: "quickReply",
refValue: "No"
}
]
};

const template:MessageTemplate = {
name: "sample_issue_resolution",
language: "en_US",
bindings: templateBindings,
values: [nameValue, yesAction, noAction]
};

const result = await client.path("/messages/notifications:send").post({
contentType: "application/json",
body: {
channelRegistrationId: "<Channel_Registration_Id>",
to: ["<to-phone-number-1>"],
kind: "template",
template: template
}
});
if (result.status === "202") {
const response:Send202Response = result as Send202Response;
response.body.receipts.forEach((receipt) => {
console.log("Message sent to:"+receipt.to+" with message id:"+receipt.messageId);
});
} else {
throw new Error("Failed to send message");
}
```

## Send a Text Message with WhatsApp Channel

`Note: Business can't start a conversation with a text message. It needs to be user initiated.`

```typescript
const result = await client.path("/messages/notifications:send").post({
contentType: "application/json",
body: {
channelRegistrationId: "<Channel_Registration_Id>",
to: ["<to-phone-number-1>"],
kind: "text",
content: "Hello World!!"
}
});

if (result.status === "202") {
const response:Send202Response = result as Send202Response;
response.body.receipts.forEach((receipt) => {
console.log("Message sent to:"+receipt.to+" with message id:"+receipt.messageId);
});
} else {
throw new Error("Failed to send message");
}
```

## Send a Media Message with WhatsApp Channel

`Note: Business can't start a conversation with a media message. It needs to be user initiated.`

```typescript
const result = await client.path("/messages/notifications:send").post({
contentType: "application/json",
body: {
channelRegistrationId: "<Channel_Registration_Id>",
to: ["<to-phone-number-1>"],
kind: "image",
mediaUri: "https://<your-media-image-file>"
}
});

if (result.status === "202") {
const response:Send202Response = result as Send202Response;
response.body.receipts.forEach((receipt) => {
console.log("Message sent to:"+receipt.to+" with message id:"+receipt.messageId);
});
} else {
throw new Error("Failed to send message");
}
```
After setup, you can choose which type of [credential](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#credentials) from `@azure/identity` to use.
As an example, [DefaultAzureCredential](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#defaultazurecredential)
can be used to authenticate the client.

Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables:
AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET

## Troubleshooting

Expand All @@ -214,28 +55,3 @@ setLogLevel("info");
```

For more detailed instructions on how to enable logs, you can look at the [@azure/logger package docs](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/core/logger).

## Next steps

Please take a look at the [samples](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/communication/communication-messages-rest/samples) directory for detailed examples on how to use this library.

## Contributing

If you'd like to contribute to this library, please read the [contributing guide](https://github.com/Azure/azure-sdk-for-js/blob/main/CONTRIBUTING.md) to learn more about how to build and test the code.


## Related projects

- [Microsoft Azure SDK for Javascript](https://github.com/Azure/azure-sdk-for-js)

[azure_cli]: https://docs.microsoft.com/cli/azure
[azure_sub]: https://azure.microsoft.com/free/
[azure_portal]: https://portal.azure.com
[azure_powershell]: https://docs.microsoft.com/powershell/module/az.communication/new-azcommunicationservice
[defaultazurecredential]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#defaultazurecredential
[azure_identity]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity
[azure_communication_messaging_qs]: https://learn.microsoft.com/azure/communication-services/concepts/advanced-messaging/whatsapp/whatsapp-overview
[register_whatsapp_business_account]: https://learn.microsoft.com/azure/communication-services/quickstarts/advanced-messaging/whatsapp/connect-whatsapp-business-account
[create-manage-whatsapp-template]: https://developers.facebook.com/docs/whatsapp/business-management-api/message-templates/

![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-js%2Fsdk%2Fcommunication%2Fcommunication-sms%2FREADME.png)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
"mainEntryPointFilePath": "dist/esm/index.d.ts",
"mainEntryPointFilePath": "./dist/esm/index.d.ts",
"docModel": {
"enabled": true
},
Expand All @@ -11,7 +11,7 @@
"dtsRollup": {
"enabled": true,
"untrimmedFilePath": "",
"publicTrimmedFilePath": "dist/communication-messages.d.ts"
"publicTrimmedFilePath": "./types/communication-messages.d.ts"
},
"messages": {
"tsdocMessageReporting": {
Expand All @@ -28,4 +28,4 @@
}
}
}
}
}
6 changes: 0 additions & 6 deletions sdk/communication/communication-messages-rest/assets.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export default [
"@azure/azure-sdk/ts-apiextractor-json-types": "warn",
"@azure/azure-sdk/ts-package-json-types": "warn",
"@azure/azure-sdk/ts-package-json-engine-is-present": "warn",
"tsdoc/syntax": "warn",
},
},
"@azure/azure-sdk/ts-package-json-module": "off",
"@azure/azure-sdk/ts-package-json-files-required": "off",
"@azure/azure-sdk/ts-package-json-main-is-cjs": "off",
"tsdoc/syntax": "warn"
}
}
];
Loading

0 comments on commit f9d0a20

Please sign in to comment.