在自定义菜单上检测用户操作以将卡插入捆绑中 [英] Detecting a user action on a custom menu to insert cards in a bundle

查看:73
本文介绍了在自定义菜单上检测用户操作以将卡插入捆绑中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个只带封面的捆绑包,我已经使用镜像API将其插入了时间轴.现在我想要的是,当用户单击捆绑软件时,我会得到一个自定义菜单,单击该菜单后将调用后端,以在同一捆绑软件中再次插入一组卡片.

I have a bundle with just a cover that i have inserted in my timeline using mirror API. Now what i want is when a user clicks on the bundle, i get a custom menu clicking on which the backend is called to insert a set of cards again in the same bundle.

public class newsfeedbliss {
    static String bundleId = "lunchRoulette" + UUID.randomUUID();
    private static ArrayList<String> newstext = new ArrayList<String>();

    static final String PROD_BASE_URL = "https://newsfeedbliss.appspot.com";
      private static final String PROD_CALLBACK = PROD_BASE_URL + "/newsfeedcallback";
      private static final String TEST_CALLBACK = "https://newsfeedbliss.appspot.com/newsfeedcallback";

     public static void subscribe( HttpServletRequest req, String userId )
              throws IOException
          {
            Mirror mirror = MirrorUtils.getMirror( req );

            // START:subscribe

            final String callbackUrl = "https://newsfeedbliss.appspot.com/newsfeedcallback";
            Subscription tliSubscription = new Subscription()
              .setCallbackUrl( callbackUrl )
              .setVerifyToken( "a_secret_to_everybody" )
              .setUserToken( userId )
              .setCollection( "timeline" )
              .setOperation( Collections.singletonList( "UPDATE" ) );

            mirror.subscriptions().insert( tliSubscription ).execute();
            // END:subscribe

            // TODO: check if this user has subscribed, skip if already has
            SubscriptionsListResponse subscriptions = mirror.subscriptions().list().execute();
            for (Subscription sub : subscriptions.getItems()) {
              System.out.println( sub );
            }
          }



          public static TimelineItem buildarticlestimeline(
                  ServletContext ctx, String userId )
throws IOException, ServletException, ParserConfigurationException, SAXException
{
              Mirror mirror = MirrorUtils.getMirror( userId );
                Timeline timeline1 = mirror.timeline();
                TimelineItem timelineItem1 = new TimelineItem()
                .setText("Hello");
                timeline1.insert( timelineItem1 ).executeAndDownloadTo( System.out );

            return timelineItem1;
            }


    public static void insertSimpleTextTimelineItem( HttpServletRequest req )
            throws IOException, ParserConfigurationException, SAXException
            {
            Mirror mirror = MirrorUtils.getMirror( req );
            Timeline timeline = mirror.timeline();
            TimelineItem timelineItem = new TimelineItem()
            .setHtml("<article>\n  <section>\n    <p class=\"text-auto-size\">This <em class=\"yellow\">paragraph</em> auto-resizes according to the <strong class=\"blue\">HTML</strong> content length.\n    </p>\n  </section>\n</article>\n")
            .setBundleId(bundleId)
            .setIsBundleCover(true);
            setSimpleMenuItems(timelineItem,true);
            timeline.insert( timelineItem ).executeAndDownloadTo( System.out );
            System.out.println("Hello hello");

            }


    public static void setSimpleMenuItems( TimelineItem ti, boolean hasRestaurant ) {
        // Add blank menu list
        ti.setMenuItems( new LinkedList<MenuItem>() );
        ti.getMenuItems().add( new MenuItem().setAction( "READ_ALOUD" ) );
        ti.getMenuItems().add( new MenuItem().setAction( "DELETE" ) );
        List<MenuValue> menuValues = new ArrayList<MenuValue>(2);
          menuValues.add( new MenuValue()
            .setState( "DEFAULT" )
            .setDisplayName( "Alternative" )
            // .setIconUrl( "" )
          );
          menuValues.add( new MenuValue()
            .setState( "PENDING" )
            .setDisplayName( "Generating Alternative" ) );

          ti.getMenuItems().add( new MenuItem()
            .setAction( "CUSTOM" )
              .setId( "ALT" )
              .setPayload( "ALT" )
              .setValues( menuValues )
          );
        }
}

这是我的servlet文件

This is my servlet file

    public class NewsfeedblissServlet extends HttpServlet
{
    private static final Logger log = Logger.getLogger(NewsfeedblissServlet.class.getName());
/** Accept an HTTP GET request, and write a random lunch type. */

public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException
{

    log.info("in do get");
    try {
        newsfeedbliss.insertSimpleTextTimelineItem( req );
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    log.info("called insert text");
    resp.setContentType("text/html");
    resp.getWriter().append( "Inserted Timeline Item" );
}
}

这是我编写的具有希望在回调上运行的代码的类,该代码可检测到自定义菜单单击并插入卡.

And this is the class i have written that has the code that i want to run on callback that detects custom menu click and inserts the cards.

    public class TimelineUpdateServlet extends HttpServlet
{
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
    System.out.println("Hey, Hello");
    res.getWriter().append( "Inside update servlet" );
// Generate Notification from request body
JsonFactory jsonFactory = new JacksonFactory();
Notification notification =
jsonFactory.fromInputStream( req.getInputStream(), Notification.class );
// Get this user action's type
String userActionType = null;
if( !notification.getUserActions().isEmpty() )
userActionType = notification.getUserActions().get(0).getType();
//If this is a pinned timeline item, log who and which timeline item

if( "timeline".equals( notification.getCollection() )
&& "UPDATE".equals( notification.getOperation() )
&& "CUSTOM".equals( userActionType ) )
{
    UserAction userAction = notification.getUserActions().get(0);
    if( "ALT".equals( userAction.getPayload() ) )
    {
    // Add a new timeline item, and bundle it to the previous one
    String userId = notification.getUserToken();
    String itemId = notification.getItemId();
    Mirror mirror = MirrorUtils.getMirror( userId );
    Timeline timeline = mirror.timeline();
    // Get the timeline item that owns the tapped menu
    TimelineItem current = timeline.get( itemId ).execute();
    String bundleId = current.getBundleId();
    // If not a bundle, update this item as a bundle
    if( bundleId == null ) {
    bundleId = "lunchRoulette" + UUID.randomUUID();
    current.setBundleId( bundleId );
    timeline.update( itemId, current).execute();
    }
    // Create a new random restaurant suggestion
    TimelineItem newTi=null;
    try {
        newTi = newsfeedbliss.buildarticlestimeline( getServletContext(), userId );
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    newTi.setBundleId( bundleId );
    timeline.insert( newTi ).execute();

}
}
}
}

推荐答案

在您的TimelineUpdateServlet代码中,您必须将原始项目修改为setIsBundleCover(true).捆绑包封面将没有任何菜单项,因为它仅充当其下级菜单项的父项.因此呼叫

In your TimelineUpdateServlet code you must modify your original item to setIsBundleCover(true). The bundle cover will not have any menu items as it only acts as a parent for the items below it. Thus call

current.setIsBundleCover(true);

在致电之前

timeline.update( itemId, current).execute();

这应该允许新卡正确出现在捆绑软件中.

This should allow the new card to appear in the bundle properly.

这篇关于在自定义菜单上检测用户操作以将卡插入捆绑中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