/// <summary>
 /// Creates a Bot Service. Bot Service is a resource group wide resource type.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='resourceGroupName'>
 /// The name of the resource group within the user's subscription.
 /// </param>
 /// <param name='resourceName'>
 /// The name of the Bot resource.
 /// </param>
 /// <param name='parameters'>
 /// The parameters to provide for the created bot.
 /// </param>
 /// <param name='deploymentInfo'>
 /// The deployment parameters to provide for the bot creation.
 /// </param>
 public static Bot CreateWebAppBot(this IBotsOperations operations, string resourceGroupName, string resourceName, Bot parameters, BotDeploymentInfo deploymentInfo)
 {
     return(operations.CreateWebAppBotAsync(resourceGroupName, resourceName, parameters, deploymentInfo).GetAwaiter().GetResult());
 }
        /// <summary>
        /// Creates a Bot Service. Bot Service is a resource group wide resource type.
        /// </summary>
        /// <param name='operations'>
        /// The operations group for this extension method.
        /// </param>
        /// <param name='resourceGroupName'>
        /// The name of the resource group within the user's subscription.
        /// </param>
        /// <param name='resourceName'>
        /// The name of the Bot resource.
        /// </param>
        /// <param name='parameters'>
        /// The parameters to provide for the created bot.
        /// </param>
        /// <param name='deploymentInfo'>
        /// The deployment parameters to provide for the bot creation.
        /// </param>
        /// <param name='cancellationToken'>
        /// The cancellation token.
        /// </param>
        public static async Task <Bot> CreateFunctionBotAsync(this IBotsOperations operations, string resourceGroupName, string resourceName, Bot parameters, BotDeploymentInfo deploymentInfo, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (string.IsNullOrEmpty(operations.Client.TenantId))
            {
                throw new ValidationException(ValidationRules.CannotBeNull, nameof(operations.Client.TenantId));
            }

            try
            {
                parameters.Kind = Kind.Bot;

                var templateParams = new Dictionary <string, Dictionary <string, object> > {
                    { "zipUrl", new Dictionary <string, object> {
                          { "value", deploymentInfo.BotZipUrl ?? DefaultFunctionBotZipUrl }
                      } },
                    { "botId", new Dictionary <string, object> {
                          { "value", resourceName }
                      } },
                    { "location", new Dictionary <string, object> {
                          { "value", deploymentInfo.DeploymentLocation }
                      } },
                    { "kind", new Dictionary <string, object> {
                          { "value", parameters.Kind }
                      } },
                    { "sku", new Dictionary <string, object> {
                          { "value", parameters.Sku.Name }
                      } },
                    { "siteName", new Dictionary <string, object> {
                          { "value", resourceName }
                      } },
                    { "appId", new Dictionary <string, object> {
                          { "value", parameters.Properties.MsaAppId }
                      } },
                    { "createNewStorage", new Dictionary <string, object> {
                          { "value", deploymentInfo.CreateStorage }
                      } },
                    { "storageAccountName", new Dictionary <string, object> {
                          { "value", resourceName }
                      } },
                    { "storageAccountResourceId", new Dictionary <string, object> {
                          { "value", deploymentInfo.StorageAccountResourceId }
                      } },
                    { "botEnv", new Dictionary <string, object> {
                          { "value", "prod" }
                      } },
                    { "useAppInsights", new Dictionary <string, object> {
                          { "value", deploymentInfo.CreateAppInsights }
                      } },
                    { "appInsightsLocation", new Dictionary <string, object> {
                          { "value", deploymentInfo.AppInsightsLocation }
                      } },
                    { "createServerFarm", new Dictionary <string, object> {
                          { "value", true }
                      } },
                    { "serverFarmId", new Dictionary <string, object> {
                          { "value", $"/subscriptions/{operations.Client.SubscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/serverfarms/{resourceName}" }
                      } },
                    { "serverFarmLocation", new Dictionary <string, object> {
                          { "value", deploymentInfo.ServerFarmLocation }
                      } },
                    { "azureWebJobsBotFrameworkDirectLineSecret", new Dictionary <string, object> {
                          { "value", "" }
                      } },
                };

                var deployParams = new Deployment
                {
                    Properties = new DeploymentProperties
                    {
                        Template   = JObject.Parse(File.ReadAllText(@"Templates\functionapp.template.json")),
                        Parameters = templateParams,
                        Mode       = DeploymentMode.Incremental
                    }
                };

                var resourceClient = new ResourceManagementClient(operations.Client.Credentials);
                resourceClient.SubscriptionId = operations.Client.SubscriptionId;

                var deployResult = resourceClient.Deployments.CreateOrUpdate(resourceGroupName, "bot-deploy", deployParams);

                using (var _result = await operations.GetWithHttpMessagesAsync(resourceGroupName, resourceName, null, cancellationToken).ConfigureAwait(false))
                {
                    return(_result.Body);
                }
            }
            catch (Exception ex)
            {
                throw new ErrorException(BotServiceErrorMessages.CreateOperationFailed, ex);
            }
        }