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

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/antihax/optional"
    10  	"github.com/grokify/goauth"
    11  	"github.com/grokify/mogo/fmt/fmtutil"
    12  	"github.com/grokify/mogo/net/http/httputilmore"
    13  	"github.com/jessevdk/go-flags"
    14  	"github.com/rs/zerolog/log"
    15  
    16  	rc "github.com/grokify/go-ringcentral-client/office/v1/client"
    17  	ru "github.com/grokify/go-ringcentral-client/office/v1/util"
    18  	"github.com/grokify/goauth/ringcentral"
    19  )
    20  
    21  type CliOptions struct {
    22  	CredsPath     string   `short:"c" long:"credspath" description:"Environment File Path"`
    23  	Account       string   `short:"a" long:"account" description:"Environment Variable Name"`
    24  	To            []string `short:"t" long:"to" description:"Recipients" required:"true"`
    25  	Files         []string `short:"f" long:"file" description:"Files to send"`
    26  	CoverPageText string   `short:"p" long:"coverpagetext" description:"Cover Page Text"`
    27  }
    28  
    29  func main() {
    30  	opts := CliOptions{}
    31  	_, err := flags.Parse(&opts)
    32  	if err != nil {
    33  		log.Fatal().Err(err)
    34  		panic(err)
    35  	}
    36  	fmtutil.PrintJSON(opts)
    37  
    38  	cset, err := goauth.ReadFileCredentialsSet(opts.CredsPath, true)
    39  	if err != nil {
    40  		log.Fatal().Err(err).
    41  			Str("credentials_filepath", opts.CredsPath).
    42  			Msg("cannot read credentials file")
    43  	}
    44  
    45  	creds, err := cset.Get(opts.Account)
    46  	if err != nil {
    47  		log.Fatal().Err(err).
    48  			Str("available_accounts", strings.Join(cset.Accounts(), ", ")).
    49  			Str("credentials_account", opts.Account).
    50  			Msg("cannot find credentials account")
    51  	}
    52  
    53  	httpClient, err := creds.NewClient(context.Background())
    54  	if err != nil {
    55  		log.Fatal().Err(err)
    56  	}
    57  
    58  	cu := ringcentral.ClientUtil{
    59  		Client:    httpClient,
    60  		ServerURL: creds.OAuth2.ServerURL}
    61  
    62  	usr, err := cu.GetSCIMUser()
    63  	if err != nil {
    64  		log.Fatal().Err(err)
    65  	}
    66  	fmtutil.PrintJSON(usr)
    67  
    68  	demoData := ru.FaxRequest{
    69  		To:            opts.To,
    70  		CoverPageText: "Hello World Go",
    71  		Resolution:    "High",
    72  		FilePaths:     []string{"test_file.pdf"}}
    73  
    74  	if 1 == 0 {
    75  		resp, err := demoData.Post(httpClient, creds.OAuth2.ServerURL)
    76  		if err != nil {
    77  			log.Fatal().Err(err)
    78  		}
    79  		err = httputilmore.PrintResponse(resp, true)
    80  		if err != nil {
    81  			log.Fatal().Err(err)
    82  		}
    83  	}
    84  
    85  	if 1 == 1 {
    86  		apiClient, err := ru.NewApiClientHttpClientBaseURL(httpClient, creds.OAuth2.ServerURL)
    87  		if err != nil {
    88  			log.Fatal().Err(err)
    89  		}
    90  		fmt.Println(demoData.FilePaths[0])
    91  
    92  		file, err := os.Open(demoData.FilePaths[0])
    93  		if err != nil {
    94  			log.Fatal().Err(err)
    95  		}
    96  
    97  		params := rc.SendFaxMessageOpts{}
    98  		if len(demoData.CoverPageText) > 0 {
    99  			//params.FaxResolution = optional.NewString("High")
   100  			params.CoverPageText = optional.NewString(demoData.CoverPageText)
   101  		}
   102  		if 1 == 1 {
   103  			params.Attachment = optional.NewInterface(file)
   104  		}
   105  		info, resp, err := apiClient.MessagesApi.SendFaxMessage(
   106  			context.Background(),
   107  			"~",
   108  			"~",
   109  			demoData.To,
   110  			&params)
   111  
   112  		if err != nil {
   113  			log.Fatal().Err(err)
   114  		} else if resp.StatusCode > 299 {
   115  			panic(fmt.Errorf("API Status %v", resp.StatusCode))
   116  		}
   117  		fmtutil.PrintJSON(info)
   118  	}
   119  
   120  	fmt.Println("DONE")
   121  }