Generating lots of Invite Tokens

official

(Arpit Jalan) #1

So you want to generate multiple Invite Tokens? Great! Let’s get started!

Install discourse-invite-tokens plugin

Plugin location:

Installation instructions:

Enable plugin

After installing plugin enable site setting invite tokens enabled.

Clone Discourse API Gem

If you already have Git and Ruby installed on your system, you can install Discourse API by running following command from console:

git clone https://github.com/discourse/discourse_api.git ~/discourse_api

Generate Master API Key

Generate Master API Key for your Discourse instance by visiting /admin/api, to interact with Discourse API.

Generate multiple Invite Tokens

Now that you have cloned Discourse API gem and generated master API key, let’s start generating invite tokens!

Open the discourse_api/examples/disposable_invite_tokens.rb file, and modify following information:

  • Provide API credentials:
client = DiscourseApi::Client.new("http://localhost:3000")
client.api_key = "YOUR_API_KEY"
client.api_username = "YOUR_USERNAME"

Replace http://localhost:3000 with the url of your discourse instance, eg: http://discourse.example.com

Replace YOUR_API_KEY with the master API key of your discourse instance, eg: b1f3175cb682b3e9b6ca419db77772120b19af993cbc14ebed80fea08e3bbd66

Replace YOUR_USERNAME with the Admin username of your discourse instance, eg: codinghorror

  • Provide Invite Token details:
invite_tokens = client.disposable_tokens(username: "eviltrout", quantity: 200 , group_names: "security,support")

Replace eviltrout with the username of person whose account you want to be attached with generated invites.

Replace 200 with the quantity of tokens you want to generate.

The group_names parameter is optional. If you want invited users to be automatically added to specific group(s), specify the group names separated with comma. eg: security,support

Now in console, from discourse_api directory run:

ruby examples/disposable_invite_tokens.rb

This command will generate a invite_tokens.csv file inside discourse_api/examples/ folder.

Open the invite_tokens.csv file and voilà it will have 100 invite tokens ready to be used instantly!

Prepare Invitation Links from Invite Token

The Invitation URL will be like:

http://discourse.example.com/invite-token/redeem/TOKEN?username=USERNAME&email=EMAIL&name=NAME&topic=TOPICID

Replace following fields:

  • *discourse.example.com with the URL of your Discourse instance.
  • *TOKEN with one of the 200 Invite tokens you just generated.
  • *EMAIL with the email of user you want to invite
  • USERNAME with the desired username of invited user
  • NAME with the first name of invited user
  • TOPIC with the id of the topic to direct the user to after joining

(*) these fields are required!

[Note that we use Percent-encoding in URL, so space needs to be specified as %20, + needs to be specified as %2B, etc.]

Visit Invitation Link

When the user will visit Invitation Link, they will be registered, activated and logged in instantly. If you specified the groups while generating tokens, they will be automatically added to that group(s) when logged in.

That’s it! Start generating Invite Tokens for your Discourse instance today!


Update: Now you can generate Invite links via UI!

To generate copyable invite link go to your invites page, click on “Send an Invite” button, enter the email of the user you want to invite, now click on “Copy Invite Link” button:

That’s it, now you will see copyable invite link:

Copy and paste away! :link:


Invite workflow
Sending Bulk User Invites
Invite token bulk email API sample
Make invitation email text customizable by admin or inviter
User invite process improvements
Invite Users - Account Name and Password
How to make users to explicitly agree to ToS
Invite tokens without email?
Invite tokens without email?
User invite process improvements
Remove Invite Spam Protection?
Sending Bulk User Invites
Multiple invites on single topic?
Invites assume user ID will be derived from email address
Sending Bulk User Invites
Short code to insert Join/Leave group button into a post
How to Assign Usernames?
(Allen - Watchman Monitoring) #2

Two questions -

1 - would these tokens expire?

2 - Could the URL be used to set a custom field:
For instance, i could I store a custom field named subdomain and value of widgetsinc with:

&custom_field=subdomain&custom_field_subdomain=widgetsinc

Clearly that’s not a feature now but I’m wondering how feasible this would be to build


(Sam Saffron) #3

Yes they do, there should be site settings for it.

Nope, this would be a slightly tricky change cause the custom fields would have to be manually flagged as “user editable”. It is feasible though.


(Joshua Frank) #4

Is there a way to generate these tokens from the app and not client code? I’m not a ruby programmer and would have a big learning curve and environment setup to do it this way. Is this logic available as a REST endpoint, for instance, that I could call from .NET?


(Sam Saffron) #5

All JSON api end points are available from whatever. Just do it in the ui with fiddler running and then emulate.


(Joachim Schwarz) #6

Hi,

I tried to follow the tutorial but when it comes to actually running the script I get the following error:

   root@forum:~/discourse_api# ruby examples/disposable_invite_tokens.rb
