目錄

使用Angular及Teams ToolKit建立Teams App

在B站看到 2021年9月 在對岸 微軟 Reactor 關於 Teams App 的活動, 也剛剛好~公司也有小小的需求希望能在 Teams 上做一些操作, 所以就順便動手研究了。

爬了很多文看了很多Doc, 發現大部分都是用 React 寫的, 關於 Angular 的部分可以說是幾乎沒有(不知道是太少人用還是太Easy🤣)。

所以這篇就記錄一下研究的過程!

準備 Microsoft 365 組織

必要開發工具

建立步驟

  1. 建立一個新資料夾

    1
    2
    
    cd D:\
    mkdir Demo
    
  2. 在此資料夾內以範本建立專案

    1
    2
    3
    
    teamsfx new template hello-world-tab-without-sso --folder "./Demo"
    cd ./Demo/hello-world-tab-without-sso
    code .
    
  3. 將 tabs 資料夾內全部檔案刪除, 並新增 Angular 專案

    https://user-images.githubusercontent.com/33840759/180796003-47d65766-49d6-48e5-9323-b55285092bd9.png
    1
    2
    
     ng new tabs
     cd ./tabs
    
  4. 加入Teams App 在 Compiler 時必要套件

    1
    2
    
     npm install @microsoft/teams-js
     npm install @types/node env-cmd cross-env --save-dev #後兩套件是對於開發環境變數做操做的, 如不需要可以不用裝(根據TeamsToolKit預設是有安裝的)
    
  5. 開啟 tabs內的Package.json 加入以下命令

    1
    2
    3
    4
    
     "dev:teamsfx": "env-cmd --silent -f .env.teamsfx.local npm run start",
     "install:teamsfx": "npm install",
     "build:teamsfx": "cross-env-shell \"env-cmd -f .env.teamsfx.${TEAMS_FX_ENV} npm run build\"",
     "build:teamsfx:dev": "cross-env TEAMS_FX_ENV=dev npm run build:teamsfx"
    
    https://user-images.githubusercontent.com/33840759/180758144-796ba196-56ae-49ba-b9e8-f3ab0c869167.png
  6. 點選 Teams ToolKit, 並點選 編輯 manifest

    https://user-images.githubusercontent.com/33840759/180759058-2937357d-b283-4896-b15d-c922b4d3520e.png
  7. 編輯 manifest , 修改以下欄位

    https://user-images.githubusercontent.com/33840759/180762949-7df6b1d7-660f-45ea-a678-a38ee049a9fa.png

    安裝時所顯示的畫面

    https://user-images.githubusercontent.com/33840759/180763375-e275734b-fd59-4ef5-87de-d837d8302e7e.png
  8. 建立 Teams Config Component

    1
    
     ng new TeamsConfig
    
  9. import teams-js 並寫入設定(❗不加此設定,會無法顯示❗)

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    
        import { Component, OnInit } from '@angular/core';
    
        import * as microsoftTeams from '@microsoft/teams-js';//引入 teams-js
    
        @Component({
        selector: 'app-teams-config',
        templateUrl: './teams-config.component.html',
        styleUrls: ['./teams-config.component.scss']
        })
        export class TeamsConfigComponent implements OnInit {
    
        constructor() { }
    
        ngOnInit(): void {
            // Initialize the Microsoft Teams SDK
            microsoftTeams.initialize();
    
            microsoftTeams.settings.registerOnSaveHandler((saveEvent) => {
                const baseUrl = window.location.protocol + '//' + window.location.host + '/';
                microsoftTeams.settings.setSettings({
                    suggestedDisplayName: 'Tab Test',
                    entityId: 'index', // 與 manifast內的staticTabs底下entityId需相同
                    contentUrl: baseUrl,
                    websiteUrl: baseUrl,
                });
                saveEvent.notifySuccess();
            });
    
    
            microsoftTeams.settings.setValidityState(true);
          }
        }
    
  10. 編輯 teams-config.component.html

    https://user-images.githubusercontent.com/33840759/180786400-7f9a59d5-25a8-4982-86d9-1259e7b0c570.png
  11. 建立 Tab Component 並撰寫內容

    1
    
        ng new tab
    
    https://user-images.githubusercontent.com/33840759/180786400-7f9a59d5-25a8-4982-86d9-1259e7b0c570.png
  12. 修改 app-routing.module.tsapp.module.ts

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
    //app-routing.module.ts
    import { NgModule } from '@angular/core';
    import { RouterModule, Routes } from '@angular/router';
    import { TabComponent } from './components/tab/tab.component';
    import { TeamsConfigComponent } from './components/teams-config/teams-config.component';
    
    const routes: Routes = [
        { path: '', pathMatch: 'full', redirectTo: '/tab' },
        { path: 'tab', component: TabComponent },
        { path: 'config', component: TeamsConfigComponent },
        { path: '**', pathMatch: 'full', redirectTo: '/tab' }, // catch any unfound routes and redirect to home page
    ];
    
    @NgModule({
        imports: [RouterModule.forRoot(routes)],
        exports: [RouterModule],
    })
    export class AppRoutingModule {}
    
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    
    //app.module.ts
    import { NgModule } from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    
    import { AppRoutingModule } from './app-routing.module';
    import { AppComponent } from './app.component';
    import { TabComponent } from './components/tab/tab.component';
    import { TeamsConfigComponent } from './components/teams-config/teams-config.component';
    
    @NgModule({
        declarations: [AppComponent, TabComponent, TeamsConfigComponent],
        imports: [BrowserModule, AppRoutingModule],
        providers: [],
        bootstrap: [AppComponent],
    })
    export class AppModule {}
    
    

    修改完別忘了將 app.component.html也加上 <router-outlet></router-outlet>

  13. 建立 開發測試用途的自簽憑證 (Self-Signed Certificate), 並放入tabs資料夾的根目錄

    https://user-images.githubusercontent.com/33840759/180790126-6a3cc4e6-960f-4378-bc03-575451c707ed.png
  14. 修改 package.jsonscripts,將 ng serve修改為下列命令列

    1
    
        ng serve --ssl --ssl-key server.key --ssl-cert server.crt
    
    https://user-images.githubusercontent.com/33840759/180790641-375f0c3e-9bde-4e9f-a89a-b5f78f9f2fe2.png

    因Teams App要Tab顯示內容,包含的網頁必須是有 SSL ,所以要為Angular加入憑證

  15. F5執行

    • 執行時會看到 OUTPUT跑出下圖內容

      https://user-images.githubusercontent.com/33840759/180791651-71a5ef4a-f518-424a-b7b6-f59cbddd5f9d.png
    • compiler成功後就會自動開啟網頁版Teams

      https://user-images.githubusercontent.com/33840759/180792325-737075f3-d75c-47de-a4fa-c8014972ba5e.png
    • 因選擇Add to a teams(加入到團隊頻道), 故此處要選擇團隊頻道

      https://user-images.githubusercontent.com/33840759/180792638-9c763695-abfc-48c6-8307-6f40d80186e3.png
    • 此處顯示的是 teams-config.component.html的內容

      https://user-images.githubusercontent.com/33840759/180794247-e521c1cc-4141-474b-b69e-e8adc305318b.png
    • 點選 about 裡面顯示的是 manifast 上 的內容

      https://user-images.githubusercontent.com/33840759/180794518-825852a3-705b-4d4f-8de3-264bae189314.png
    • 加入安裝完後, 顯示的是 tab.component.html的內容

      https://user-images.githubusercontent.com/33840759/180794772-ec94b426-c7a5-4e78-87b9-7c37547139b1.png

可能出現的錯誤

如果有跳出有關 env 的錯誤訊息的話, 較有可能的就是 需要 env-cmdcross-env這類套件(通常沒用到是不會有錯拉)

還有就是要從 Teams Toolkit內設定

https://user-images.githubusercontent.com/33840759/180797830-74fe2060-1e97-475a-aae9-7134e5f18275.png

參考資料