Skip to content

Commit

Permalink
feat: add http backend integration example
Browse files Browse the repository at this point in the history
Signed-off-by: sarthakjdev <[email protected]>
  • Loading branch information
sarthakjdev committed Jun 2, 2024
1 parent aedd569 commit 970f72d
Show file tree
Hide file tree
Showing 19 changed files with 273 additions and 195 deletions.
154 changes: 0 additions & 154 deletions example-chat-bot/main.go

This file was deleted.

181 changes: 181 additions & 0 deletions examples/chat-bot/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package main

import (
"fmt"
"strings"

"github.com/sarthakjdev/wapi.go/internal/manager"
wapi "github.com/sarthakjdev/wapi.go/pkg/client"
wapiComponents "github.com/sarthakjdev/wapi.go/pkg/components"
"github.com/sarthakjdev/wapi.go/pkg/events"
)

func main() {
// creating a client
whatsappClient, err := wapi.New(wapi.ClientConfig{
PhoneNumberId: "",
ApiAccessToken: "",
BusinessAccountId: "",
WebhookPath: "/webhook",
WebhookSecret: "1234567890",
WebhookServerPort: 8080,
})

if err != nil {
fmt.Println("error creating client", err)
return
}

// create a message
textMessage, err := wapiComponents.NewTextMessage(wapiComponents.TextMessageConfigs{
Text: "Hello, from wapi.go",
})

if err != nil {
fmt.Println("error creating text message", err)
return
}

audioMessage, err := wapiComponents.NewAudioMessage(wapiComponents.AudioMessageConfigs{
Link: "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3",
})

if err != nil {
fmt.Println("error creating audio message message", err)
return
}

videoMessage, err := wapiComponents.NewVideoMessage(wapiComponents.VideoMessageConfigs{
Link: "https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4",
})

if err != nil {
fmt.Println("error creating video message", err)
return
}

imageMessage, err := wapiComponents.NewImageMessage(wapiComponents.ImageMessageConfigs{
Link: "https://www.w3schools.com/w3css/img_lights.jpg",
})

if err != nil {
fmt.Println("error creating image message", err)
return
}

contactMessage, err := wapiComponents.NewContactMessage([]wapiComponents.Contact{
*wapiComponents.NewContact(wapiComponents.ContactName{
FormattedName: "Sarthak Jain",
FirstName: "Sarthak",
})})

if err != nil {
fmt.Println("error creating contact message", err)
return
}

locationMessage, err := wapiComponents.NewLocationMessage(28.7041, 77.1025)

if err != nil {
fmt.Println("error creating location message", err)
return
}

listMessage, err := wapiComponents.NewListMessage(wapiComponents.ListMessageParams{
ButtonText: "Button 1",
BodyText: "Body 1",
})

if err != nil {
fmt.Println("error creating list message", err)
return
}

listSectionRow, err := wapiComponents.NewListSectionRow("1", "Title 1", "Description 1")

if err != nil {
fmt.Println("error creating list section row", err)
return
}

listSection, err := wapiComponents.NewListSection("Section1")

if err != nil {
fmt.Println("error creating list section row", err)
return
}

listSection.AddRow(listSectionRow)
listMessage.AddSection(listSection)
jsonData, err := listMessage.ToJson(wapiComponents.ApiCompatibleJsonConverterConfigs{SendToPhoneNumber: "919643500545"})

if err != nil {
fmt.Println("error converting message to json", err)
return
}

fmt.Println(string(jsonData))

whatsappClient.Message.Send(manager.SendMessageParams{Message: listMessage, PhoneNumber: "919643500545"})

buttonMessage, err := wapiComponents.NewQuickReplyButtonMessage("Body 1")

if err != nil {
fmt.Println("error creating button message", err)
return
}

buttonMessage.AddButton("1", "Button 1")
buttonMessage.AddButton("2", "Button 2")

whatsappClient.On(manager.ReadyEvent, func(event events.BaseEvent) {
fmt.Println("client is ready")
})

whatsappClient.On(manager.TextMessageEvent, func(event events.BaseEvent) {
fmt.Println("text message event received")

textMessageEvent := event.(*events.TextMessageEvent)

switch strings.ToLower(textMessageEvent.Text) {
case "text":
textMessageEvent.Reply(textMessage)
case "audio":
textMessageEvent.Reply(audioMessage)
case "image":
textMessageEvent.Reply(imageMessage)
case "video":
textMessageEvent.Reply(videoMessage)
case "contact":
textMessageEvent.Reply(contactMessage)
case "location":
textMessageEvent.Reply(locationMessage)
case "reaction":
textMessageEvent.React("😍")
case "list":
textMessageEvent.Reply(listMessage)
case "button":
textMessageEvent.Reply(buttonMessage)
default:
textMessageEvent.Reply(textMessage)
}
})

whatsappClient.On(manager.AudioMessageEvent, func(be events.BaseEvent) {
fmt.Println("audio message event received")
})

whatsappClient.On(manager.VideoMessageEvent, func(be events.BaseEvent) {
fmt.Println("video message event received")
})

whatsappClient.On(manager.DocumentMessageEvent, func(be events.BaseEvent) {
fmt.Println("document message event received")
})

whatsappClient.On(manager.ImageMessageEvent, func(be events.BaseEvent) {
fmt.Println("image message event received")
})

whatsappClient.InitiateClient()
}
51 changes: 51 additions & 0 deletions examples/http-backend-integration/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"fmt"

"github.com/labstack/echo/v4"
"github.com/sarthakjdev/wapi.go/internal/manager"
wapi "github.com/sarthakjdev/wapi.go/pkg/client"
"github.com/sarthakjdev/wapi.go/pkg/components"
"github.com/sarthakjdev/wapi.go/pkg/events"
)

func main() {

whatsappClient, err := wapi.New(wapi.ClientConfig{
PhoneNumberId: "",
ApiAccessToken: "",
BusinessAccountId: "",
WebhookPath: "/webhook",
WebhookSecret: "1234567890",
WebhookServerPort: 8080,
})

whatsappClient.On(manager.TextMessageEvent, func(event events.BaseEvent) {
textMessageEvent := event.(*events.TextMessageEvent)
reply, err := components.NewTextMessage(components.TextMessageConfigs{
Text: "Hello, from wapi.go",
})
if err != nil {
fmt.Println("error creating text message", err)
return
}
textMessageEvent.Reply(reply)
})

getHandler := whatsappClient.GetWebhookGetRequestHandler()
postHandler := whatsappClient.GetWebhookPostRequestHandler()

if err != nil {
fmt.Println("error creating client", err)
return
}

server := echo.New()

server.GET("/webhook", getHandler)
server.POST("/webhook", postHandler)

server.Start(":8080")

}
Loading

0 comments on commit 970f72d

Please sign in to comment.