lib

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 16, 2026 License: MIT Imports: 11 Imported by: 0

README

Teams API wrapper Lib

Go Reference License


High-level Go (Golang) library that simplifies interaction with Microsoft Graph API. Provides abstraction over operations related to Teams, Channels, and Chats, adding a layer of automatic caching and name resolution.

🚀 Key Features

  • Simplified Authentication: Built-in MSAL token support.
  • Intelligent Cache: Automatic mapping of team names to IDs (e.g., "DevOps Team" -> UUID), reducing API queries.
  • Facade Architecture: One main Client providing access to all services (Teams, Channels, Chats).
  • Type Safety: All operations return strongly typed models.

📦 Installation

go get https://github.com/pzsp-teams/lib

🛠️ Architecture & Concepts

The library uses a Facade Pattern. The Client struct aggregates domain-specific services:

  • client.Teams: Manage teams lifecycles and members.
  • client.Channels: Manage standard and private channels.
  • client.Chats: Handle messages and chat members.
The "Reference" concept

Many methods accept a _Ref argument. This allows you to pass:

  • UUID
  • Display Name (email address in case of UserRefs) - this provides convenient usage in interactive applications. Library will automatically resolve refs to IDs.

💻 Quick Start

Full example usage is showcased HERE Below is a simple example showing how to initialize the client and list the current user's teams.

1. Client initialization
import (
    "context"
    "time"
    "github.com/pzsp-teams/lib"
    "github.com/pzsp-teams/lib/config"
)

func main() {
    ctx := context.Background()

    // Auth config (Azure AD)
    authCfg := &config.AuthConfig{
        ClientID:   "your-client-id",
        Tenant:     "your-tenant-id",
        Email:      "your-email",
        Scopes:     []string{"[https://graph.microsoft.com/.default](https://graph.microsoft.com/.default)"},
        AuthMethod: "DEVICE_CODE", // Or "INTERACTIVE"
    }

    // Cache config
    cacheCfg := &config.CacheConfig{
        Mode:     config.CacheAsync,
        Provider: config.CacheProviderJSONFile, // Local file cache
    }

    // Client init
    client, err := lib.NewClient(ctx, authCfg, nil, cacheCfg)
    if err != nil {
        panic(err)
    }
    defer lib.Close() // Important if using cache
}
2. Example usage
// List joined teams
teams, _ := client.Teams.ListMyJoined(ctx)
for _, t := range teams {
    fmt.Printf("Team: %s (ID: %s)\n", t.DisplayName, t.ID)
}

// Create a new team
newTeam, _ := client.Teams.CreateViaGroup(ctx, "Project Alpha", "project-alpha", "public")

Authentication

The library uses config.AuthConfig to establish the connection. Ensure your Azure App Registration has the necessary API Permissions (e.g., Team.ReadBasic.All, Channel.ReadBasic.All) granted in the Azure Portal. Complete list of scopes required by all functions is available HERE

There are two available ways to authenticate:

  • INTERACTIVE - log in window will automatically be opened within your browser.
  • DEVICE CODE - library will provide you the URL and code, which need to be manually opened with browser of your choice.

Cache

If enabled, stores metadata and non-sensitive mappings (e.g., TeamRef -> UUID) to provide efficient reference resolution.


⚠️ Important:

Because the cache might run background goroutines to keep data fresh, you must call lib.Close() when your application shuts down. This ensures all background operations complete and prevents memory leaks or race conditions.

defer lib.Close() // Important: closes global cache/background workers

📚 Documentation

Full API reference, architecture details, and configuration guides are available here:

👉 Read the Documentation

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

Ports

This library is also available in Python

Documentation

Overview

Package lib acts as the primary entry point for the Microsoft Teams API client library. It adopts a Facade pattern, aggregating specialized services (Teams, Channels, Chats) into a single, cohesive Client.

The package manages the complexity of:

  • Authentication (via MSAL and Graph Token Providers).
  • Dependency Injection (wiring APIs, Caches, and Resolvers).
  • Caching strategies (transparently wrapping operations with caching layers).

Usage: Initialize the Client using NewClient for a standard setup. Alternatively, if you need only specific services, use:

  • NewTeamServiceFromGraphClient for Teams service.
  • NewChannelServiceFromGraphClient for Channels service.
  • NewChatServiceFromGraphClient for Chats service.

Always ensure to call Close() upon application shutdown to flush any background cache operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close()

Close ensures a graceful shutdown of the library. It waits for any pending background operations (such as asynchronous cache updates) to complete before returning, preventing data loss or race conditions.

func NewChannelServiceFromGraphClient

