Botkit: Microsoft Teams not displaying the bot reply after some time

Created on 21 Nov 2017  路  15Comments  路  Source: howdyai/botkit

Hi,

I am not sure if this is an issue with botkit per say, but noticing an unexpected behavior while used with Microsoft Teams (With Slack, no such behavior observed).

I have a very basic Teams bot, exactly, as described at https://github.com/howdyai/botkit/blob/master/docs/readme-teams.md. I only added some console.log messages just before the bot replies.

The bot is hosted in AWS as an ECS service. It works fine for about an hour and a half. After that, Teams does not display the messages from the bot. I tried with bot the paid and developer versions of Microsoft Teams.

From the console messages I added, I can see that the bot is replying to the requests from Teams. If I restart the bot, the communication again works for next 1.5 hours.

Any clue on what might be happening?

Thanks!

Most helpful comment

@peterswimm It is an access token issue. The token that bot generate using聽login.microsoftonline.com/common/oauth2/v2.0/token聽to access Microsoft Teams API has a lifespan of 1 hour. Teams Botkit library api (Teams.js TeamsAPI.js`) do not validate/refresh token on expiry. So after an hour of uptime when bot try to use the same token to get user profile information from MS Teams (getUserById) it fails with 401. Since bot reply message doesn't contain user information, message is not reaching Microsoft Teams client. After bot restart/redeploy the new token gets generated and communication works again for 1 hour.
getuserfromteams
accesstoken-401

All 15 comments

Maybe your ECS instance is idling and timing out? There are a few threads here from users who ran into trouble hosting on Amazon, try searching the issue archive.

Maybe you would have better luck hosting it on Glitch
https://glitch.com/~botkit-teams

@peterswimm I found similar issues reported earlier for Slack Bot- https://github.com/howdyai/botkit/issues/442 https://github.com/howdyai/botkit/issues/276

So just thinking if we are running into a similar issue in Teams Bot.
I am using Botkit version 0.6.4 , Node version 6.9.5

@peterswimm I hosted botkit-starter-teams bot on Glitch. It works fine for few hours and after that I don't get response from bot in Teams. I see Error loading user profile info: User not found error in glitch project logs.

REQ >  /teams/receive
Error loading user profile info:  User not found

info: >   [End]  5  Conversation with  29:1AQMAftWtvoevGMnHMw5zaIrFUlXc91U-3OVn2gM4xOIcrWBgSHrlOReHL3bYUwdSx7thDD9Vjj_0xq42B9pijA in a:1GqF-eTIzCo0Y9kiIb_VqotcouKbEJKWqeMeBFamvOF_9rtBJdGLyok1vw_AmSoMaocNEm_qi3zXePlEsHIWFxksx9hefebYKCYZrfBianbu00o2ZWHCTtUCL3PUez-le

info: [End]  5  Task for  29:1AQMAftWtvoevGMnHMw5zaIrFUlXc91U-3OVn2gM4xOIcrWBgSHrlOReHL3bYUwdSx7thDD9Vjj_0xq42B9pijA in a:1GqF-eTIzCo0Y9kiIb_VqotcouKbEJKWqeMeBFamvOF_9rtBJdGLyok1vw_AmSoMaocNEm_qi3zXePlEsHIWFxksx9hefebYKCYZrfBianbu00o2ZWHCTtUCL3PUez-le

screen shot 2017-11-21 at 7 17 29 pm
Bot stops responding..

screen shot 2017-11-21 at 7 18 55 pm
Error is glitch logs..

After bot restart, the communication again works for few hours and no errors in logs.
screen shot 2017-11-21 at 7 25 15 pm

screen shot 2017-11-21 at 7 24 58 pm

Ah yes the timeout question. You should use botkit storage to save your state on a timeout, so it can cleanly resume if a user leaves in the middle of a conversation. We have an article on working with data and studio you might find useful"
https://botkit.groovehq.com/knowledge_base/topics/how-to-store-data-for-your-users

@peterswimm It is an access token issue. The token that bot generate using聽login.microsoftonline.com/common/oauth2/v2.0/token聽to access Microsoft Teams API has a lifespan of 1 hour. Teams Botkit library api (Teams.js TeamsAPI.js`) do not validate/refresh token on expiry. So after an hour of uptime when bot try to use the same token to get user profile information from MS Teams (getUserById) it fails with 401. Since bot reply message doesn't contain user information, message is not reaching Microsoft Teams client. After bot restart/redeploy the new token gets generated and communication works again for 1 hour.
getuserfromteams
accesstoken-401

Good detective work @narenderpal - I was about to suggest it was a refresh token issue.

@narenderpal Care to submit a PR to fix this? @billbliss Hi!

Hey @narenderpal did you get a PR for this, I'm also seeing the same issue, waaaa!!! If no I can have a crack, I've also got a documentation update I owe BotKit :) I just found this post after getting into the API and coming to the same conclusion.

@peterswimm setTimeout?, every 50mins? Teams.js

function tokenHandler(err) { if (err) { // this is a fatal error - could not create a Teams API client throw new Error(err); } setTimeout(controller.api.getToken(tokenHandler), 3000000); }; controller.api.getToken(tokenHandler);

To follow up, I fixed this last night, bot stayed up all night which was great, I'll write some test coverage and submit a PR.

Please do, and link this thread!

PR fixing the behaviour and a bit of coverage #1230

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fieldcorbett picture fieldcorbett  路  4Comments

znat picture znat  路  4Comments

koubenecn picture koubenecn  路  3Comments

stelio picture stelio  路  4Comments

dfischer picture dfischer  路  4Comments