Skip to content
This repository has been archived by the owner on Jan 9, 2025. It is now read-only.

Commit

Permalink
feat: add WhatsApp component (#226)
Browse files Browse the repository at this point in the history
Because

- We want to integrate WhatsApp component to VDP

This commit

TODO:
- [x] Send Text-Based Template Message
- [x] Send Media-Based Template Message
- [x] Send Location-Based Template Message
- [x] Send Authentication Template Message

For more information about sending template message, please go to:
https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-message-templates
)

- [x] Send Text Message
- [x] Send Media Message
- [x] Send Location Message
- [x] Send Contact Message
- [x] Send Interactive Call-to-Action URL Button Message
(https://developers.facebook.com/docs/whatsapp/cloud-api/messages/interactive-cta-url-messages)

For more information about sending messages, please go to:
https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-messages
- I don't implement all the possible type of messages because 
1. Too complex and make the UI very clustered
2. I don't think those messages are frequently used.

- I initially wanted to make send catalog template message task.
However, sending catalog template requires a real phone number which is
not connected to any other WhatsApp account. For now, I will be skipping
send catalog template task since I don't have the necessary phone number
to test it.
- There is no read message task because in order to read/receive
message, need to use webhook.

Here is a document to explain more about the component

https://docs.google.com/document/d/1mXM7GqRiTJAI-eRth5nEXpah5ADxuFmm3pz3m8Ua8ys/edit?usp=sharing

---------

Co-authored-by: ChunHao <64747455+chuang8511@users.noreply.github.com>
  • Loading branch information
AmeliaCelline and chuang8511 authored Aug 9, 2024
1 parent ced4c62 commit 28d0de8
Show file tree
Hide file tree
Showing 12 changed files with 2,959 additions and 0 deletions.
305 changes: 305 additions & 0 deletions application/whatsapp/v0/README.mdx

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions application/whatsapp/v0/assets/WhatsApp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions application/whatsapp/v0/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package whatsapp

import (
"github.com/instill-ai/component/internal/util/httpclient"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/structpb"
)

func newClient(setup *structpb.Struct, logger *zap.Logger) *WhatsAppClient {
c := httpclient.New("WhatsApp", basePath+"/"+version,
httpclient.WithLogger(logger),
httpclient.WithEndUserError(new(errBody)),
)

c.SetAuthToken(getToken(setup))

w := &WhatsAppClient{httpclient: c}

return w
}

type errBody struct {
Error struct {
Message string `json:"message"`
} `json:"error"`
}

func (e errBody) Message() string {
return e.Error.Message
}

func getToken(setup *structpb.Struct) string {
return setup.GetFields()["token"].GetStringValue()
}

type WhatsAppClient struct {
httpclient *httpclient.Client
}

// api functions

type WhatsAppInterface interface {
SendMessageAPI(req interface{}, res interface{}, PhoneNumberID string) error
}

func (c *WhatsAppClient) SendMessageAPI(req interface{}, resp interface{}, PhoneNumberID string) error {
httpReq := c.httpclient.R().SetBody(req).SetResult(resp)
if _, err := httpReq.Post("/" + PhoneNumberID + "/messages"); err != nil {
return err
}
return nil
}
27 changes: 27 additions & 0 deletions application/whatsapp/v0/config/definition.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"availableTasks": [
"TASK_SEND_TEXT_BASED_TEMPLATE_MESSAGE",
"TASK_SEND_MEDIA_BASED_TEMPLATE_MESSAGE",
"TASK_SEND_LOCATION_BASED_TEMPLATE_MESSAGE",
"TASK_SEND_AUTHENTICATION_TEMPLATE_MESSAGE",
"TASK_SEND_TEXT_MESSAGE",
"TASK_SEND_MEDIA_MESSAGE",
"TASK_SEND_LOCATION_MESSAGE",
"TASK_SEND_CONTACT_MESSAGE",
"TASK_SEND_INTERACTIVE_CALL_TO_ACTION_URL_BUTTON_MESSAGE"
],
"documentationUrl": "https://www.instill.tech/docs/component/application/whatsapp",
"icon": "assets/WhatsApp.svg",
"id": "whatsapp",
"public": true,
"title": "WhatsApp",
"description": "Use WhatsApp Business Platform API to send template and messages",
"tombstone": false,
"type": "COMPONENT_TYPE_APPLICATION",
"uid": "028c0ce7-94ff-4ac2-9324-d9a362724eed",
"vendor": "WhatsApp",
"vendorAttributes": {},
"version": "0.1.0",
"sourceUrl": "https://github.com/instill-ai/component/blob/main/application/whatsapp/v0",
"releaseStage": "RELEASE_STAGE_ALPHA"
}
27 changes: 27 additions & 0 deletions application/whatsapp/v0/config/setup.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"token": {
"description": "Fill in your WhatsApp access token. Go here for more information: https://developers.facebook.com/docs/whatsapp/cloud-api/get-started",
"instillUpstreamTypes": [
"reference"
],
"instillAcceptFormats": [
"string"
],
"instillSecret": true,
"instillUIOrder": 0,
"title": "Token",
"type": "string"
}
},
"required": [
"token"
],
"instillEditOnNodeFields": [
"token"
],
"title": "WhatsApp Connection",
"type": "object"
}
Loading

0 comments on commit 28d0de8

Please sign in to comment.