Skip to content

Instantly share code, notes, and snippets.

@ejazhussain
Last active June 22, 2024 11:21
Show Gist options
  • Select an option

  • Save ejazhussain/f54c8125175bb2d59ccd4875a88147a9 to your computer and use it in GitHub Desktop.

Select an option

Save ejazhussain/f54c8125175bb2d59ccd4875a88147a9 to your computer and use it in GitHub Desktop.
Microsoft Copilot Plugin - Calling external web service
import { default as axios } from "axios";
import * as querystring from "querystring";
import {
TeamsActivityHandler,
CardFactory,
TurnContext,
MessagingExtensionQuery,
MessagingExtensionResponse,
} from "botbuilder";
import * as ACData from "adaptivecards-templating";
import productCard from "./adaptiveCards/productCard.json";
import config from "./config";
export class SearchApp extends TeamsActivityHandler {
constructor() {
super();
}
// Search.
public async handleTeamsMessagingExtensionQuery(
context: TurnContext,
query: MessagingExtensionQuery
): Promise<MessagingExtensionResponse> {
//Extracting parameters from the user prompt
const searchQuery = query.parameters[0].value;
var productName = this.getQueryData(query, "productName");
var productDescription = this.getQueryData(query, "productDescription");
var revenueType = this.getQueryData(query, "revenueType");
var serviceArea = this.getQueryData(query, "serviceArea");
var serviceAreaOwner = this.getQueryData(query, "serviceAreaOwner");
const data = {
productName: productName,
productDescription: productDescription,
revenueType: revenueType,
serviceArea: serviceArea,
serviceAreaOwner: serviceAreaOwner,
};
const attachments = [];
try {
// Invoke the Azure Function, passing in search query parameters
const response = await axios.post(config.functionAppUrl, data);
response.data.forEach((obj) => {
const template = new ACData.Template(productCard);
const card = template.expand({
$root: {
productName: obj.productName,
productDescription: obj.productDescription,
skuid: obj.skuid,
catalogue: obj.catalogue,
revenueType: obj.revenueType,
plPostingGroup: obj.plPostingGroup,
serviceArea: obj.serviceArea,
serviceGroup: obj.serviceGroup,
serviceAreaOwner: obj.serviceAreaOwner,
documents: obj.documents,
},
});
const preview = CardFactory.heroCard(obj.productName);
const attachment = { ...CardFactory.adaptiveCard(card), preview };
attachments.push(attachment);
});
}
catch (error) {
console.error(error);
}
return {
composeExtension: {
type: "result",
attachmentLayout: "list",
attachments: attachments,
},
};
}
private getQueryData(query: MessagingExtensionQuery, key: string): string {
if (!query?.parameters?.length) {
return '';
}
const foundPair = query.parameters.find(pair => pair.name === key);
return foundPair?.value?.toString() ?? '';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment