Repository: Yoctol/bottender
Pull request #19 (2017-11-11 12:46:14)

export sendMessage, sendTemplate, sendOpenGraphTemplate, sendMediaTemplate on context
export following methods on context:

- sendMessage
- sendTemplate 
- sendOpenGraphTemplate
- sendMediaTemplate


Patch:
diff --git a/src/context/MessengerContext.js b/src/context/MessengerContext.js
index 949ee232..d743219e 100644
--- a/src/context/MessengerContext.js
+++ b/src/context/MessengerContext.js
@@ -163,15 +163,19 @@ class MessengerContext extends Context implements PlatformContext {
 
 const sendMethods = [
   // method name, arguments length
+  ['sendMessage', 3],
   ['sendAttachment', 3],
   ['sendImage', 3],
   ['sendAudio', 3],
   ['sendVideo', 3],
   ['sendFile', 3],
   ['sendQuickReplies', 4],
+  ['sendTemplate', 3],
   ['sendGenericTemplate', 3],
   ['sendButtonTemplate', 4],
   ['sendListTemplate', 4],
+  ['sendOpenGraphTemplate', 3],
+  ['sendMediaTemplate', 3],
   ['sendReceiptTemplate', 3],
   ['sendAirlineBoardingPassTemplate', 3],
   ['sendAirlineCheckinTemplate', 3],


Test patch:
diff --git a/src/context/__tests__/MessengerContext.spec.js b/src/context/__tests__/MessengerContext.spec.js
index 292a7449..819555f2 100644
--- a/src/context/__tests__/MessengerContext.spec.js
+++ b/src/context/__tests__/MessengerContext.spec.js
@@ -24,6 +24,7 @@ const createMockGraphAPIClient = () => ({
   typingOn: jest.fn(),
   typingOff: jest.fn(),
   markSeen: jest.fn(),
+  sendMessage: jest.fn(),
   sendText: jest.fn(),
   sendAttachment: jest.fn(),
   sendImage: jest.fn(),
@@ -31,9 +32,12 @@ const createMockGraphAPIClient = () => ({
   sendVideo: jest.fn(),
   sendFile: jest.fn(),
   sendQuickReplies: jest.fn(),
+  sendTemplate: jest.fn(),
   sendGenericTemplate: jest.fn(),
   sendButtonTemplate: jest.fn(),
   sendListTemplate: jest.fn(),
+  sendOpenGraphTemplate: jest.fn(),
+  sendMediaTemplate: jest.fn(),
   sendReceiptTemplate: jest.fn(),
   sendAirlineBoardingPassTemplate: jest.fn(),
   sendAirlineCheckinTemplate: jest.fn(),
@@ -101,6 +105,26 @@ it('get #client works', () => {
   expect(context.client).toBe(client);
 });
 
+describe('#sendMessage', () => {
+  it('should call client.sendMessage', async () => {
+    const { context, client, session } = setup();
+
+    await context.sendMessage({
+      text: 'Hello!',
+    });
+
+    expect(client.sendMessage).toBeCalledWith(
+      session.user.id,
+      {
+        text: 'Hello!',
+      },
+      {
+        messaging_type: 'RESPONSE',
+      }
+    );
+  });
+});
+
 describe('#sendText', () => {
   it('should call client.sendText', async () => {
     const { context, client, session } = setup();
@@ -306,6 +330,42 @@ describe('#sendQuickReplies', () => {
   });
 });
 
+describe('#sendTemplate', () => {
+  it('should call client.sendTemplate', async () => {
+    const { context, client, session } = setup();
+
+    await context.sendTemplate({
+      template_type: 'button',
+      text: 'title',
+      buttons: [
+        {
+          type: 'postback',
+          title: 'Start Chatting',
+          payload: 'USER_DEFINED_PAYLOAD',
+        },
+      ],
+    });
+
+    expect(client.sendTemplate).toBeCalledWith(
+      session.user.id,
+      {
+        template_type: 'button',
+        text: 'title',
+        buttons: [
+          {
+            type: 'postback',
+            title: 'Start Chatting',
+            payload: 'USER_DEFINED_PAYLOAD',
+          },
+        ],
+      },
+      {
+        messaging_type: 'RESPONSE',
+      }
+    );
+  });
+});
+
 describe('#sendGenericTemplate', () => {
   it('should call client.sendGenericTemplate', async () => {
     const { context, client, session } = setup();
@@ -433,6 +493,84 @@ describe('#sendListTemplate', () => {
   });
 });
 
+describe('#sendOpenGraphTemplate', () => {
+  it('should call client.sendOpenGraphTemplate', async () => {
+    const { context, client, session } = setup();
+
+    await context.sendOpenGraphTemplate([
+      {
+        media_type: 'image',
+        attachment_id: '1854626884821032',
+        buttons: [
+          {
+            type: 'web_url',
+            url: 'https://en.wikipedia.org/wiki/Rickrolling',
+            title: 'View Website',
+          },
+        ],
+      },
+    ]);
+
+    expect(client.sendOpenGraphTemplate).toBeCalledWith(
+      session.user.id,
+      [
+        {
+          media_type: 'image',
+          attachment_id: '1854626884821032',
+          buttons: [
+            {
+              type: 'web_url',
+              url: 'https://en.wikipedia.org/wiki/Rickrolling',
+              title: 'View Website',
+            },
+          ],
+        },
+      ],
+      {
+        messaging_type: 'RESPONSE',
+      }
+    );
+  });
+});
+
+describe('#sendMediaTemplate', () => {
+  it('should call client.sendMediaTemplate', async () => {
+    const { context, client, session } = setup();
+
+    await context.sendMediaTemplate([
+      {
+        url: 'https://open.spotify.com/track/7GhIk7Il098yCjg4BQjzvb',
+        buttons: [
+          {
+            type: 'web_url',
+            url: 'https://en.wikipedia.org/wiki/Rickrolling',
+            title: 'View More',
+          },
+        ],
+      },
+    ]);
+
+    expect(client.sendMediaTemplate).toBeCalledWith(
+      session.user.id,
+      [
+        {
+          url: 'https://open.spotify.com/track/7GhIk7Il098yCjg4BQjzvb',
+          buttons: [
+            {
+              type: 'web_url',
+              url: 'https://en.wikipedia.org/wiki/Rickrolling',
+              title: 'View More',
+            },
+          ],
+        },
+      ],
+      {
+        messaging_type: 'RESPONSE',
+      }
+    );
+  });
+});
+
 describe('#sendReceiptTemplate', () => {
   it('should call client.sendReceiptTemplate', async () => {
     const { context, client, session } = setup();