botkit icon indicating copy to clipboard operation
botkit copied to clipboard

MS Teams: Property 'api' does not exist on type 'BotWorker'

Open MoosaSaadat opened this issue 2 years ago • 10 comments

I need to get the user email. I am trying to run the following feature from the docs:

controller.hears('who am i', 'direct_message, direct_mention', function(bot, message) {
    bot.api.getUserById(message.channel, message.user, function(err, user) {
        if (err) {
          bot.reply(message,'Error loading user:' + err);
        } else {
          bot.reply(message,'You are ' + user.name + ' and your email is ' + user.email + ' and your user id is ' + user.id);
        }
    });
});

But, I get the following error:

error TS2339: Property 'api' does not exist on type 'BotWorker'.

Maybe the docs are outdated? I also tried using the TeamsBotWorker.teams for getMember(). But, I couldn't get it working either.

  • Botkit version: 4.10.0
  • Messaging Platform: MS Teams
  • Node version: v16.13.2
  • OS: Linux

MoosaSaadat avatar Feb 28 '22 07:02 MoosaSaadat

When I use

bot.teams.getTeamDetails(bot.getConfig('context'));

I get the following error:

error TS2576: Property 'getTeamDetails' does not exist on type 'TeamsInfo'. Did you mean to access the static member 'TeamsInfo.getTeamDetails' instead?

MoosaSaadat avatar Feb 28 '22 07:02 MoosaSaadat

@benbrown can you help here?

MoosaSaadat avatar Mar 02 '22 15:03 MoosaSaadat

@MoosaSaadat I'm doing getMember to get user e-mail on 4.10.0

await bot.teams.getMember(bot.getConfig('context'), message.user);

bzoel avatar Mar 09 '22 17:03 bzoel

@billyzoellers Although it works fine, I am using TypeScript and get the following error:

Property 'getMember' does not exist on type 'TeamsInfo'. Did you mean to access the static member 'TeamsInfo.getMember' instead? ts(2576)

I have to disable type check in order to compile it

// @ts-nocheck

MoosaSaadat avatar Mar 10 '22 05:03 MoosaSaadat

Btw @benbrown when this issue is resolved, I can create a PR to update docs for using bot.teams.getMember() instead of bot.api.getUserById()

MoosaSaadat avatar Mar 10 '22 05:03 MoosaSaadat

Hi guys.

For historical accuracy - the doc you linked to in your OP is to the previous version of Botkit, not the current 4.0 version.

Accessing the bot.teams object is what you want to do... However I am not sure how to deal with the static member warning. Disabling the type check should have no impact on the behavior...

benbrown avatar Mar 23 '22 19:03 benbrown

@benbrown as per the specification of static:

Neither static methods nor static properties can be called on instances of the class

The error we are facing is also demonstrated in the first example:

class Triple {
  ...
  static calculate(n = 1) {
    return n * 3;
  }
}
...
const tp = new Triple();
...
// This throws because calculate() is a static member, not an instance member.
console.log(tp.calculate());                // 'tp.calculate is not a function'

So, in our case, access to the TeamsInfo class is required. It should be exported from botkit so that we can use it.

MoosaSaadat avatar Mar 24 '22 09:03 MoosaSaadat

Can you import it directly from the botbuilder library?

https://github.com/howdyai/botkit/blob/main/packages/botkit/src/core.ts#L374

benbrown avatar Mar 24 '22 15:03 benbrown

Yes, I can import it directly and it works fine like that. But, I guess it is not a standard practice to import dependencies which are not mentioned in package.json? Also, eslint will show this problem:

'botbuilder' should be listed in the project's dependencies. Run 'npm i -S botbuilder' to add it - eslint [import/no-extraneous-dependencies](https://github.com/import-js/eslint-plugin-import/blob/v2.25.4/docs/rules/no-extraneous-dependencies.md)

Is there a way to list it in dependencies without having to download multiple versions of it? Will have to make sure that the listed dependency has the same version as botkit dependency to avoid unnecessary duplication.

MoosaSaadat avatar Mar 26 '22 16:03 MoosaSaadat

@MoosaSaadat Correct, you should use the same version of botbuilder as Botkit to avoid conflicts.

benbrown avatar Mar 29 '22 15:03 benbrown