Unable to process events from azure EventHub after adding blob storage - azure-blob-storage

The simple receiver function was working fine, but I was reading all the events again if the service restarts. To mitigate this added blob storage support for checkpoints.
After adding checkpointStore in the new EventHubConsumerClient it is not reading anything just logs "start event hub subscription"
const consumerClient = new EventHubConsumerClient(
consumerGroup,
connectionString,
eventHubName,
checkpointStore,
)
Am I missing something? Why is it not reaching to processEvents?
import { EventHubConsumerClient, CheckpointStore } from '#azure/event-hubs';
import { ContainerClient } from '#azure/storage-blob';
import { BlobCheckpointStore } from '#azure/eventhubs-checkpointstore-blob';
export async function startReceiverEventService() {
console.log(`Running receiveEvents sample`);
const connectionString = process.env.EVENTHUB_CONNECTION_STRING;
const eventHubName = process.env.EVENTHUB_NAME;
const consumerGroup = process.env.CONSUMER_GROUP_NAME;
const storageConnectionString = process.env.STORAGE_CONNECTION_STRING;
const containerName = process.env.STORAGE_CONTAINER_NAME;
const containerClient = new ContainerClient(
storageConnectionString,
containerName,
);
if (!(await containerClient.exists())) {
await containerClient.create();
}
/** Checkpoints for data consumed */
const checkpointStore: CheckpointStore = new BlobCheckpointStore(
containerClient,
);
/** Consumer connrection */
const consumerClient = new EventHubConsumerClient(
consumerGroup,
connectionString,
eventHubName,
checkpointStore,
);
console.log(' start event hub subscription ');
/** Subscribing the events */
const subscription = consumerClient.subscribe({
processEvents: async (events, context) => {
console.log('processing events -');
/**
* Note: It is possible for `events` to be an empty array.
* This can happen if there were no new events to receive
* in the `maxWaitTimeInSeconds`, which is defaulted to
* 60 seconds.
* The `maxWaitTimeInSeconds` can be changed by setting
* it in the `options` passed to `subscribe()`.
*/
for (const event of events) {
const data = JSON.stringify(event.body);
console.log(
`Received event: '${data}' from partition: '${context.partitionId}' and consumer group: '${context.consumerGroup}'`,
);
// TODO: have to do futher task here
}
try {
// save a checkpoint for the last event now that we've processed this batch.
await context.updateCheckpoint(events[events.length - 1]);
} catch (err) {
console.log(
`Error when checkpointing on partition ${context.partitionId}: `,
err,
);
throw err;
}
console.log(
`Successfully checkpointed event with sequence number: ${
events[events.length - 1].sequenceNumber
} from partition: '${context.partitionId}'`,
);
},
processError: async (err, context) => {
console.log(`Error : ${err}`);
},
});
}

Related

Poll API every 2 seconds and update state until result turns successful/failure - React

I have an array of ids and I need to loop through the array and call an API with the id. The response from the API will have a Status field which ranges from [0,1,2,-1]. I want to call the API and update the state with the responses. I have a working code which has a Promise which gets resolved only if the Status is Finished or Failed. How do I go over about updating the state even while the Status is in Started and Queued.
This is my code.
import React from 'react';
import './style.css';
import { get, includes, keys, forEach, map } from 'lodash';
const Status = {
Queued: 0,
Started: 1,
Finished: 2,
Failed: -1,
};
/**
* Keeps getting data every 2.5 seconds
* If path is Status, keep calling the API every 2.5 seconds until Status is Finished or Failed, and then resolve
* #param url
* #param path
*/
/**
* The MAP where the list of timers are stored
*/
let dataFetchingTimerMap = {};
const clearDataFetchingTimer = (key) => {
/**
* Clear the timeout
*/
clearTimeout(dataFetchingTimerMap[key]);
/**
* Delete the key
*/
delete dataFetchingTimerMap[key];
};
const setDataFetchingTimer = (key, cb, delay) => {
/**
* Save the timeout with a key
*/
dataFetchingTimerMap[key] = window.setTimeout(() => {
/**
* Delete key when it executes
*/
clearDataFetchingTimer(key);
/**
* Execute the callback (loop function)
*/
cb();
}, delay);
};
export const getDataAtIntervals = (url, path) => {
const timerKey = `${url}_${path}`;
clearTimeout(+timerKey);
return new Promise((resolve, reject) => {
(async function loop() {
try {
const resultData = await fetch(url);
const result = await resultData.json();
if (
get(result, path) &&
includes(
[Status.Finished, Status.Failed, Status.FailedWithReturnFile],
get(result, path)
)
) {
/**
* Resolve with the data
*/
return resolve(result);
}
setDataFetchingTimer(timerKey, loop, 2500);
} catch (e) {
reject(e);
}
})();
});
};
/**
* Clear every timeout
*/
export const clearGetDataAtIntervals = () =>
forEach(keys(dataFetchingTimerMap), clearDataFetchingTimer);
export default function App() {
const [state, setState] = React.useState([]);
const handleClick = async () => {
const ids = [1, 2, 3];
const responses = await Promise.all(
map(
ids,
async (id) =>
await getDataAtIntervals(
`https://jsonplaceholder.typicode.com/todos/${id}`,
'completed'
)
)
);
setState(responses);
};
return (
<div>
<h1>Hello StackBlitz!</h1>
<p>Start editing to see some magic happen :)</p>
<button onClick={handleClick}>Click me</button>
</div>
);
}
Please advice. This is my Stackblitz link:
https://stackblitz.com/edit/react-dos3i3?file=src%2FApp.js,src%2Findex.js

