Teams Toolkit – I need help integrating long term storage into my bot

Overview: I built a bot using teams toolkit – I have had this issue for a while where the bot will lose references for the member when a new version comes out or after some time of not sending notifications. In order to counteract this, I’m implementing CosmosDB into the bot to store the member object for each user. This way when the bot loses the member, we can target the saved member object

It’s important to know that I’m targeting a member object and not a conversation or a conversation reference so this is not a conversation context issue. Ill share how the targeting works below :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>export async function handleDailyCard(
target: BotBuilderCloudAdapter.Member,
payload: DailyCardData
) {
const baseCard = // card_information_here
// CARD LOGIC HERE
try {
console.log("sending message to", target);
await target?.sendAdaptiveCard(
AdaptiveCards.declare<DailyCardData>(baseCard).render({
memberId: payload.memberId,
// other fields here
})
);
} catch (error) {
console.log("error sending note", error);
console.log(target);
}
</code>
<code>export async function handleDailyCard( target: BotBuilderCloudAdapter.Member, payload: DailyCardData ) { const baseCard = // card_information_here // CARD LOGIC HERE try { console.log("sending message to", target); await target?.sendAdaptiveCard( AdaptiveCards.declare<DailyCardData>(baseCard).render({ memberId: payload.memberId, // other fields here }) ); } catch (error) { console.log("error sending note", error); console.log(target); } </code>
export async function handleDailyCard(
  target: BotBuilderCloudAdapter.Member,
  payload: DailyCardData
) {
  const baseCard = // card_information_here
  // CARD LOGIC HERE

  try {
    console.log("sending message to", target);
    await target?.sendAdaptiveCard(
      AdaptiveCards.declare<DailyCardData>(baseCard).render({
        memberId: payload.memberId,
        // other fields here
      })
    );
  } catch (error) {
    console.log("error sending note", error);
    console.log(target);
  }

now in my index.ts file – im inititalizing the bot and cosmos db like so:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const cosmosStorage = new CosmosDbPartitionedStorage({
cosmosDbEndpoint: "cosmos-db-endpoint",
authKey:"my-auth-key",
databaseId: "database-id",
containerId: "container-id",
compatibilityMode: false,
});
const teamsBot = new TeamsBot();
</code>
<code>const cosmosStorage = new CosmosDbPartitionedStorage({ cosmosDbEndpoint: "cosmos-db-endpoint", authKey:"my-auth-key", databaseId: "database-id", containerId: "container-id", compatibilityMode: false, }); const teamsBot = new TeamsBot(); </code>
const cosmosStorage = new CosmosDbPartitionedStorage({
  cosmosDbEndpoint: "cosmos-db-endpoint",
  authKey:"my-auth-key",
  databaseId: "database-id",
  containerId: "container-id",
  compatibilityMode: false,
});

const teamsBot = new TeamsBot();

in the same index file – i define the functions to store and retrieve items from cosmos-

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>async function storeMemberInCosmos(member: BotBuilderCloudAdapter.Member) {
const memberData = {
id: member.account.aadObjectId, // Use the member ID as the document ID
memberInfo: member,
};
try {
// Write the full member object to Cosmos DB
await cosmosStorage.write({ [member.account.aadObjectId]: memberData });
} catch (error) {
console.error("Error storing member in Cosmos DB:", error);
}
}
async function getMemberFromCosmos(
memberId: string
): Promise<BotBuilderCloudAdapter.Member | null> {
try {
const memberData = await cosmosStorage.read([memberId]);
if (memberData && memberData[memberId]) {
const memberInfo = memberData[memberId].memberInfo;
const adapter = notificationApp.adapter;
// Here I'm reconstructing the parent portion with the adapter - allows me to use built in functions
const parent = {
...memberInfo.parent,
adapter: adapter, // Reassigning adapter here - might cause issues
};
// Rebuilding the member object with the adapter
const rebuiltMember = new BotBuilderCloudAdapter.Member(
parent,
memberInfo.account
);
return rebuiltMember;
} else {
console.log("Member not found in Cosmos DB");
return null;
}
} catch (error) {
console.error("Error retrieving member from Cosmos DB:", error);
return null;
}
}
</code>
<code>async function storeMemberInCosmos(member: BotBuilderCloudAdapter.Member) { const memberData = { id: member.account.aadObjectId, // Use the member ID as the document ID memberInfo: member, }; try { // Write the full member object to Cosmos DB await cosmosStorage.write({ [member.account.aadObjectId]: memberData }); } catch (error) { console.error("Error storing member in Cosmos DB:", error); } } async function getMemberFromCosmos( memberId: string ): Promise<BotBuilderCloudAdapter.Member | null> { try { const memberData = await cosmosStorage.read([memberId]); if (memberData && memberData[memberId]) { const memberInfo = memberData[memberId].memberInfo; const adapter = notificationApp.adapter; // Here I'm reconstructing the parent portion with the adapter - allows me to use built in functions const parent = { ...memberInfo.parent, adapter: adapter, // Reassigning adapter here - might cause issues }; // Rebuilding the member object with the adapter const rebuiltMember = new BotBuilderCloudAdapter.Member( parent, memberInfo.account ); return rebuiltMember; } else { console.log("Member not found in Cosmos DB"); return null; } } catch (error) { console.error("Error retrieving member from Cosmos DB:", error); return null; } } </code>
async function storeMemberInCosmos(member: BotBuilderCloudAdapter.Member) {
  const memberData = {
    id: member.account.aadObjectId, // Use the member ID as the document ID
    memberInfo: member,
  };

  try {
    // Write the full member object to Cosmos DB
    await cosmosStorage.write({ [member.account.aadObjectId]: memberData }); 
  } catch (error) {
    console.error("Error storing member in Cosmos DB:", error);
  }
}

async function getMemberFromCosmos(
  memberId: string
): Promise<BotBuilderCloudAdapter.Member | null> {
  try {
    const memberData = await cosmosStorage.read([memberId]);

    if (memberData && memberData[memberId]) {

      const memberInfo = memberData[memberId].memberInfo;
      const adapter = notificationApp.adapter;

      // Here I'm reconstructing the parent portion with the adapter - allows me to use built in functions 
      const parent = {
        ...memberInfo.parent,
        adapter: adapter, // Reassigning adapter here - might cause issues
      };

      // Rebuilding the member object with the adapter
      const rebuiltMember = new BotBuilderCloudAdapter.Member(
        parent,
        memberInfo.account
      );

      return rebuiltMember;
    } else {
      console.log("Member not found in Cosmos DB");
      return null;
    }
  } catch (error) {
    console.error("Error retrieving member from Cosmos DB:", error);
    return null;
  }
}

Now my solution seems to be working locally with the storage but when I after I deploy it I seem to be getting a 500 server error and the logs dont really provide much information there- I think the deployment is successful and its getting zipped correctly- the pipeline and all the test cases seem to pass

I have no deployment issues if the storage integration is not included- do I need to fix the .yml files or deployment scripts? I hope my question is clear, but I can provide additional information if needed for clarity

2

For the 500 error, you should look into the logs and see what the detail is, or add logging to help with that. In any case, you’re basing rolling your own state management when there is something built into the bot framework for this for you already – See https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-howto-v4-state?view=azure-bot-service-4.0&tabs=csharp for more on State management. There are various types, depending on your needs, but you’d want “User” (versus “Conversation”) state, as you noted in your question.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật