我有一个Web应用程序,如果它在Microsoft Teams中运行,则需要执行特定的代码,但是我还没有弄清是否有任何方法可以判断您的应用程序是否在团队中运行。有任何想法或见识吗?
编辑:
对于想知道我们最终使用以下两个答案的任何人,在应用程序启动时,它将检查应用程序的网址以查看其是否包含“ / teams”。明确告知team应用程序指向{app_name} / teams,如果是这种情况,它将运行以下代码块:
import * as microsoftTeams from '@microsoft/teams-js';
if (window.location.pathname.includes('teams')) {
microsoftTeams.initialize(() => {
microsoftTeams.getContext(context => {
store.dispatch(teamsDetected(context.hostClientType!));
try {
microsoftTeams.settings.registerOnSaveHandler(saveEvent => {
microsoftTeams.settings.setSettings({
websiteUrl: window.location.href,
contentUrl: `${window.location.href}&teams`,
entityId: context.entityId,
suggestedDisplayName: document.title
});
saveEvent.notifySuccess();
});
microsoftTeams.settings.setValidityState(true);
} catch (err) {
console.error('failed to set teams settings')
}
});
});
}
答案 0 :(得分:3)
您可能曾经遇到过,如果您不是团队中的 ,则对microsoftTeams.getContext(...)
的调用永远不会返回。
因此,我有一个用setInterval
监视的标志,如果this._teamsContext
是真实的,并且具有合理的值;并且只有当有this._hasAttemptedConnection
这有点像个回合。
稍后我实现的另一种机制是传递带有URL入口点的标志(在我们的示例中:这是“团队”标签)https://<oururl>?context=teams
,并且仅在团队中时才使用团队代码路径。
我在Microsoft Teams .js github中看到要求从microsoftTeams.getContext(...)
引用返回失败的请求:is there any API to detect running in Teams or not?
在标记之前,我有一些看起来像这样的Typescript代码
WireTeams(): Promise<boolean> {
this._hasAttemptedConnection = false
return new Promise<boolean>((resolve, reject) => {
microsoftTeams.initialize()
microsoftTeams.getContext((context) => {
if (context === null || context === undefined) {
resolve(false)
}
this._teamsContext = context
})
})
this._hasAttemptedConnection = true
}