Class: Kogno::Telegram::ChatActivity

Inherits:
Object
  • Object
show all
Defined in:
lib/core/lib/telegram/chat_activity.rb

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ ChatActivity

Returns a new instance of ChatActivity.



5
6
7
# File 'lib/core/lib/telegram/chat_activity.rb', line 5

def initialize(data)
  @data = data
end

Instance Method Details

#chatObject



13
14
15
# File 'lib/core/lib/telegram/chat_activity.rb', line 13

def chat
  @data[:chat]
end

#chat_idObject



17
18
19
# File 'lib/core/lib/telegram/chat_activity.rb', line 17

def chat_id
  @data[:chat][:id]
end

#chat_membership_statusObject



53
54
55
56
57
58
59
# File 'lib/core/lib/telegram/chat_activity.rb', line 53

def chat_membership_status    
  if ["member","administrator"].include?(@data[:new_chat_member][:status])
    return true
  elsif  ["left","kicked"].include?(@data[:new_chat_member][:status])
    return false
  end
end

#chat_permsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/core/lib/telegram/chat_activity.rb', line 37

def chat_perms
  perms = @data[:new_chat_member]
  {
    can_be_edited: perms[:can_be_edited],
    can_manage_chat: perms[:can_be_edited],
    can_change_info: perms[:can_be_edited],
    can_post_messages: perms[:can_be_edited],
    can_edit_messages: perms[:can_be_edited],
    can_delete_messages: perms[:can_be_edited],
    can_invite_users: perms[:can_be_edited],
    can_restrict_members: perms[:can_be_edited],
    can_promote_members: perms[:can_be_edited],
    can_manage_voice_chats: perms[:can_be_edited]
  }
end

#chat_titleObject



21
22
23
# File 'lib/core/lib/telegram/chat_activity.rb', line 21

def chat_title
  @data[:chat][:title]
end

#chat_typeObject



25
26
27
# File 'lib/core/lib/telegram/chat_activity.rb', line 25

def chat_type
  @data[:chat][:type]
end

#get_context(user, notification, notification_group, chat = nil) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/core/lib/telegram/chat_activity.rb', line 61

def get_context(user,notification, notification_group,chat=nil)
  # Here you can use different contexts by self.post_id
  context_class = Context::router(Kogno::Application.config.routes.chat_activity)[:class]
  context_route = :main
  context = context_class.new(user,self,notification,context_route, notification_group, chat)
  return(context)
end

#get_kgn_idObject



33
34
35
# File 'lib/core/lib/telegram/chat_activity.rb', line 33

def get_kgn_id
  nil
end

#handle_event(debug = false) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/core/lib/telegram/chat_activity.rb', line 69

def handle_event(debug=false)

  begin

    user = User.find_or_create_by_psid(self.inviter_user_id, :telegram)
    self.set_nlp(user.locale)
    I18n.locale = user.locale unless user.locale.nil?
    notification = Notification.new(user,self)
    chat = TelegramChatGroup.find_or_create({
      chat_id: self.chat_id,
      title: self.chat_title,
      kind: self.chat_type,
      inviter_user_id: user.id,
      membership: self.chat_membership_status       
    })

    notification = Notification.new(user,self)
    notification_group = Notification.new(chat,self)

    context = get_context(user,notification, notification_group, chat)
    context.run_for_chat_activity_only

    notification.send
    notification_group.send

    if Kogno::Application.config.
      user.log_message(self, :chat_activity_received) 
      user.log_response(notification)
    end
   
  rescue StandardError => e
    error_token = Digest::MD5.hexdigest("#{Time.now}#{rand(1000)}") # This helps to identify the error that arrives to Slack in order to search it in logs/http.log      
    logger.write e.message, :red
    logger.write "Error Token: #{error_token}", :red
    logger.write "Backtrace:\n\t#{e.backtrace.join("\n\t")}", :red
    ErrorHandler.notify_by_slack(Kogno::Application.config.app_name,e, error_token) if Kogno::Application.config.error_notifier.slack[:enable] rescue false
  end

end

#inviter_user_idObject



29
30
31
# File 'lib/core/lib/telegram/chat_activity.rb', line 29

def inviter_user_id
  @data[:from][:id]
end

#typeObject



9
10
11
# File 'lib/core/lib/telegram/chat_activity.rb', line 9

def type
  :chat_activity
end