Deno oak websocket must have a method that returns an async iterator

I am trying to build up a WebSocket with oak (not the native deno one).
The following code is how I build the server.
import {Application, Router, Context, send } from "https://deno.land/x/oak#v10.6.0/mod.ts";
const runWS = async (ctx: Context, next: () => Promise<unknown>) => {
try{
const ws = await ctx.upgrade();
ws.onopen = () => {
chatConnection(ws);
};
ws.onclose = () => { console.log('Disconnected from the client!');};
}catch{await next();}
}
let sockets = new Map<string, WebSocket>();
const chatConnection = async (ws: WebSocket) => {
console.log('new websocket, ws: ',ws);
const uid = globalThis.crypto.randomUUID();
sockets.set(uid,ws);
console.log('socket: ',sockets);
for await (const ev of ws){
console.log('ev: ', ev);
}
}
export const wsRoutes = new Router()
.get('/ws', runWS);
But in the for loop (at the end), for ws it says Type 'WebSocket' must have a '[Symbol.asyncIterator]()' method that returns an async iterator.. What's the deal with this and how to fix it?
The error message is providing you with useful information: the WebSocket is not AsyncIterable, which means that it cannot be used with a for await...of loop.
Here is the type documentation for WebSocket in Deno. It is (for the most part) the same as the WHATWG standard WebSocket that is documented on MDN.
If your intention is to respond to incoming message events, you'll need to attach an event listener:
webSocket.addEventListener("message", (messageEvent) => {
// Do something in response to each message event
});
Additional:
Here's an observation based on the code you've shown, but not in response to your question:
It's probably more ergonomic to store the sockets as the keys of your map, and the associated state data in the values. (This is the inverse of what you've shown). Here's an example of why:
import {
Router,
type RouterMiddleware,
} from "https://deno.land/x/oak#v10.6.0/mod.ts";
// You seem to want to log data to the console.
// This function will help you easily log only certain properties of objects:
/**
* Functional implementation of the type utility
* [`Pick<Type, Keys>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#picktype-keys)
*/
function pick<T, K extends keyof T>(
obj: T,
keys: readonly K[],
): Pick<T, K> {
const result = {} as Pick<T, K>;
for (const key of keys) result[key] = obj[key];
return result;
}
type SocketData = { id: string };
const socketMap = new Map<WebSocket, SocketData>();
// Do something when a connection is opened
function handleOpen(ev: Event, ws: WebSocket) {
const socketData: SocketData = { id: window.crypto.randomUUID() };
socketMap.set(ws, socketData);
console.log({
event: pick(ev, ["type"]),
socketData,
});
}
// Do something when an error occurs
function handleError(ev: Event, ws: WebSocket) {
const socketData = socketMap.get(ws);
console.log({
event: pick(ev, ["type"]),
socketData,
});
socketMap.delete(ws);
}
// Do something when a connection is closed
function handleClose(ev: CloseEvent, ws: WebSocket) {
ev.code; // number
ev.reason; // string
ev.wasClean; // boolean
const socketData = socketMap.get(ws);
console.log({
event: pick(ev, ["type", "code", "reason", "wasClean"]),
socketData,
});
socketMap.delete(ws);
}
// Do something when a message is received
// Change `unknown` to the type of message payloads used in your application.
// (for example, JSON messages are `string`)
function handleMessage(ev: MessageEvent<unknown>, ws: WebSocket) {
ev.data; // unknown
ev.lastEventId; // string
ev.ports; // readonly MessagePort[]
const socketData = socketMap.get(ws);
if (socketData) {
socketData.id; // string
}
console.log({
event: pick(ev, ["type", "data", "lastEventId", "ports"]),
socketData,
});
}
const webSocketMiddleware: RouterMiddleware<"/ws"> = async (ctx, next) => {
const ws = ctx.upgrade();
ws.addEventListener("open", (ev) => handleOpen(ev, ws));
ws.addEventListener("error", (ev) => handleError(ev, ws));
ws.addEventListener("close", (ev) => handleClose(ev, ws));
ws.addEventListener("message", (ev) => handleMessage(ev, ws));
await next();
};
export const router = new Router();
router.get("/ws", webSocketMiddleware);
This is my updated code. It avoids the problem entirely
import {Application, Router, Context, send } from "https://deno.land/x/oak#v10.6.0/mod.ts";
interface BroadcastObj{
name: string,
mssg: string
}
const runWS = async (ctx: Context, next: () => Promise<unknown>) => {
if(!ctx.isUpgradable){
ctx.throw(501);
}
const uid = globalThis.crypto.randomUUID();
try{
const ws = await ctx.upgrade();
ws.onopen = () => {
chatConnection(ws);
};
ws.onmessage = (m) => {
let mssg = m.data as string;
if(typeof(mssg) === 'string'){
chatMessage(JSON.parse(mssg));
}
};
ws.onerror = (e) => {console.log('error occured: ', e);};
ws.onclose = () => { chatDisconnect(uid);};
}catch{await next();}
}
let sockets = new Map<string, WebSocket>();
const chatConnection = async (ws: WebSocket, uid: string) => {
await sockets.set(uid,ws);
}
const chatMessage = async (msg: BroadcastObj) => {
await sockets.forEach((ws: WebSocket) => {
ws.send(JSON.stringify(msg));
});
}
const chatDisconnect = async (uid: string) => {
await sockets.delete(uid);
}
export const wsRoutes = new Router()
.get('/ws', runWS);

