We currently have a workflow where users submit data by sending an email to a custom incoming email address, creating a staged user and a private message to a Discourse group.
Is it possible to achieve the same workflow using API calls? Basically 1) create staged user, 2) generate API key for that user, then 3) post on that users behalf? I see how to post group messages for existing users using the API docs, but don’t know if steps 1) and 2) are possible currently.
Yes, just create a user via the api. They won’t be “staged” at this point since they actually exist. If they ever need to log in, they can just reset their password.
This is also possible
Here is a rough example of how to create a user, active them, and generate an api key for them.
def create_user
user = {
name: example1,
email: "example1@example.com",
password: "ZvAmmkcSWQfsPQLBksg7wK59",
username: example1,
active: "false",
approved: "true",
approved_by_id: 1,
approved_at: DateTime.now
}
new_user = @client.create_user(user)
id = new_user['user_id']
@client.activate(id)
uri = URI.parse(@config.full_discourse_url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new("/admin/users/#{id}/generate_api_key?api_key=#{@client.api_key}&api_username=#{@client.api_username}")
response = http.request(request)
result = JSON.parse(response.body)
end
Another option instead of generating an api key for each user is you can just instantiate a new discourse client using the same admin api key and just specify the new username:
client = DiscourseApi::Client.new("http://127.0.0.1:3000")
client.api_key = "a71cb5058c6be27e42806ad788bc7b0008af9c15170d1be1827a24c8e8334107"
client.api_username = "system"
... create user here...
client2 = DiscourseApi::Client.new("http://127.0.0.1:3000")
client2.api_key = "a71cb5058c6be27e42806ad788bc7b0008af9c15170d1be1827a24c8e8334107"
client2.api_username = example1
... create post here ...
I’m trying to avoid creating a full-fledged user account and picking a username. Essentially I want to replicate whatever is happening in the email trigger (‘custom incoming email address’ in the group settings) where the sender’s email address will be ‘staged,’ such that if/when they do register, they’ll be able to claim any messages that were created on their behalf via the email trigger.
Does that make sense? Is there a way to ‘spoof’ an incoming email using API calls? Thanks for all your help!
Blake님, 감사합니다. 하지만 제 환경에서는 여전히 작동하지 않습니다. API에서 "email has been received and is queued for processing"라는 응답을 받았지만, Discourse에는 아무것도 표시되지 않습니다. 새로운 주제도, 대기 중인 사용자도 없습니다.
설정 검토 내용은 다음과 같습니다:
전역:
email_in: on
email_in_min_trust: 0
enable_staged_users: on
카테고리:
사용자 지정 수신 이메일 주소: [my_name]@gmail.com으로 설정
계정이 없는 익명 사용자의 이메일 허용: on
API 호출: curl -i -sS -X POST "[my_domain]/admin/email/handle_mail" -H "Content-Type: multipart/form-data;" -H "Api-Key: [...]" -H "Api-Username: system" -F "email=Date: Mon, 24 Feb 2020 13:13:34 -0700 From: [some_name]@gmail.com To: [my_name]@gmail.com Subject: test API email post This is a sample email message."
“고급 텍스트(Advanced text)” 탭에 동일한 이메일 텍스트를 입력해 보니, 각 항목(날짜, 발신자, 수신자, 제목)이 새 줄로 시작하고 본문은 두 개의 줄바꿈으로 구분될 경우 작동하는 것 같습니다. curl과 정확히 같은 방식으로 텍스트를 입력하면 아무것도 반환되지 않습니다. 이는 원시(raw) 이메일의 줄바꿈 때문일까요? 그렇다면 어떻게 해야 할까요?
관심 있는 분들을 위해, 어떤 설정이었나요? 저는 아무것도 변경하지 않은 것 같아서 궁금합니다.
이것은 의도된 동작이라고 생각합니다. Discourse 그룹으로 이메일을 보내는 경우에도 동일한 일이 발생합니다. 사용자는 자신이 Discourse 포럼으로 이메일을 보내고 있다는 사실조차 모를 수 있도록 설계되어 있기 때문입니다. 일반 이메일 계정으로 이메일을 보내면, 이메일 제공업체로부터 이메일이 수신되었다는 답장을 받지 않듯이, 우리는 그것이 작동한다고 믿고 있습니다.
설계상, 단계적 승인(staged) 사용자의 이메일 템플릿에서는 응답 지침과 같은 여러 필드가 생략됩니다:
그 이유에 대해 정확히 확신은 없지만, 코드 상으로는 이렇게 구현되어 있으므로, 현재는 Discourse가 기본적으로 지원하지 않는 작업을 시도하고 있어 상당한 어려움에 직면해 있다고 볼 수 있습니다.
네, 이메일 템플릿을 수정할 수는 있지만, 커스터마이징할 수 있는 범위는 매우 제한적입니다. 예를 들어, 단계적 승인 사용자와 일반 사용자에게 각각 다른 스타일의 이메일 템플릿을 적용하는 것은 불가능하다고 생각합니다. 대신, 알림 이메일 하단에 메시지를 추가하여 이메일로만 상호작용하는 대신 웹사이트를 방문하도록 유도하는 링크를 넣는 것은 가능합니다.