Skip to content

Commit

Permalink
upgrade go version
Browse files Browse the repository at this point in the history
  • Loading branch information
piusalfred committed Aug 12, 2023
1 parent 35d7542 commit dbefb20
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 21 deletions.
Empty file added _examples/messages/messages.go
Empty file.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/piusalfred/whatsapp

go 1.20
go 1.21
47 changes: 27 additions & 20 deletions http/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"io"
"net/http"
"net/http/httputil"
"os"
"strings"
)

Expand All @@ -36,30 +35,38 @@ type DebugFunc func(io.Writer) Hook
// the request and response.
func DebugHook(writer io.Writer) Hook {
return func(ctx context.Context, req *http.Request, resp *http.Response) {
var buff strings.Builder
buff := &strings.Builder{}
name := strings.ToUpper(RequestNameFromContext(ctx))
buff.WriteString(name)
buff.WriteString("\n")
if req != nil {
b, err := httputil.DumpRequestOut(req, true)
if err == nil {
buff.Write(b)
buff.WriteString("\n")
}
}
buff.WriteString("\n\n")

if resp != nil {
b, err := httputil.DumpResponse(resp, true)
if err == nil {
buff.Write(b)
buff.WriteString("\n")
}
}
dumpRequest(buff, req)
dumpResponse(buff, resp)
//
//if writer == nil {
// _, _ = os.Stdout.Write(buff.Bytes())
//} else {
// _, _ = writer.Write(buff.Bytes())
//}
}
}

if writer == nil {
_, _ = os.Stdout.Write([]byte(buff.String()))
func dumpRequest(buff *strings.Builder, req *http.Request) {
if req != nil {
b, err := httputil.DumpRequestOut(req, true)
if err == nil {
buff.Write(b)
buff.WriteString("\n\n")
}
}
}

_, _ = writer.Write([]byte(buff.String()))
func dumpResponse(buff *strings.Builder, resp *http.Response) {
if resp != nil {
b, err := httputil.DumpResponse(resp, true)
if err == nil {
buff.Write(b)
buff.WriteString("\n\n")
}
}
}
5 changes: 5 additions & 0 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,8 @@ type ResponseError struct {
func (e *ResponseError) Error() string {
return fmt.Sprintf("whatsapp error: http code: %d, %s", e.Code, strings.ToLower(e.Err.Error()))
}

// Unwrap returns the underlying error for ResponseError.
func (e *ResponseError) Unwrap() error {
return e.Err
}
58 changes: 58 additions & 0 deletions templates/templates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Package templates provides structures and utilities for creating and manipulating WhatsApp message
templates.
WhatsApp message templates are specific message formats that businesses use to send out notifications
or customer care messages to people that have opted in to notifications. These notifications can include
a variety of messages such as appointment reminders, shipping information, issue resolution, or payment
updates.
Before using this package to send a template message, you must first create a template. For more information
on creating templates, refer to the guide titled "Create Message Templates for Your WhatsApp Business Account"
(https://developers.facebook.com/micro_site/url/?click_from_context_menu=true&country=TZ&destination=https%3A%2F%2Fwww.facebook.com%2Fbusiness%2Fhelp%2F2055875911147364&event_type=click&last_nav_impression_id=0jjgfFiSZMkMJ8TP8&max_percent_page_viewed=10&max_viewport_height_px=999&max_viewport_width_px=1414&orig_http_referrer=https%3A%2F%2Fdevelopers.facebook.com%2Fdocs%2Fwhatsapp%2Fcloud-api%2Fguides%2Fsend-message-templates&orig_request_uri=https%3A%2F%2Fdevelopers.facebook.com%2Fajax%2Fdocs%2Fnav%2F%3Fpath1%3Dwhatsapp%26path2%3Dcloud-api%26path3%3Dguides%26path4%3Dsend-message-templates&region=emea&scrolled=false&session_id=1Qi3IoIHE5nPFSkug&site=developers)
If your account is not yet verified, you can use one of the pre-approved templates provided.
This package supports the following template types:
- Text-based message templates
- Media-based message templates
- Interactive message templates
- Location-based message templates
- Authentication templates with one-time password buttons
- Multi-Product Message templates
All API calls made using this package must be authenticated with an access token. Developers can authenticate their API calls with the access token generated in the App Dashboard > WhatsApp > API Setup panel. Business Solution Providers (BSPs) need to authenticate themselves with an access token that has the 'whatsapp_business_messaging' permission.
*/
package templates


type Message struct {
MessagingProduct string `json:"messaging_product"`
RecipientType string `json:"recipient_type"`
To string `json:"to"`
Type string `json:"type"`
Template *Template `json:"template"`
}

type Template struct {
Name string `json:"name"`
Language Language `json:"language"`
Components []Component `json:"components"`
}

type Language struct {
Code string `json:"code"`
}

type Component struct {
Type string `json:"type"`
SubType string `json:"sub_type,omitempty"`
Index string `json:"index,omitempty"`
Parameters []Parameter `json:"parameters"`
}

type Parameter struct {
Type string `json:"type"`
Text string `json:"text"`
}

0 comments on commit dbefb20

Please sign in to comment.