Handle unsubscribe GraphQL subscription

I have an issue with subscription can't be unsubscribe.
Before we start, this is my setup: Apollo Client(graphql-ws) <-> Apollo Server(graphql-ws). On the server, I build a custom PubSub instead of using the one provided.
As you can see here, the client has sent a complete request to server with the id. However, the server is still sending more data to it. I have read somewhere that you have to send GQL_STOP, aka STOP instead. However, this is what Apollo Client is sending.
A bit of code:
Client subscription:
export const useGetDataThroughSubscription = (
resourceIds: number[],
startDate?: Date,
endDate?: Date
) => {
const variables = {
startTime: startDate?.toISOString() ?? '',
endTime: endDate?.toISOString() ?? '',
resourceIds,
};
return useGetDataSubscription({
variables,
...
})
}
Server pubsub:
const createPubSub = <TopicPayload extends { [key: string]: unknown }>(
emitter: EventEmitter = new EventEmitter()
) => ({
publish: <Topic extends Extract<keyof TopicPayload, string>>(
topic: Topic,
payload: TopicPayload[Topic]
) => {
emitter.emit(topic as string, payload);
},
async *subscribe<Topic extends Extract<keyof TopicPayload, string>>(
topic: Topic,
retrievalFunc: (value: TopicPayload[Topic]) => Promise<any>
): AsyncIterableIterator<TopicPayload[Topic]> {
const asyncIterator = on(emitter, topic);
for await (const [value] of asyncIterator) {
const data = await retrievalFunc(value);
yield data;
}
},
Server subscribe to event:
const resolver: Resolvers = {
Subscription: {
[onGetAllLocationsEvent]: {
async *subscribe(_a, _b, ctx) {
const locations = await ...;
yield locations;
const iterator = ctx.pubsub.subscribe(
onGetAllLocationsEvent,
async (id: number) => {
const location = ...;
return location;
}
);
for await (const data of iterator) {
if (data) {
yield [data];
}
}
},
resolve: (payload) => payload,
},
},
};
In this one, if instead of the for loop, I return iterator instead, then the server will send back a complete and stop the subscription all together. That's great, but I want to keep the connection open until client stop listening.
And server publish
ctx.pubsub.publish(onGetAllResourcesEvent, resource.id);
So how should I deal with this?

How can I excute code after the entire request for GraphQL has finished in NestJS?

I'm trying to set up Sentry transactions with something like this:
(A globally registered interceptor)
async intercept(
context: ExecutionContext,
next: CallHandler,
): Promise<Observable<any>> {
const transaction = Sentry.startTransaction({
op: 'gql',
name: 'GraphQLTransaction'
});
this.setTransaction(context, transaction); // adds a `transaction` property to the context
return next.handle().pipe(
tap((...args) => {
transaction.finish();
}),
);
}
and then inside a FieldMiddleware I track spans with something like this:
(A globally registered field middleware)
export const checkRoleMiddleware: FieldMiddleware = async (
ctx: MiddlewareContext,
next: NextFn,
) => {
try {
const { info, context: gqlCtx } = ctx;
const transaction: Transaction = gqlCtx.transaction;
const span = transaction.startChild({
op: 'resolver',
description: `${info.parentType.name}.${info.fieldName}`,
});
const result = await next();
span.finish();
return result;
} catch (e) {
// log error to console, since for some reason errors are silenced in field middlewares
console.error(e);
Sentry.captureException(e);
return next();
}
};
However, it seems that transaction.finished() inside the tap() operator gets called before fields are resolved.
Is there another operator that I should be using?

The bot name is already registered to another bot application

I had previously tried to deploy this bot and had issues. I attempted to delete the resource group and try again from scratch and am getting this error.
Built with: Bot Framework Composer (v1.1.1)
Deploying with: provisionComposer.js
> Deploying Azure services (this could take a while)...
✖
{
"error": {
"code": "InvalidBotData",
"message": "Bot is not valid. Errors: The bot name is already registered to another bot application.. See https://aka.ms/bot-requirements for detailed requirements."
}
}
** Provision failed **
The link in the error message doesn't mention 'bot name' or 'name'.
Does a bot name have to be unique to the subscription, tenant, etc?
Is there a place I need to go to 'un-register' the bot name so that it can be registered to another application? Was deleting the resource group not enough?
Thanks in advance for the assistance.
Best Regards,
Josh
I ran into the same issue. After some modifications to the script I was able to complete the provisioning of resources. Issue was the name used to create some of the resources did not match because of the environment variable appended.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
const chalk = require("chalk");
const fs = require("fs-extra");
const msRestNodeAuth = require("#azure/ms-rest-nodeauth");
const argv = require("minimist")(process.argv.slice(2));
const path = require("path");
const rp = require("request-promise");
const { promisify } = require("util");
const { GraphRbacManagementClient } = require("#azure/graph");
const {
ApplicationInsightsManagementClient,
} = require("#azure/arm-appinsights");
const { AzureBotService } = require("#azure/arm-botservice");
const { ResourceManagementClient } = require("#azure/arm-resources");
const readFile = promisify(fs.readFile);
const ora = require("ora");
const logger = (msg) => {
if (msg.status === BotProjectDeployLoggerType.PROVISION_ERROR) {
console.log(chalk.red(msg.message));
} else if (
msg.status === BotProjectDeployLoggerType.PROVISION_ERROR_DETAILS
) {
console.log(chalk.white(msg.message));
} else {
console.log(chalk.green(msg.message));
}
};
const usage = () => {
const options = [
["subscriptionId", "Azure Subscription Id"],
["name", "Project Name"],
["appPassword", "16 character password"],
["environment", "Environment name (Defaults to dev)"],
["location", "Azure Region (Defaults to westus)"],
["appId", "Microsoft App ID (Will create if absent)"],
[
"tenantId",
"ID of your tenant if required (will choose first in list by default)",
],
["createLuisResource", "Create a LUIS resource? Default true"],
[
"createLuisAuthoringResource",
"Create a LUIS authoring resource? Default true",
],
["createCosmosDb", "Create a CosmosDB? Default true"],
["createStorage", "Create a storage account? Default true"],
["createAppInsights", "Create an AppInsights resource? Default true"],
["createQnAResource", "Create a QnA resource? Default true"],
[
"customArmTemplate",
"Path to runtime ARM template. By default it will use an Azure WebApp template. Pass `DeploymentTemplates/function-template-with-preexisting-rg.json` for Azure Functions or your own template for a custom deployment.",
],
];
const instructions = [
``,
chalk.bold(
"Provision Azure resources for use with Bot Framework Composer bots"
),
`* This script will create a new resource group and the necessary Azure resources needed to operate a Bot Framework bot in the cloud.`,
`* Use this to create a publishing profile used in Composer's "Publish" toolbar.`,
``,
chalk.bold(`Basic Usage:`),
chalk.greenBright(`node provisionComposer --subscriptionId=`) +
chalk.yellow("<Azure Subscription Id>") +
chalk.greenBright(" --name=") +
chalk.yellow("<Name for your environment>") +
chalk.greenBright(" --appPassword=") +
chalk.yellow("<16 character password>"),
``,
chalk.bold(`All options:`),
...options.map((option) => {
return (
chalk.greenBright("--" + option[0]) + "\t" + chalk.yellow(option[1])
);
}),
];
console.log(instructions.join("\n"));
};
// check for required parameters
if (Object.keys(argv).length === 0) {
return usage();
}
if (!argv.name || !argv.subscriptionId || !argv.appPassword) {
return usage();
}
// Get required fields from the arguments
const subId = argv.subscriptionId;
const name = argv.name.toString();
const appPassword = argv.appPassword;
// Get optional fields from the arguments
const environment = argv.environment || "dev";
const location = argv.location || "westus";
const appId = argv.appId; // MicrosoftAppId - generated if left blank
// Get option flags
const createLuisResource = argv.createLuisResource == "false" ? false : true;
const createLuisAuthoringResource =
argv.createLuisAuthoringResource == "false" ? false : true;
const createCosmosDb = argv.createCosmosDb == "false" ? false : true;
const createStorage = argv.createStorage == "false" ? false : true;
const createAppInsights = argv.createAppInsights == "false" ? false : true;
const createQnAResource = argv.createQnAResource == "false" ? false : true;
var tenantId = argv.tenantId ? argv.tenantId : "";
const templatePath =
argv.customArmTemplate ||
path.join(
__dirname,
"DeploymentTemplates",
"template-with-preexisting-rg.json"
);
const BotProjectDeployLoggerType = {
// Logger Type for Provision
PROVISION_INFO: "PROVISION_INFO",
PROVISION_ERROR: "PROVISION_ERROR",
PROVISION_WARNING: "PROVISION_WARNING",
PROVISION_SUCCESS: "PROVISION_SUCCESS",
PROVISION_ERROR_DETAILS: "PROVISION_ERROR_DETAILS",
};
/**
* Create a Bot Framework registration
* #param {} graphClient
* #param {*} displayName
* #param {*} appPassword
*/
const createApp = async (graphClient, displayName, appPassword) => {
try {
const createRes = await graphClient.applications.create({
displayName: displayName,
passwordCredentials: [
{
value: appPassword,
startDate: new Date(),
endDate: new Date(
new Date().setFullYear(new Date().getFullYear() + 2)
),
},
],
availableToOtherTenants: true,
replyUrls: ["https://token.botframework.com/.auth/web/redirect"],
});
return createRes;
} catch (err) {
logger({
status: BotProjectDeployLoggerType.PROVISION_ERROR,
message: err.body.message,
});
return false;
}
};
/**
* Create an Azure resources group
* #param {} client
* #param {*} location
* #param {*} resourceGroupName
*/
const createResourceGroup = async (client, location, resourceGroupName) => {
logger({
status: BotProjectDeployLoggerType.PROVISION_INFO,
message: `> Creating resource group ...`,
});
const param = {
location: location,
};
return await client.resourceGroups.createOrUpdate(resourceGroupName, param);
};
/**
* Format parameters
* #param {} scope
*/
const pack = (scope) => {
return {
value: scope,
};
};
const unpackObject = (output) => {
const unpacked = {};
for (const key in output) {
const objValue = output[key];
if (objValue.value) {
unpacked[key] = objValue.value;
}
}
return unpacked;
};
/**
* For more information about this api, please refer to this doc: https://learn.microsoft.com/en-us/rest/api/resources/Tenants/List
* #param {*} accessToken
*/
const getTenantId = async (accessToken) => {
if (!accessToken) {
throw new Error(
"Error: Missing access token. Please provide a non-expired Azure access token. Tokens can be obtained by running az account get-access-token"
);
}
if (!subId) {
throw new Error(
`Error: Missing subscription Id. Please provide a valid Azure subscription id.`
);
}
try {
const tenantUrl = `https://management.azure.com/subscriptions/${subId}?api-version=2020-01-01`;
const options = {
headers: { Authorization: `Bearer ${accessToken}` },
};
const response = await rp.get(tenantUrl, options);
const jsonRes = JSON.parse(response);
if (jsonRes.tenantId === undefined) {
throw new Error(`No tenants found in the account.`);
}
return jsonRes.tenantId;
} catch (err) {
throw new Error(`Get Tenant Id Failed, details: ${getErrorMesssage(err)}`);
}
};
const getDeploymentTemplateParam = (
appId,
appPwd,
location,
name,
shouldCreateAuthoringResource,
shouldCreateLuisResource,
shouldCreateQnAResource,
useAppInsights,
useCosmosDb,
useStorage
) => {
return {
appId: pack(appId),
appSecret: pack(appPwd),
appServicePlanLocation: pack(location),
botId: pack(name),
shouldCreateAuthoringResource: pack(shouldCreateAuthoringResource),
shouldCreateLuisResource: pack(shouldCreateLuisResource),
shouldCreateQnAResource: pack(shouldCreateQnAResource),
useAppInsights: pack(useAppInsights),
useCosmosDb: pack(useCosmosDb),
useStorage: pack(useStorage),
};
};
/**
* Validate the deployment using the Azure API
*/
const validateDeployment = async (
client,
resourceGroupName,
deployName,
templateParam
) => {
logger({
status: BotProjectDeployLoggerType.PROVISION_INFO,
message: "> Validating Azure deployment ...",
});
const templateFile = await readFile(templatePath, { encoding: "utf-8" });
const deployParam = {
properties: {
template: JSON.parse(templateFile),
parameters: templateParam,
mode: "Incremental",
},
};
return await client.deployments.validate(
resourceGroupName,
deployName,
deployParam
);
};
/**
* Using an ARM template, provision a bunch of resources
*/
const createDeployment = async (
client,
resourceGroupName,
deployName,
templateParam
) => {
const templateFile = await readFile(templatePath, { encoding: "utf-8" });
const deployParam = {
properties: {
template: JSON.parse(templateFile),
parameters: templateParam,
mode: "Incremental",
},
};
return await client.deployments.createOrUpdate(
resourceGroupName,
deployName,
deployParam
);
};
/**
* Format the results into the expected shape
*/
const updateDeploymentJsonFile = async (
client,
resourceGroupName,
deployName,
appId,
appPwd
) => {
const outputs = await client.deployments.get(resourceGroupName, deployName);
if (outputs && outputs.properties && outputs.properties.outputs) {
const outputResult = outputs.properties.outputs;
const applicationResult = {
MicrosoftAppId: appId,
MicrosoftAppPassword: appPwd,
};
const outputObj = unpackObject(outputResult);
if (!createAppInsights) {
delete outputObj.applicationInsights;
}
if (!createCosmosDb) {
delete outputObj.cosmosDb;
}
if (!createLuisAuthoringResource && !createLuisResource) {
delete outputObj.luis;
}
if (!createStorage) {
delete outputObj.blobStorage;
}
const result = {};
Object.assign(result, outputObj, applicationResult);
return result;
} else {
return null;
}
};
const provisionFailed = (msg) => {
logger({
status: BotProjectDeployLoggerType.PROVISION_ERROR,
message: chalk.bold("** Provision failed **"),
});
};
const getErrorMesssage = (err) => {
if (err.body) {
if (err.body.error) {
if (err.body.error.details) {
const details = err.body.error.details;
let errMsg = "";
for (let detail of details) {
errMsg += detail.message;
}
return errMsg;
} else {
return err.body.error.message;
}
} else {
return JSON.stringify(err.body, null, 2);
}
} else {
return JSON.stringify(err, null, 2);
}
};
/**
* Provision a set of Azure resources for use with a bot
*/
const create = async (
creds,
subId,
name,
location,
environment,
appId,
appPassword,
createLuisResource = true,
createLuisAuthoringResource = true,
createQnAResource = true,
createCosmosDb = true,
createStorage = true,
createAppInsights = true
) => {
// If tenantId is empty string, get tenanId from API
if (!tenantId) {
const token = await creds.getToken();
const accessToken = token.accessToken;
// the returned access token will almost surely have a tenantId.
// use this as the default if one isn't specified.
if (token.tenantId) {
tenantId = token.tenantId;
logger({
status: BotProjectDeployLoggerType.PROVISION_INFO,
message: `> Using Tenant ID: ${tenantId}`,
});
} else {
tenantId = await getTenantId(accessToken);
}
}
const graphCreds = new msRestNodeAuth.DeviceTokenCredentials(
creds.clientId,
tenantId,
creds.username,
"graph",
creds.environment,
creds.tokenCache
);
const graphClient = new GraphRbacManagementClient(graphCreds, tenantId, {
baseUri: "https://graph.windows.net",
});
// If the appId is not specified, create one
if (!appId) {
logger({
status: BotProjectDeployLoggerType.PROVISION_INFO,
message: "> Creating App Registration ...",
});
// create the app registration
const appCreated = await createApp(
graphClient,
`${name}-${environment}`,
appPassword
);
if (appCreated === false) {
return provisionFailed();
}
// use the newly created app
appId = appCreated.appId;
}
logger({
status: BotProjectDeployLoggerType.PROVISION_INFO,
message: `> Create App Id Success! ID: ${appId}`,
});
const resourceGroupName = `${name}-${environment}`;
const deployName = `${name}-${environment}-deploy`;
// timestamp will be used as deployment name
const timeStamp = new Date().getTime().toString();
const client = new ResourceManagementClient(creds, subId);
// Create a resource group to contain the new resources
try {
const rpres = await createResourceGroup(
client,
location,
resourceGroupName
);
} catch (err) {
logger({
status: BotProjectDeployLoggerType.PROVISION_ERROR,
message: getErrorMesssage(err),
});
return provisionFailed();
}
// Caste the parameters into the right format
const deploymentTemplateParam = getDeploymentTemplateParam(
appId,
appPassword,
location,
`${name}-${environment}`,
createLuisAuthoringResource,
createQnAResource,
createLuisResource,
createAppInsights,
createCosmosDb,
createStorage
);
// Validate the deployment using the Azure API
const validation = await validateDeployment(
client,
resourceGroupName,
deployName,
deploymentTemplateParam
);
// Handle validation errors
if (validation.error) {
logger({
status: BotProjectDeployLoggerType.PROVISION_ERROR,
message: `! Error: ${validation.error.message}`,
});
if (validation.error.details) {
logger({
status: BotProjectDeployLoggerType.PROVISION_ERROR_DETAILS,
message: JSON.stringify(validation.error.details, null, 2),
});
}
logger({
status: BotProjectDeployLoggerType.PROVISION_ERROR,
message: `+ To delete this resource group, run 'az group delete -g ${resourceGroupName} --no-wait'`,
});
return provisionFailed();
}
// Create the entire stack of resources inside the new resource group
// this is controlled by an ARM template identified in templatePath
logger({
status: BotProjectDeployLoggerType.PROVISION_INFO,
message: `> Deploying Azure services (this could take a while)...`,
});
const spinner = ora().start();
try {
const deployment = await createDeployment(
client,
resourceGroupName,
deployName,
deploymentTemplateParam
);
// Handle errors
if (deployment._response.status != 200) {
spinner.fail();
logger({
status: BotProjectDeployLoggerType.PROVISION_ERROR,
message: `! Template is not valid with provided parameters. Review the log for more information.`,
});
logger({
status: BotProjectDeployLoggerType.PROVISION_ERROR,
message: `! Error: ${validation.error}`,
});
logger({
status: BotProjectDeployLoggerType.PROVISION_ERROR,
message: `+ To delete this resource group, run 'az group delete -g ${resourceGroupName} --no-wait'`,
});
return provisionFailed();
}
} catch (err) {
spinner.fail();
logger({
status: BotProjectDeployLoggerType.PROVISION_ERROR,
message: getErrorMesssage(err),
});
return provisionFailed();
}
// If application insights created, update the application insights settings in azure bot service
if (createAppInsights) {
logger({
status: BotProjectDeployLoggerType.PROVISION_INFO,
message: `> Linking Application Insights settings to Bot Service ...`,
});
const appinsightsClient = new ApplicationInsightsManagementClient(
creds,
subId
);
const appComponents = await appinsightsClient.components.get(
resourceGroupName,
resourceGroupName
);
const appinsightsId = appComponents.appId;
const appinsightsInstrumentationKey = appComponents.instrumentationKey;
const apiKeyOptions = {
name: `${resourceGroupName}-provision-${timeStamp}`,
linkedReadProperties: [
`/subscriptions/${subId}/resourceGroups/${resourceGroupName}/providers/microsoft.insights/components/${resourceGroupName}/api`,
`/subscriptions/${subId}/resourceGroups/${resourceGroupName}/providers/microsoft.insights/components/${resourceGroupName}/agentconfig`,
],
linkedWriteProperties: [
`/subscriptions/${subId}/resourceGroups/${resourceGroupName}/providers/microsoft.insights/components/${resourceGroupName}/annotations`,
],
};
const appinsightsApiKeyResponse = await appinsightsClient.aPIKeys.create(
resourceGroupName,
resourceGroupName,
apiKeyOptions
);
const appinsightsApiKey = appinsightsApiKeyResponse.apiKey;
logger({
status: BotProjectDeployLoggerType.PROVISION_INFO,
message: `> AppInsights AppId: ${appinsightsId} ...`,
});
logger({
status: BotProjectDeployLoggerType.PROVISION_INFO,
message: `> AppInsights InstrumentationKey: ${appinsightsInstrumentationKey} ...`,
});
logger({
status: BotProjectDeployLoggerType.PROVISION_INFO,
message: `> AppInsights ApiKey: ${appinsightsApiKey} ...`,
});
if (appinsightsId && appinsightsInstrumentationKey && appinsightsApiKey) {
const botServiceClient = new AzureBotService(creds, subId);
const botCreated = await botServiceClient.bots.get(
resourceGroupName,
`${name}-${environment}`
);
if (botCreated.properties) {
botCreated.properties.developerAppInsightKey = appinsightsInstrumentationKey;
botCreated.properties.developerAppInsightsApiKey = appinsightsApiKey;
botCreated.properties.developerAppInsightsApplicationId = appinsightsId;
const botUpdateResult = await botServiceClient.bots.update(
resourceGroupName,
`${name}-${environment}`,
botCreated
);
if (botUpdateResult._response.status != 200) {
logger({
status: BotProjectDeployLoggerType.PROVISION_ERROR,
message: `! Something went wrong while trying to link Application Insights settings to Bot Service Result: ${JSON.stringify(
botUpdateResult
)}`,
});
throw new Error(`Linking Application Insights Failed.`);
}
logger({
status: BotProjectDeployLoggerType.PROVISION_INFO,
message: `> Linking Application Insights settings to Bot Service Success!`,
});
} else {
logger({
status: BotProjectDeployLoggerType.PROVISION_WARNING,
message: `! The Bot doesn't have a keys properties to update.`,
});
}
}
}
spinner.succeed("Success!");
// Validate that everything was successfully created.
// Then, update the settings file with information about the new resources
const updateResult = await updateDeploymentJsonFile(
client,
resourceGroupName,
deployName,
appId,
appPassword
);
// Handle errors
if (!updateResult) {
const operations = await client.deploymentOperations.list(
resourceGroupName,
deployName
);
if (operations) {
const failedOperations = operations.filter(
(value) =>
value &&
value.properties &&
value.properties.statusMessage.error !== null
);
if (failedOperations) {
failedOperations.forEach((operation) => {
switch (
operation &&
operation.properties &&
operation.properties.statusMessage.error.code &&
operation.properties.targetResource
) {
case "MissingRegistrationForLocation":
logger({
status: BotProjectDeployLoggerType.PROVISION_ERROR,
message: `! Deployment failed for resource of type ${operation.properties.targetResource.resourceType}. This resource is not avaliable in the location provided.`,
});
break;
default:
logger({
status: BotProjectDeployLoggerType.PROVISION_ERROR,
message: `! Deployment failed for resource of type ${operation.properties.targetResource.resourceType}.`,
});
logger({
status: BotProjectDeployLoggerType.PROVISION_ERROR,
message: `! Code: ${operation.properties.statusMessage.error.code}.`,
});
logger({
status: BotProjectDeployLoggerType.PROVISION_ERROR,
message: `! Message: ${operation.properties.statusMessage.error.message}.`,
});
break;
}
});
}
} else {
logger({
status: BotProjectDeployLoggerType.PROVISION_ERROR,
message: `! Deployment failed. Please refer to the log file for more information.`,
});
}
}
return updateResult;
};
console.log(chalk.bold("Login to Azure:"));
msRestNodeAuth
.interactiveLogin({ domain: tenantId })
.then(async (creds) => {
const createResult = await create(
creds,
subId,
name,
location,
environment,
appId,
appPassword,
createLuisResource,
createLuisAuthoringResource,
createQnAResource,
createCosmosDb,
createStorage,
createAppInsights
);
if (createResult) {
console.log("");
console.log(
chalk.bold(
`Your Azure hosting environment has been created! Copy paste the following configuration into a new profile in Composer's Publishing tab.`
)
);
console.log("");
const token = await creds.getToken();
const profile = {
accessToken: token.accessToken,
name: `${name}-${environment}`,
environment: environment,
hostname: `${name}-${environment}`,
luisResource: `${name}-${environment}-luis`,
settings: createResult,
};
console.log(chalk.white(JSON.stringify(profile, null, 2)));
console.log("");
}
})
.catch((err) => {
console.error(err);
});
Hope it works for you as well!

Resources