Facebook Send-To-Messenger事件监听器

时间:2016-09-02 02:40:18

标签: javascript facebook facebook-messenger

Facebook推出了一些非常酷的东西,我一直试图弄清楚如何让用户点击和选择加入页面重定向。现在,我对java没有任何好处,php是我的事。

参考:https://developers.facebook.com/docs/messenger-platform/plugin-reference/send-to-messenger#event

工作代码:

library(stringi)
library(tibble)
library(raster)
library(ggplot2)
library(ggthemes)
library(ggrepel)
library(RColorBrewer)
library(data.table)

# Get some data. Credit to hmbrmstr for a few lines in the following code.

mylist <- c("http://weather.unisys.com/hurricane/atlantic/2007H/BARRY/track.dat",
            "http://weather.unisys.com/hurricane/atlantic/2007H/TEN/track.dat",
            "http://weather.unisys.com/hurricane/atlantic/2006H/ERNESTO/track.dat",
            "http://weather.unisys.com/hurricane/atlantic/2006H/ALBERTO/track.dat")

temp <- rbindlist(
            lapply(mylist, function(x){
                foo <- readLines(x)
                foo <- read.table(textConnection(gsub("TROPICAL ", "TROPICAL_",
                                  foo[3:length(foo)])), 
                                  header=TRUE, stringsAsFactors=FALSE)

                year <- stri_extract_first(str = x, regex = "[0-9]+")
                name <- stri_extract_first(str = x, regex = "[A-Z]{2,}")

                cbind(foo, year, name)

            }
        ))


### Add a fake row for 2017
temp <- temp %>%
        add_row(ADV = NA, LAT = NA, LON = NA, TIME = NA, WIND = NA,
                PR = NA, STAT = NA, year = 2017, name = NA)

### Prepare a map
usa <- getData('GADM', country = "usa", level = 1)
mymap <- subset(usa, NAME_1 %in% c("Florida", "Arkansas", "Louisiana",
                                   "Alabama", "Georgia", "Tennessee",
                                   "Mississippi",
                                   "North Carolina", "South Carolina"))
mymap <- fortify(mymap)


# Create a data.table for labeling hurricanes later.
label <- temp[, .SD[1], by = name][complete.cases(name)]

g <- ggplot() +
     geom_map(data = mymap, map = mymap,
              aes(x = long, y = lat, group = group, map_id = id),
                  color = "black", size = 0.2, fill = "white") +
    geom_path(data = temp, aes(x = LON, y = LAT, group = name, color = WIND), size = 1) +
    scale_color_gradientn(colours = rev(brewer.pal(5, "Spectral")), name = "Wind (mph)") +
    facet_wrap(~ year) +
    coord_map() +
    theme_map() +
    geom_text_repel(data = label,
                    aes(x = LON, y = LAT, label = name),
                    size = 2, 
                    force = 1,
                    max.iter = 2e3,
                    nudge_x = 1,
                    nudge_y = -1) +
    theme(legend.position = "right") 

这将启动console.log和重定向,但我想做的是当用户点击按钮并使用send_to_messener按钮触发选择时执行某些操作。

所以我试过

FB.Event.subscribe('send_to_messenger', function(e) {
// callback for events triggered by the plugin
console.log('inside the send_to_messenger');  
window.top.location = 'http://google.com';
});

---也--- ---

FB.Event.subscribe('clicked', function(e) {
// callback for events triggered by the plugin
console.log('user clicked button');  
window.top.location = 'http://google.com';
});

同样,没有任何效果 - 如果某人有一个可以指向正确方向的线索,我会很感激..谢谢!!

2 个答案:

答案 0 :(得分:0)

再看一遍后,我想出来了。

if (response.is_after_optin == true ){ // do something }

答案 1 :(得分:0)

//完整的工作示例:           

    window.fbAsyncInit = function() {
      FB.init({
        appId: "app_id",
        xfbml: true,
    status: true, 
    cookie: true,
        version: "v2.6"
      });


      FB.Event.subscribe('send_to_messenger', function(response) {
    if ( response.event == 'clicked' ) {
          // callback for events triggered by the plugin
            window.top.location = 'https://www.messenger.com/t/page_id';
    };
      });

    };

    (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.net/en_US/sdk.js";
       fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));

  </script>

<br>
<div class="fb-send-to-messenger"

  messenger_app_id="app_id" 
  page_id="page_id"
  data-ref="send this info to the app"
  color="blue"
  size="xlarge" >
</div>