github.com/grokify/go-ringcentral-client@v0.3.31/office/v1/examples/glip/glip_bot_update_name/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  	"os"
     8  
     9  	"github.com/caarlos0/env/v11"
    10  	"github.com/grokify/goauth/authutil"
    11  	"github.com/grokify/mogo/config"
    12  	"github.com/grokify/mogo/fmt/fmtutil"
    13  	"github.com/jessevdk/go-flags"
    14  
    15  	rc "github.com/grokify/go-ringcentral-client/office/v1/client"
    16  	ru "github.com/grokify/go-ringcentral-client/office/v1/util"
    17  )
    18  
    19  type Options struct {
    20  	FirstName string `short:"f" long:"firstname" description:"First Name" required:"true"`
    21  	LastName  string `short:"l" long:"lastname" description:"Last Name" required:"true"`
    22  }
    23  
    24  type RingCentralConfig struct {
    25  	TokenJSON string `env:"RINGCENTRAL_TOKEN_JSON"`
    26  	ServerURL string `env:"RINGCENTRAL_SERVER_URL"`
    27  }
    28  
    29  func Initialize() (*rc.APIClient, error) {
    30  	appCfg := RingCentralConfig{}
    31  	err := env.Parse(&appCfg)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	httpClient, err := authutil.NewClientTokenJSON(
    37  		context.Background(), []byte(appCfg.TokenJSON))
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	apiClient, err := ru.NewApiClientHttpClientBaseURL(
    43  		httpClient, appCfg.ServerURL)
    44  	return apiClient, err
    45  }
    46  
    47  // This code takes a bot token and creates a permanent webhook.
    48  func main() {
    49  	opts := &Options{}
    50  	_, err := flags.Parse(opts)
    51  	if err != nil {
    52  		log.Fatal(err)
    53  	}
    54  
    55  	_, err = config.LoadDotEnv([]string{os.Getenv("ENV_PATH"), "./.env"}, 1)
    56  	if err != nil {
    57  		log.Fatal(err)
    58  	}
    59  
    60  	apiClient, err := Initialize()
    61  	if err != nil {
    62  		log.Fatal(err)
    63  	}
    64  
    65  	req := rc.ExtensionUpdateRequest{
    66  		Contact: rc.ContactInfoUpdateRequest{
    67  			FirstName: opts.FirstName,
    68  			LastName:  opts.LastName}}
    69  
    70  	info, resp, err := apiClient.UserSettingsApi.UpdateExtension(
    71  		context.Background(), "~", "~", req)
    72  	if err != nil {
    73  		log.Fatal(err)
    74  	} else if resp.StatusCode >= 300 {
    75  		log.Fatal(fmt.Sprintf("API Status %v", resp.StatusCode))
    76  	}
    77  
    78  	fmt.Printf("Status: %v\n", resp.StatusCode)
    79  
    80  	fmtutil.PrintJSON(info)
    81  
    82  	fmt.Println("DONE")
    83  }