Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Latest commit

 

History

History
172 lines (119 loc) · 5.72 KB

TUTORIAL.md

File metadata and controls

172 lines (119 loc) · 5.72 KB

Tutorial

This 4-part tutorial should help you get started on using the library quickly.

Steps

(1) Setting up new app

  • Go to developers.facebook.com and log in.

  • Click on My Apps:
    My Apps

  • Click on Create app:
    My Apps

  • Select app type as Business:
    My Apps

  • Fill in basic info by inputting the display name & email address, and then click Create app:
    My Apps

  • On the app page, select whatsapp and click Set up:
    My Apps

  • Select a business account or create one if you don't have. Click Continue to finish setting up:
    My Apps

  • On the Getting Started page, copy the token and number ID as we will need them later:
    My Apps

(2) Setting up local project

  • Start a new project on your local machine:

    # create a new folder
    mkdir hello-whatsapp
    cd hello-whatsapp
    
    # initialize npm & install the library
    npm init -y
    npm i whatsapp-cloud-api
    
    # create index.js file
    mkdir src
    touch src/index.js
  • Update src/index.js to look as follows:

    const { createBot } = require('whatsapp-cloud-api');
    
    (async () => {
      try {
        // replace the values below from the values you copied above
        const from = 'YOUR_WHATSAPP_PHONE_NUMBER_ID';
        const token = 'YOUR_TEMPORARY_OR_PERMANENT_ACCESS_TOKEN';
        const to = 'PHONE_NUMBER_OF_RECIPIENT'; // your phone number without the leading '+'
        const webhookVerifyToken = 'YOUR_WEBHOOK_VERIFICATION_TOKEN'; // use a random value, e.g. 'bju#hfre@iu!e87328eiekjnfw'
    
        const bot = createBot(from, token);
    
        const result = await bot.sendText(to, 'Hello world');
    
        // Start express server to listen for incoming messages
        await bot.startExpressServer({
          webhookVerifyToken,
        });
    
        // Listen to ALL incoming messages
        bot.on('message', async (msg) => {
          console.log(msg);
    
          if (msg.type === 'text') {
            await bot.sendText(msg.from, 'Received your text message!');
          } else if (msg.type === 'image') {
            await bot.sendText(msg.from, 'Received your image!');
          }
        });
      } catch (err) {
        console.log(err);
      }
    })();
  • Update package.json to look as follows:

    {
      ...
      "scripts": {
        "start": "node ./src/index.js"
      },
      ...
    }
    
  • Start the app:

    npm start

Check your whatsapp app to ensure that you have received a text message (Hello world).

Congratulations, you have just sent your first message via the whatsapp API! 🎉

(3) Setting up ngrok

By default, the endpoint for whatsapp-related requests will be: /webhook/whatsapp. This means that locally, your URL will be: http://localhost/webhook/whatsapp.

You can use a reverse proxy to make the server publicly available. An example of this is ngrok. For the purposes of this tutorial, we will use ngrok as our reverse proxy:

  • Download ngrok: https://ngrok.com/download.
  • Follow the instuctions to set it up.
  • Run it: ngrok http 3000. You should get a public URL, e.g. https://1234.ngrok.io.
  • Go to configuration and click on Edit webhook:
    My Apps
  • Use the url received from ngrok: https://1234.ngrok.io/webhook/whatsapp and the verification token you provided above in the index.js file: const webhookVerifyToken = 'YOUR_WEBHOOK_VERIFICATION_TOKEN';.
  • Finally, hit the Verify and save button to verify and save the webhook:
    My Apps
  • Ensure you see following on the terminal:
    My Apps
  • Also ensure that the dialog for inputting the Callback URL/Verify token on the Facebook dashboard closes successfully (which means there is no error).

Congratulations, you have just verified your webhook! 🎉

(4) Receiving messages

  • On the dashboard under Configuration, click on Manage under Webhook fields to add a subscription:
    My Apps

  • Click Subscribe next to the messages webhook to subscribe to new messages:
    My Apps

  • Send a message from your whatsapp app on your device. Check your terminal to ensure you get a response as below:
    My Apps

Congratulations, you have just received your first message via the whatsapp API! 🎉

Next steps

Feel free to customize the index.js file and play around with the functions as you wish.

You can also use the API Reference to view definitions of functions and interfaces.

Tips & Tricks

1. Creating long-lived tokens

By default, the access token expires after 24 hours. You can extend this to 2 months.

This should now extend the token by 2 months: 🎉

My Apps

Issues?

Feel free to create an issue if you experience any issue.

Resources