chat_session_message.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. class Sessions::Event::ChatSessionMessage < Sessions::Event::ChatBase
  2. =begin
  3. a agent or customer creates a new chat session message
  4. payload
  5. {
  6. event: 'chat_session_message',
  7. data: {
  8. content: 'some message',
  9. },
  10. }
  11. return is sent as message back to peer
  12. =end
  13. def run
  14. return super if super
  15. return if !check_chat_session_exists
  16. chat_session = current_chat_session
  17. user_id = nil
  18. if @session
  19. user_id = @session['id']
  20. end
  21. chat_message = Chat::Message.create(
  22. chat_session_id: chat_session.id,
  23. content: @payload['data']['content'],
  24. created_by_id: user_id,
  25. )
  26. message = {
  27. event: 'chat_session_message',
  28. data: {
  29. session_id: chat_session.session_id,
  30. message: chat_message,
  31. },
  32. }
  33. # send to participents
  34. chat_session.send_to_recipients(message, @client_id)
  35. # send chat_session_init to agent
  36. {
  37. event: 'chat_session_message',
  38. data: {
  39. session_id: chat_session.session_id,
  40. message: chat_message,
  41. self_written: true,
  42. },
  43. }
  44. end
  45. end