3

I'm sending the user from the messenger chat to a payment page in my application. Messenger opens the page in a webview. Now I'd like to close the webview and send the user back to Messenger while also sending something to the webhook in order to notify it that the user has finished the payment page.

What is the best way to accomplish this?

michaelr524
  • 871
  • 2
  • 11
  • 21

1 Answers1

1

You can only achieve this if the payment page is controlled(developed by you), if it is a third party payment gateway there is nothing you can do. if the payment page is controlled by you, you can pass the sender ID as a parameter via web_url or get the sender ID via

<script>
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.com/en_US/messenger.Extensions.js";
fjs.parentNode.insertBefore(js, fjs);
 }(document, "script", "Messenger"));

  window.extAsyncInit = function () {
  // the Messenger Extensions JS SDK is done loading
   MessengerExtensions.getUserID(function success(uids) {
    var psid = uids.psid;//This is your page scoped sender_id
    alert(psid);
}, function error(err) {
    alert("Messenger Extension Error: " + err);
});
};
</script>  

using the sender ID you can then send a message text back to the bot. to close the webview after all of this include this script after sending the text to the bot

   <script>
  (function(d, s, id){
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) {return;}
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.com/en_US/messenger.Extensions.js";
  fjs.parentNode.insertBefore(js, fjs);
  }(document, "script", "Messenger"));

  window.extAsyncInit = function () {
  // the Messenger Extensions JS SDK is done loading
  //close the webview
  MessengerExtensions.requestCloseBrowser(function success() {

  }, function error(err) {

  });

  };
  </script>

Just like from within your Bot you have to ensure that the page access token is available before you send the text, also ensure that you whitelist the domain used in you webview and you set "messenger_extensions": true, in your web_url button or you won't be able to get the Sender ID using messenger Extension

references

url button

messenger extension

  • Not clear how to send message back to bot: could You elaborate please, provide more detailed example with comments? – Aleksey Kontsevich Aug 25 '17 at 18:53
  • @AlekseyKontsevich just send it the same way you do from within the bot. Only make sure you have the page scoped ID, and page access token available, that is within your scope. – Onyemenam Ndubuisi Aug 26 '17 at 19:58
  • What do You mean? I'm using MS Bot Framework for the bot, think in FB it is different. – Aleksey Kontsevich Aug 26 '17 at 22:52
  • @AlekseyKontsevich i don't know how that works with MS Bot Framework – Onyemenam Ndubuisi Sep 03 '17 at 23:40
  • @AlekseyKontsevich are you still comfortable using MS Bot Framework ? :) – boldnik Sep 21 '17 at 10:15
  • @boldnik, yes quite comfortable, why not? Like it very much, powerful and convenient thing. – Aleksey Kontsevich Sep 22 '17 at 10:53
  • @AlekseyKontsevich is there any open article how do you manage dialogs? Within state machine definition or somehow else? – boldnik Sep 29 '17 at 14:01
  • @boldnik, yes: https://learn.microsoft.com/en-us/bot-framework/nodejs/bot-builder-nodejs-dialog-manage-conversation-flow and https://learn.microsoft.com/en-us/bot-framework/nodejs/bot-builder-nodejs-state – Aleksey Kontsevich Sep 30 '17 at 16:38
  • @AlekseyKontsevich yes, I know this one, but it's only a state storage. What I'm looking for is the dialog handling method which simplifies how to build a question-answer chaining. Do you hardcode it as a chained functions or somehow else? – boldnik Oct 02 '17 at 16:38
  • @boldnik, yes hardcoded for now with waterfall in Node.js, also action was used. In other project With C# loaded dialog flow from DB and coded all dialog logic and state myself. – Aleksey Kontsevich Oct 02 '17 at 21:04
  • @AlekseyKontsevich we used Koa server for similar (aka middleware) pattern, though I do not think it's the best one. – boldnik Oct 03 '17 at 15:28