User model
It is one of the models that is predefined in a new project and is associated with the users table in the database.
In the conversation flow
@user
can be called, which is an instance of this model for the user who is chatting.bot/models/user.rb
class User < ActiveRecord::Base
end
id | Secuencial record ID. |
psid | The user identification on each platform. |
platform | The platform through which the user is chatting. |
psid_from_post_comment | The user's ID who has commented on a post on the Fan Page associated with the project. |
page_id | Facebook Page's ID |
name | User's name. |
first_name | User's first name. |
last_name | User's last name. |
timezone | User's timezone. |
locale | User's locale. |
last_usage_at | Stores the last time when a message or event was received from the user. |
context | The current context of the conversation with the user. |
context_params | Stores the parameters from the current context of the conversation with the user. |
session_vars | Stores the user's session vars. |
last_message_read | Boolean that indicates if the last message sent was read by the user. |
created_at | Date and time of record creation. |
updated_at | Date and time of the record's last update. |
This method returns true if the user has been created current session.
if @user.first_time?
@reply.text t(:welcome)
else
@reply.text t(:hello)
end
This attribute returns the user's platform. For example:
"messenger"
, "telegram"
or "whatsapp"
.case @user.platform
when "messenger"
@reply.text "You're in Messenger"
when "whatsapp"
@reply.text "You're in WhatsApp"
when "telegram"
@reply.text "You're in Telegram"
end
Returns the current context of the conversation with the user.
@user.context
Exit the user from the current context of the conversation.
@user.exit_context
Re-Schedule the messages associated with the provided tag.
@user.reschedule_scheduled_message(:window_24h, Time.now + 23.hours + 55.minutes)
Returns true if there is a scheduled message associated with the provided tag.
@user.scheduled_message?(:window_24h)
Deletes the scheduled message associated with the provided tag.
@user.destroy_scheduled_messages(:window_24h)
Returns the user's subscription status for Messenger Recurring Notifications
user = User.where(platform: :messenger).first
user.messenger_recurring_notification_data
=> {:token=>"XXXXXXX", :frecuency=>"daily", :expires_at=>2022-11-21 18:06:31 UTC, :token_status=>"NOT_REFRESHED", :timezone=>"UTC", :status=>:active}
Returns true if the subscription to Messenger Recurring Notifications is active.
user = User.where(platform: :messenger).first
if user.subscribed_to_messenger_recurring_notification?
puts "active"
else
puts "unactive"
end
This attribute allows saving and retrieving any data within the conversation flow.
if @user.vars[:contact_information].nil?
@reply.text "Your email: #{@user.vars[:contact_information][:email]}"
end
@user.vars[:contact_information] = nil
Para usar este attributo fuera del flujo de la conversación se necesita llamar a los metodos get_session_vars() y save_session_vars()
user = User.first
user.get_session_vars()
user.vars[:contact_information] = {
email: "[email protected]",
phone: "+34 654 022 112"
}
user.save_session_vars()
Set user locale
@user.set_locale(:es)
Suppose we need to ask the user to leave us their email and we would like to save that information.
To do this we could do the following:
alter table users add email varchar(60)
In the User model we will create the methods that we consider necessary to carry out this operation: in this case we will create one to save the mail and another to verify that it exists.
class User < ActiveRecord::Base
def save_email(email)
self.email = email
self.save
end
def has_email?
self.email.nil?
end
end
We can test this on the console by running
kogno c
in the terminal2.6.3 :001 > user = User.first
User Load (0.6ms) SELECT `users`.* FROM `users` WHERE `users`.`psid` = '111112222333333' LIMIT 1
=> #<User id: 1, psid: "111112222333333", page_id: "1111111111111111"....
2.6.3 :002 > user.has_email?
=> false
2.6.3 :003 > user.save_email("[email protected]")
(0.2ms) BEGIN
User Update (6.8ms) UPDATE `users` SET `users`.`email` = '[email protected]"' WHERE `users`.`id` = 1
(4.5ms) COMMIT
=> true
2.6.3 :004 > user.has_email?
=> true
Last modified 10mo ago