/usr/local/rvm/rubies/ruby-2.1.3/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- dotenv (LoadError)
        from /usr/local/rvm/rubies/ruby-2.1.3/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from /root/discourse_api/lib/discourse_api/client.rb:1:in `<top (required)>'
        from /usr/local/rvm/rubies/ruby-2.1.3/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from /usr/local/rvm/rubies/ruby-2.1.3/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from /root/discourse_api/lib/discourse_api.rb:2:in `<top (required)>'
        from /usr/local/rvm/rubies/ruby-2.1.3/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from /usr/local/rvm/rubies/ruby-2.1.3/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from examples/disposable_invite_tokens.rb:4:in `<main>'

I get similar errors for the other examples as well.

root@forum:~/discourse_api# ruby -v
ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-linux]

I have Ruby 2.1 installed, but so far I have basically no knowledge about ruby at all. So I apologize in case this is something really obvious. :wink:

Thanks for your help!

EDIT: Ok, it probably was something very obvious.

gem install discourse_api

seems to be the solution.

EDIT: However now I get the following error when running

root@forum:~/tmp/discourse_api# ruby examples/disposable_invite_tokens.rb
examples/disposable_invite_tokens.rb:12:in `<main>': undefined method `body' for #<Array:0x00000001c1d5f8> (NoMethodError)

(Arpit Jalan) #7

Sorry, I somehow missed this error. Fixed now

https://github.com/discourse/discourse_api/pull/76


#8

Can you clarify if this NAME field is equivalent to the ‘Your full name’ field on the conventional signup screen?
Also, if USERNAME is omitted, does discourse generate a username based on email the same as it would when using a bulk invite csv?


(Arpit Jalan) #9

:white_check_mark: Yes.

:white_check_mark: Yes.


(Christoph) #10

And if I haven’t? Or do I always have it since I’m running discourse, which is written in Ruby and pulled from Github?

Do I really need to do that when I already have access to admin/api and have already created an api key?


(Blake Erickson) #11

These instructions are for running on your local computer that then talks to your Discourse server. So you will need to have ruby installed on your local computer and you will need to install the discourse_api ruby gem in order to run the invite tokens command.


(Christoph) #12

Oh. So which steps do I do on my discourse server and which ones on my local computer?

Does it make sense to install git and Ruby on my Windows PC or should I rather fire up a virtual Linux machine and do it there?


(Felix Freiberger) #13

Assuming you use Windows 10, the easiest solution is probably to do this within the Windows Subsystem for Linux :slight_smile:


(Christoph) #14

I finally managed to produce my invite_tokens.csv :tada:

I kept getting

christoph@PC:~/discourse_api$ ruby examples/disposable_invite_tokens.rb
Traceback (most recent call last):
        3: from examples/disposable_invite_tokens.rb:16:in `<main>'
        2: from /home/christoph/.rbenv/versions/2.5.0/lib/ruby/2.5.0/csv.rb:1272:in `open'
        1: from /home/christoph/.rbenv/versions/2.5.0/lib/ruby/2.5.0/csv.rb:1272:in `open'
/home/christoph/.rbenv/versions/2.5.0/lib/ruby/2.5.0/csv.rb:1272:in `initialize': Permission denied @ rb_sysopen - /home/christoph/discourse_api/examples/invite_tokens.csv (Errno::EACCES)
christoph@PC:~/discourse_api$

In case anyone else is running into the same problem, I solved it with sudo chmod 777 /home/christoph/discourse_api/examples/ (though there may be a more elegant solution)

Now that I have my tokens, I realize that they are somewhat long and complicated for distributing them on paper (e.g. 0fcf1e297a34b45768efe6ca594ce93c). I don’t want anyone to have to type this, especially since people are bound to get certain letters wrong or mistake a 0 for an O or whatever. So I wonder if there would be any way of creating either pronounceable tokens or (probably easier), much shorter ones, perhaps 8 or 12 characters divided into three blocks of 4, separated by a dash: 0FCF-1E29-7A34?


(Rafael dos Santos Silva) #15

You could distribute those as QR Codes too.

https://meta.discourse.org/invites/0fcf1e297a34b45768efe6ca594ce93c would become

image


(Christoph) #16

I suspected that someone would come up with QR codes, but the problem is: since I don’t have the email-address, it doesn’t help much to create a QR code because people still need to manually add their email.

But maybe you could make the email address requirement a site setting so that this link: https://meta.discourse.org/invite-token/redeem/0fcf1e297a34b45768efe6ca594ce93c would not lead to this error message: {"errors":["Validation failed: Primary email is invalid"]} but show people the invitee landing page that ordinary invited users arrive at, where they can enter their email address, etc?

In fact, letting all invitees, including those using a mass-generated token, pass through the same invitee landing page would have the additional benefit that they have to fill in the same required user fields as everyone else.


Agnostic Invite Link