Rails:跟踪Facebook Messenger chatbot的点击次数的Shorturl节省了太多点击次数(Rails: Shorturl to track clicks for Facebo

编程入门 行业动态 更新时间:2024-10-07 10:22:44
Rails:跟踪Facebook Messenger chatbot的点击次数的Shorturl节省了太多点击次数(Rails: Shorturl to track clicks for Facebook Messenger chatbot is saving way too many clicks)

我使用Facebook-Messenger gem创建聊天机器人,使我可以发送消息给用户。

我想保存在我的rails数据库中,每次点击通用模型的url按钮。 为此,我创建了一个指向shorturl控制器的特殊url,该控件在保存数据后重定向到最终的url。

工作中的发送请求:

def perform(sender, title, image_url, subtitle, cta_url, cta_text, access_token, letter_id) Bot.deliver({ recipient: { id: sender }, message: { "metadata": letter_id, "attachment":{ "type": "template", "payload":{ "template_type": "generic", "elements":[ { "title": title, "image_url": image_url.html_safe, "subtitle": subtitle, "default_action": { "type": "web_url", "url": cta_url.html_safe }, "buttons": [ { "type": "web_url", "url": cta_url.html_safe, "title": cta_text }, { "type": "element_share" } ] } ] } } } }, access_token: access_token) end

Shorturl控制器:

class ShorturlController < ApplicationController def show @shorturl = ShortUrl.find_by_token(params[:id]) @card = Card.find(@shorturl.card_id) @subscriber = BotUser.find_by_sender_id(params['u']) analytic_clic.increment!(:click_count, by = 1) @final_url = @card.cta_button_url redirect_to @final_url, :status => 301 end private def analytic_clic @analytic_clic ||= AnalyticClic.find_or_create_by( card_id: @card.id, short_url_id: @shorturl.id, bot_user_id: @subscriber.id ) end end

与短程控制器相关的路线:

get 's/:id' => "shorturl#show"

不幸的是,当我将制作中的信息发送给大约200位订阅者时,它立即节省了大约100次点击。 我不知道为什么,因为Facebook不会通过发送http请求来“测试”url。 日志很好,我有很多请求短命,但在此之前没有错误。

任何想法为什么它执行所有这些请求我的短裤,即使用户还没有点击链接呢?

以下是我创建的shorturl示例: https ://www.botletter.com/s/qsKcsm7NCRp4qkRGUqfPx7Sp?u = 1451521288246029

I use the Facebook-Messenger gem in order to create a chatbot allowing me to send messages out of the blue to my subscribers.

I want to save in my rails database every click on the url button of the generic model. For that, I create a special url directing to a shorturl controller that redirects to the final url after saving the data.

The send request in a worker:

def perform(sender, title, image_url, subtitle, cta_url, cta_text, access_token, letter_id) Bot.deliver({ recipient: { id: sender }, message: { "metadata": letter_id, "attachment":{ "type": "template", "payload":{ "template_type": "generic", "elements":[ { "title": title, "image_url": image_url.html_safe, "subtitle": subtitle, "default_action": { "type": "web_url", "url": cta_url.html_safe }, "buttons": [ { "type": "web_url", "url": cta_url.html_safe, "title": cta_text }, { "type": "element_share" } ] } ] } } } }, access_token: access_token) end

The shorturl controller:

class ShorturlController < ApplicationController def show @shorturl = ShortUrl.find_by_token(params[:id]) @card = Card.find(@shorturl.card_id) @subscriber = BotUser.find_by_sender_id(params['u']) analytic_clic.increment!(:click_count, by = 1) @final_url = @card.cta_button_url redirect_to @final_url, :status => 301 end private def analytic_clic @analytic_clic ||= AnalyticClic.find_or_create_by( card_id: @card.id, short_url_id: @shorturl.id, bot_user_id: @subscriber.id ) end end

The route associated with the shorturl controller:

get 's/:id' => "shorturl#show"

Unfortunately, when I send the message in production to around 200 subscribers, it saves around 100 clicks immediately. I have no idea why, as Facebook does not "test" the url by sending a http request. The logs are fine, I have many get requests for the shorturl but no errors before that.

Any idea why it's performing all these get requests to my shorturl even if the subscribers haven't clicked on the link yet?

Here is an example of a shorturl I created: https://www.botletter.com/s/qsKcsm7NCRp4qkRGUqfPx7Sp?u=1451521288246029

最满意答案

问题在于Facebook抓取工具,我添加了以下条件以避免在Facebook访问网址时节省点击次数:

if request.user_agent == "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)" puts "Facebook crawl" else # save to db end

The problem was the Facebook crawler, I added the following condition to avoid saving clicks when Facebook is visiting the url:

if request.user_agent == "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)" puts "Facebook crawl" else # save to db end

更多推荐

本文发布于:2023-07-27 08:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287713.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:次数   太多   节省   chatbot   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!