func NewChannelServiceFromGraphClient(ctx context.Context, authCfg *config.AuthConfig, senderCfg *config.SenderConfig, cacheCfg *config.CacheConfig) (channels.Service, error)

NewChannelServiceFromGraphClient creates a standalone service for Channel operations. Use this if you do not need the full Client wrapper and only want to interact with Channels.

func NewChatServiceFromGraphClient

func NewChatServiceFromGraphClient(ctx context.Context, authCfg *config.AuthConfig, senderCfg *config.SenderConfig, cacheCfg *config.CacheConfig) (chats.Service, error)

NewChatServiceFromGraphClient creates a standalone service for Chat operations. Use this if you do not need the full Client wrapper and only want to interact with Chats.

func NewTeamServiceFromGraphClient

func NewTeamServiceFromGraphClient(ctx context.Context, authCfg *config.AuthConfig, senderCfg *config.SenderConfig, cacheCfg *config.CacheConfig) (teams.Service, error)

NewTeamServiceFromGraphClient creates a standalone service for Team operations. Use this if you do not need the full Client wrapper and only want to interact with Teams.

Types

type Client

type Client struct {
	Channels channels.Service
	Teams    teams.Service
	Chats    chats.Service
}

Client is the central hub for interacting with the Microsoft Teams ecosystem. It aggregates access to specific domains: Channels, Teams, and Chats, hiding the complexity of underlying Graph API calls and caching mechanisms.

func NewClient

func NewClient(ctx context.Context, authCfg *config.AuthConfig, senderCfg *config.SenderConfig, cacheCfg *config.CacheConfig) (*Client, error)

NewClient initializes a new Client instance with fully configured internal services. It handles the authentication handshake using the provided authCfg and sets up sending and caching behaviors based on senderCfg and cacheCfg.

func NewClientFromGraphClient

func NewClientFromGraphClient(graphClient *graph.GraphServiceClient, senderCfg *config.SenderConfig, cacheCfg *config.CacheConfig) (*Client, error)

NewClientFromGraphClient creates a Client using an existing, pre-configured GraphServiceClient. This is a separated exported constructor mainly for external testing purposes (via mocking Teams API by injection of GraphServiceClient).

It wires up all internal dependencies, including API clients, caching layers, and entity resolvers (e.g., resolving team names to IDs).

Directories

Path Synopsis
Package channels provides various channel-related operations.
Package channels provides various channel-related operations.
Package chats provides various chat-related operations.
Package chats provides various chat-related operations.
Package config holds configuration structs used across the application.
Package config holds configuration structs used across the application.
internal
adapter
Package adapter contains mapping helpers that convert Microsoft Graph SDK models into the library's simplified internal models.
Package adapter contains mapping helpers that convert Microsoft Graph SDK models into the library's simplified internal models.
api
Package api provides interfaces and implementations for Microsoft Teams operations backed by Microsoft Graph.
Package api provides interfaces and implementations for Microsoft Teams operations backed by Microsoft Graph.
auth
Package auth provides an Azure AD (MSAL) token provider implementation backed by a persistent MSAL cache.
Package auth provides an Azure AD (MSAL) token provider implementation backed by a persistent MSAL cache.
cacher
Package cacher contains caching utilities for the library, including:
Package cacher contains caching utilities for the library, including:
mentions
Package mentions provides helpers for building, validating, and mapping Microsoft Teams @mentions.
Package mentions provides helpers for building, validating, and mapping Microsoft Teams @mentions.
pepper
Package pepper provides helpers for obtaining and persisting a secret "pepper" value.
Package pepper provides helpers for obtaining and persisting a secret "pepper" value.
resolver
Package resolver provides helpers for resolving user-facing references into Microsoft Graph resource IDs.
Package resolver provides helpers for resolving user-facing references into Microsoft Graph resource IDs.
sender
Package sender provides helpers for executing Microsoft Graph requests with retries and timeouts.
Package sender provides helpers for executing Microsoft Graph requests with retries and timeouts.
testutil
Package testutil is a generated GoMock package.
Package testutil is a generated GoMock package.
util
Package util contains shared helpers used across the project, including:
Package util contains shared helpers used across the project, including:
Package models contains simplified Microsoft Teams domain types used by this library.
Package models contains simplified Microsoft Teams domain types used by this library.
Package search provides types used to build message-search queries and to represent paginated search results returned from the messaging backend (e.g., Microsoft Graph).
Package search provides types used to build message-search queries and to represent paginated search results returned from the messaging backend (e.g., Microsoft Graph).
Package setup provides setup utilities for the application.
Package setup provides setup utilities for the application.
Package teams provides team-related operations and abstracts the underlying Microsoft Graph API calls.
Package teams provides team-related operations and abstracts the underlying Microsoft Graph API calls.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL