title: Send message back from Facebook webview to bot
body: <p>Had written bot on ms bot frameworks for Facebook Messenger which creates <a href="https://developers.facebook.com/docs/messenger-platform/send-api-reference/generic-template" rel="nofollow noreferrer">carousel</a> using <a href="https://github.com/Microsoft/BotBuilder-Samples/blob/master/Node/blog-customChannelData/app.js" rel="nofollow noreferrer">custom channel data attachment</a> with <code>web_url</code> which enables messenger extensions: <code>"messenger_extensions": true</code>. We have <a href="https://developers.facebook.com/docs/messenger-platform/webview/extensions" rel="nofollow noreferrer">Added Messenger Extensions</a> on a webview page but it is not clear how to send message <strong>with an attachment</strong> from this webview page back to messenger and therefore to bot framework. </p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  
  &lt;body&gt;
      &lt;style type="text/css"&gt;
          .button {
              background-color: #4CAF50;
              /* Green */
              border: none;
              color: white;
              padding: 15px 32px;
              text-align: center;
              text-decoration: none;
              display: inline-block;
              font-size: 16px;
              width: 50%;
              margin: 25%;
          }
      &lt;/style&gt;
  
      &lt;button type="button" class="button" onclick="sendMessage();"&gt;Complete Booking&lt;/button&gt;
  
      &lt;script type="text/javascript"&gt;
          function sendMessage() {
              alert('Booking completed. What to do now? How to send the message back to bot?')
              /// how to return? the facebook docs don't say anything
              /// you HAVE to make a server round trip.. https://stackoverflow.com/questions/43956045/messengerextensions-how-to-send-a-message-by-messenger-to-users
              return {
                  text: "HOTEL_SERVICE_PAYLOAD",
                  attachments: [
                      {
                          email: "some@email.com",
                          hotelName: "Hotel marriott",
                          confirmNumber: "1234567"
                      }
                  ]
              }
              MessengerExtensions.requestCloseBrowser(function success() {
  
              }, function error(err) {
  
              });
          }
  
          (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("Getting PSID")
                  alert("This is the user's psid " + psid);
              }, function error(err) {
                  alert("Messenger Extension Error: " + err);
              });
          };
      &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Have read tons of <a href="https://developers.facebook.com/docs/messenger-platform" rel="nofollow noreferrer">documentation</a> and <a href="https://imick.nl/how-to-handle-referrals-optins-postbacks-on-facebook-using-microsoft-bot-framework/" rel="nofollow noreferrer">blogs</a> including stackoverflow: <a href="https://stackoverflow.com/a/44536739/630169">https://stackoverflow.com/a/44536739/630169</a>.</p>
  
  <p>Is there simple example of JavaScript script embedded on a page? Thanks!</p>