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

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  	"net/http"
     8  	"os"
     9  
    10  	"github.com/antihax/optional"
    11  	"github.com/grokify/goauth"
    12  	"github.com/grokify/mogo/config"
    13  	"github.com/grokify/mogo/fmt/fmtutil"
    14  	"github.com/grokify/mogo/net/http/httputilmore"
    15  	"github.com/grokify/mogo/net/urlutil"
    16  	"github.com/jessevdk/go-flags"
    17  
    18  	rc "github.com/grokify/go-ringcentral-client/office/v1/client"
    19  	ru "github.com/grokify/go-ringcentral-client/office/v1/util"
    20  )
    21  
    22  type CliOptions struct {
    23  	EnvFile       string   `short:"e" long:"env" description:"Env filepath"`
    24  	To            []string `short:"t" long:"to" description:"Recipients"`
    25  	Files         []string `short:"f" long:"file" description:"Files to send"`
    26  	CoverPageText string   `short:"c" long:"coverpagetext" description:"Cover Page Text"`
    27  }
    28  
    29  func sendFaxRaw(opts CliOptions, httpClient *http.Client) {
    30  	fax := ru.FaxRequest{
    31  		To:            opts.To,
    32  		CoverPageText: opts.CoverPageText,
    33  		Resolution:    "High",
    34  		FilePaths:     opts.Files,
    35  	}
    36  
    37  	url := urlutil.JoinAbsolute(os.Getenv("RINGCENTRAL_SERVER_URL"), "/restapi/v1.0/account/~/extension/~/fax")
    38  
    39  	resp, err := fax.Post(httpClient, url)
    40  	if err != nil {
    41  		panic(err)
    42  	}
    43  	err = httputilmore.PrintResponse(resp, true)
    44  	if err != nil {
    45  		panic(err)
    46  	}
    47  }
    48  
    49  // example: $ go run fax_send.go -to=+16505550100 -file=$GOPATH/src/github.com/grokify/go-ringcentral-client/office/v1/examples/fax_send/test_file.pdf
    50  func main() {
    51  	opts := CliOptions{}
    52  	_, err := flags.Parse(&opts)
    53  	if err != nil {
    54  		log.Fatal(err)
    55  	}
    56  	_, err = config.LoadDotEnv([]string{opts.EnvFile, os.Getenv("ENV_PATH"), "./.env"}, 1)
    57  	if err != nil {
    58  		log.Fatal(err)
    59  	}
    60  	fmtutil.PrintJSON(opts)
    61  	/*
    62  			rcCfg, err := NewRingCentralConfigEnv()
    63  			if err != nil {
    64  				log.Fatal(err)
    65  			}
    66  		fmtutil.PrintJSON(rcCfg)
    67  	*/
    68  
    69  	apiClient, err := ru.NewApiClientPassword(
    70  		goauth.NewCredentialsOAuth2Env("RINGCENTRAL_"))
    71  	if err != nil {
    72  		log.Fatal(err)
    73  	}
    74  
    75  	httpClient := apiClient.HTTPClient()
    76  
    77  	if 1 == 0 {
    78  		sendFaxRaw(opts, httpClient)
    79  	}
    80  
    81  	if 1 == 1 {
    82  		fmt.Println(opts.Files[0])
    83  
    84  		file, err := os.Open(opts.Files[0])
    85  		if err != nil {
    86  			log.Fatal(err)
    87  		}
    88  
    89  		params := rc.SendFaxMessageOpts{}
    90  		if len(opts.CoverPageText) > 0 {
    91  			//params.FaxResolution = optional.NewString("High")
    92  			params.CoverPageText = optional.NewString(opts.CoverPageText)
    93  		}
    94  		fmtutil.PrintJSON(opts)
    95  		if 1 == 1 {
    96  			params.Attachment = optional.NewInterface(file)
    97  		}
    98  		info, resp, err := apiClient.MessagesApi.SendFaxMessage(
    99  			context.Background(),
   100  			"~",
   101  			"~",
   102  			opts.To,
   103  			&params)
   104  
   105  		if err != nil {
   106  			panic(err)
   107  		} else if resp.StatusCode > 299 {
   108  			panic(fmt.Errorf("API Status %v", resp.StatusCode))
   109  		}
   110  		fmtutil.PrintJSON(info)
   111  	}
   112  
   113  	fmt.Println("DONE")
   114  }