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.

Location

bot/models/user.rb
class User < ActiveRecord::Base

end

users table schema

New User Creation

When an incoming message or event arrives, in the most of the cases it will be associated to a person(user).

Through this model, Kogno will automatically create a record with the user's information in the table users in the database.

Common Methods and Attributes.

first_time?()

This method returns true if the user has been created current session.

Usage

if @user.first_time?
    @reply.text t(:welcome)
else
    @reply.text t(:hello)
end

platform

This attribute returns the user's platform. For example: "messenger", "telegram" or "whatsapp".

Usage

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

context

Returns the current context of the conversation with the user.

Usage

@user.context

exit_context()

Exit the user from the current context of the conversation.

Usage

@user.exit_context

reschedule_message(tag=Symbol, send_at=Time)

Re-Schedule the messages associated with the provided tag.

Usage

@user.reschedule_scheduled_message(:window_24h, Time.now + 23.hours + 55.minutes)

scheduled_message?(tag=Symbol)

Returns true if there is a scheduled message associated with the provided tag.

Usage

@user.scheduled_message?(:window_24h)

destroy_scheduled_messages(tag=Symbol)

Deletes the scheduled message associated with the provided tag.

Usage

@user.destroy_scheduled_messages(:window_24h)

messenger_recurring_notification_data()

Returns the user's subscription status for Messenger Recurring Notifications

Usage

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} 

messenger_recurring_notification?()

Returns true if the subscription to Messenger Recurring Notifications is active.

Usage

user = User.where(platform: :messenger).first
if user.subscribed_to_messenger_recurring_notification?
    puts "active"
else
    puts "unactive"
end

vars

This attribute allows saving and retrieving any data within the conversation flow.

Saving data

@user.vars[:contact_information] = {
    email: "martin@kogno.io",
    phone: "‭+34 654 022 112‬"
}

Retrieving data

if @user.vars[:contact_information].nil?
    @reply.text "Your email: #{@user.vars[:contact_information][:email]}"
end    

Deleting data

@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: "martin@kogno.io",
    phone: "‭+34 654 022 112‬"
}
user.save_session_vars()

set_locale(locale=Symbol)

Set user locale

Usage

@user.set_locale(:es)

Customization Example

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:

Adding email field in users table.

alter table users add email varchar(60)

Editing User model

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

Testing

We can test this on the console by running kogno c in the terminal

2.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("martin@kogno.io")
  (0.2ms)  BEGIN
  User Update (6.8ms)  UPDATE `users` SET `users`.`email` = 'martin@kogno.io"' WHERE `users`.`id` = 1
  (4.5ms)  COMMIT
 => true
 2.6.3 :004 > user.has_email?
 => true
   

Last updated