# 백그라운드에서 환영 메시지 전송 스크립트

**URL:** <https://meta.discourse.org/t/script-for-sending-welcome-message-in-background/348879>\
**Category:** Product Support\
**Created:** [1월 24, 2025, 7:06오후 UTC](https://meta.discourse.org/t/script-for-sending-welcome-message-in-background/348879 "2025-01-24T19:06:19Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)\
**Post date:** [1월 24, 2025, 10:46오후 UTC](https://meta.discourse.org/t/script-for-sending-welcome-message-in-background/348879/7 "2025-01-24T22:46:28Z")

</div>

발신자 사용자명이 포함된 업데이트된 스크립트입니다:

```rb
MAX_MESSAGES = 1000 # 테스트를 위한 선택적 제한
SECONDS_BETWEEN_MESSAGES = 3 # 3초 간격
SENDER_USERNAME = "coco"

message_sender = User.find_by(username: SENDER_USERNAME)

if message_sender.nil?
  Rails.logger.error "발신자 사용자 '#{message_sender_username}'을(를) 찾을 수 없습니다!"
  exit
end

Rails.logger.info "대량 메시지 스크립트를 시작합니다..."

# 메시지 내용을 정의합니다
message_title = "커뮤니티에 오신 것을 환영합니다"
message_body = "안녕하세요! 여러분을 맞이하게 되어 정말 기쁩니다. 궁금한 점이 있으시면 알려주세요!"

# 마지막 접속 시간 순으로 사용자 조회
users = User.where(active: true)
            .where.not(staged: true) # 스테이징된 사용자 제외
            .order(last_seen_at: :desc)
            .limit(MAX_MESSAGES)

Rails.logger.info "메시지를 보낼 활성 사용자 #{users.count}명을 찾았습니다."

users.each_with_index do |user, index|
  begin
    Rails.logger.info "#{user.username}(#{index + 1}/#{users.count})에게 메시지를 큐에 추가하는 중..."

    PostCreator.create!(
      message_sender,
      title: message_title,
      raw: message_body,
      archetype: Archetype.private_message,
      target_usernames: user.username,
      skip_validations: true
    )

    Rails.logger.info "#{user.username}에게 메시지를 성공적으로 큐에 추가했습니다."

    # 다음 사용자를 처리하기 전에 지정된 간격만큼 대기
    sleep(SECONDS_BETWEEN_MESSAGES)
  rescue => e
    Rails.logger.error "#{user.username}에게 메시지 전송 실패: #{e.message}"
  end
end

Rails.logger.info "대량 메시지 스크립트 완료!"

```

---

_[View the full topic](https://meta.discourse.org/t/script-for-sending-welcome-message-in-background/348879)._
