Automatiser l'envoi d'e-mails d'invitation Discourse avec Zapier

L’API Discourse peut être utilisée pour automatiser l’envoi d’e-mails d’invitation depuis votre forum lorsqu’une action est effectuée sur une application externe. Pour de nombreuses applications, vous pouvez utiliser Zapier pour configurer cela sans avoir à écrire de code.

Un cas d’utilisation courant est d’inviter des utilisateurs sur votre forum lorsqu’ils achètent un produit ou s’inscrivent sur un service externe. Pour l’exemple dans ce sujet, j’utiliserai l’intégration Zapier WordPress pour déclencher l’envoi d’une invitation depuis Discourse lorsqu’un nouvel utilisateur s’inscrit sur un site WordPress.

Configuration d’une étape de déclenchement sur Zapier

Rendez-vous sur votre tableau de bord Zapier et recherchez l’application que vous souhaitez utiliser pour déclencher l’e-mail d’invitation. Dans les captures d’écran ci-dessous, j’ai sélectionné WordPress comme déclencheur.

La seule configuration requise est de connecter le Zap WordPress à un compte WordPress et de sélectionner l’événement Nouvel Utilisateur pour déclencher le Zap.

Configuration d’une étape d’action pour envoyer l’invitation

Cliquez sur le bouton Ajouter une étape, puis cliquez sur Action/Recherche dans le menu déroulant.

Entrez ‘webhook’ dans le menu de recherche, puis sélectionnez Webhooks par Zapier dans le menu des actions. L’étape d’action Webhook sera ajoutée à la barre latérale de votre Zap, et un formulaire s’ouvrira vous permettant de choisir le type de requête que le webhook doit effectuer.

Sélectionnez POST dans le formulaire, puis cliquez sur Continuer.

Le formulaire qui s’ouvre vous permet de configurer la requête POST effectuée vers Discourse. Dans le champ URL, entrez l’URL de base de votre forum, suivie de /invites. Par exemple, l’URL de base de mon forum est https://demo.scossar.com, donc le lien complet vers la route des invitations est https://demo.scossar.com/invites

Vous devez maintenant configurer les sections Type de Charge Utile, Données, et En-têtes du formulaire. Toutes les autres sections du formulaire peuvent conserver leurs valeurs par défaut.

Le Type de Charge Utile indique à Zapier dans quel format envoyer la charge utile. Il doit être défini sur Json.

La section Données est utilisée pour indiquer à Discourse quelle adresse e-mail envoyer l’invitation, et pour définir une liste facultative de noms de groupes et un message personnalisé. Pour inviter un utilisateur sans l’ajouter à des groupes ou inclure un message personnalisé, entrez le texte email comme Clé de données, puis cliquez sur l’icône ‘Insérer un Champ’ pour ouvrir le menu déroulant du champ. Faites défiler la liste jusqu’à ce que vous trouviez la valeur qui a été transmise depuis votre application de déclenchement et qui contient l’e-mail de l’utilisateur. Lors de l’utilisation de WordPress comme application de déclenchement, la section Données complétée devrait ressembler à ceci :

Pour ajouter automatiquement des utilisateurs à des groupes lorsqu’ils acceptent l’invitation, cliquez sur l’icône :heavy_plus_sign: de la section Données pour ajouter un nouveau champ. Définissez la Clé du champ sur group_names. Définissez la Valeur du champ sur une liste de noms de groupes séparés par des virgules que vous souhaitez attribuer aux utilisateurs. Pour inclure un message personnalisé dans l’e-mail d’invitation, créez un autre champ avec sa Clé définie sur custom_message et sa valeur définie sur le message que vous souhaitez envoyer.

Note : en ajoutant une étape de Code après l’étape de déclenchement de votre Zap, il devrait être possible de définir dynamiquement des noms de groupes et un message personnalisé pour des utilisateurs spécifiques.

La section En-têtes est utilisée pour authentifier votre requête. Pour configurer le champ En-têtes, vous devrez générer une clé d’API sur votre site Discourse. Pour ce faire, accédez à la page Admin / API de votre site Discourse et cliquez sur le bouton “Nouvelle clé d’API”. Lors de la création de la clé, attribuez-la à un utilisateur du personnel et définissez la portée sur Global, ou utilisez une portée Granulaire avec la case Invitations > Créer cochée.

Maintenant, dans la section En-têtes du Zap, entrez les paires clé/valeur suivantes. Remplacez toutes les valeurs que j’ai incluses entre crochets par des valeurs appropriées pour votre site. La casse des noms de clés est importante.

Api-Username: <le nom d'utilisateur du personnel que vous souhaitez utiliser pour envoyer les invitations>
Api-Key: <la clé d'API que vous avez générée>
Content-Type: application/json

Le formulaire devrait maintenant ressembler à la capture d’écran ci-dessous, mais avec votre Api-Username et Api-Key :

Cliquez sur le bouton Continuer.

Vous pourrez maintenant tester l’intégration en cliquant sur le bouton ‘Envoyer un Test à Webhooks par Zapier’. Le test sera effectué avec les données que vous avez configurées dans votre étape de déclenchement. Vous verrez un message de succès si tout est correctement configuré, et si l’utilisateur que vous avez configuré dans votre étape de déclenchement n’existe pas encore sur votre forum Discourse, un e-mail d’invitation sera envoyé et vous verrez un message de Succès sur Zapier. Une fois que l’étape de test passe, cliquez sur le bouton Terminer et activez votre Zap.

Pour plus de détails sur la manière d’effectuer d’autres types de requêtes à l’API Discourse avec Zapier, consultez Make requests to the Discourse API with Zapier.

25 « J'aime »

This is awesome thank you for the feature and clear instructions!

Is it possible to also set the custom group when generating an invite, like you can from within the app?

That can be done by adding a group_names key to the Headers field. Its value should be set to a comma separated list of group names. I’ll add an example of this to the topic.

The basic approach that’s used in this topic will work for any API request to Discourse.

3 « J'aime »

Awesome!! I think the dialog accepts a custom message too, guessing that can be done the same way?

1 « J'aime »

I tried setting this up this AM and was running into a 400 error. With a little bit of debugging, I found that the auth-related steps need to go in the headers section as instructed, but the actual form values (email, group name, and custom message) need to go in the data section as key/value sets.

Once I made that change, though, it worked perfectly!

1 « J'aime »

Thanks for that, @simon!

Are there any descriptions on meta about alternative ways to create an automated process for sending an invite email after the reader signed up to an offering and to which you could link here?

Thanks for testing that! What I’m finding is that when no custom_message or group_names are included in the request, passing the email in the Headers works. It’s not the correct way to pass the email address though. I’ve updated the guide to use the Data section for setting the email address, and have included examples for adding users to groups and including a custom message.

4 « J'aime »

I’m not aware of other services that are similar to Zapier, but I’m sure there are some out there. You can make requests directly to the Discourse API without using a third party service. The main requirement for doing this is that you have an application to make the requests from. This can be something a simple as a WordPress site.

A good resource on Meta for getting started with the API is Reverse engineer the Discourse API. You can test API requests with CURL from your computer’s terminal, or with a service like Postman.

2 « J'aime »

Thank you! Most of what you write I have no idea about… but I’ll check I’ll out your links and see if so can understand anything :smiley:

2 « J'aime »

@simon

Quick question:

When I send an automated invite through Zapier, would this then be an already personalized link, or a link to the general login page to my forum (where someone can log in and also create an account)?

I would like to avoid that everybody and their dog who knows the login page creates an account. How can that best be avoided?

When you send an invite link through Zapier you are triggering exactly the same action as if you had clicked the ‘Send an Invite’ link from your account’s Invite page.

Creating an invite will send an email to invited user’s email address. That email contains a link to a page where the invited user can set their name and password, and login to their new account. The email address for the new account is set by the email address that you used for the invite.

Only users who have been sent an invite can create an account through this page. If you would like to only allow invited users to register for your site, go to the Login section of your Site Settings and select the invite only site setting.

The best way to get a sense of how invites work is to invite yourself to your site. You can use a throw-away gmail address for this. You will need to log out of the site before you will be able to accept your invite.

3 « J'aime »

Ah, awesome info. Thanks!!

1 « J'aime »

Thanks for the great tutorial! Is there a way to add a topic id that the user gets redirected to after signing up like in the bulk invite feature?

It is possible to invite a new user to a topic with a similar approach to what is outlined above, but the URL that you setup in the Action step needs to point to the topic. For example, to invite a user to a topic with the id 123 you would add the URL https://forum.example.com/t/123/invite to the Action step. You would then add the email and the optional custom_message and group_names parameters in the same way as they are outlined in the guide.

3 « J'aime »

Hi Simon,

Thanks for your post !

I have 2 questions, if you could help me, it could be awesome :slight_smile:

Question 1/
On my side, the invite + message is working fine, but whatever group_names I’m adding, I only have access to the default public group, when I join the forum.

Do you know is something could “block” the invite to specific private group ?

The URL of the group name is like that : https://forum.[mywebsite].com/c/cercle/33

So I have : group_names /// PUBLIC,cercle

I also tried to use “number” instead of the group name (33) for this group, but it still doesn’t work.

Maybe there is a kind of “restriction” on the admin setting of the forum, which block the private group invite ?

Question 2/

I would like to be able to invite people, not only to “private group”, but "private “sub-group” (I don’t know the right name for that)

Screenshot 2021-05-12 at 08.04.11

Here you can see, I have the group “RÉSERVÉ…”
And all the little sub-groups under it.

I would like to invite users to specific subgroups.

Do you know how I can do that ?

Thanks a lot if you can take some time to help me on that ! :slight_smile:

1 « J'aime »

Make sure that you are using the group’s name and not the name of the category that you allow the group to access. You can find the group’s name in the URL for the group, or from the group’s Name field:

If that does not resolve the issue, make sure that the Discourse API key you add to Zapier is a global All Users API key. Also, make sure that the API Username set on Zapier has permission to add users to the group. Any admin username on your site will work for this.

Let me know if this doesn’t resolve the issue for you.

You can invite the user to multiple groups by adding a comma separated list of group names to the group_names field on Zapier. Discoure doesn’t have the concept of sub-groups, so you will need to add the name of each group you want to invite the user to.

From the screenshot you have provided, it looks like you are trying to give the user access to a category and all of its subcategories. Instead of creating a group for each subcategory you could just allow a single group to access the category and all of its subcategories. This is configured on the Security section of the category’s edit page.

2 « J'aime »

Thanks for your reply Simon !

So, regarding the API :

I had give the access to only one user (the admin) I thought it was ok. So now I created a new API to give access to all user.

But, it still didn’t work. But I may have understood the issue :slight_smile:

I’m trying to add a “category” name, in the “group_names” field. So I guess it can’t work :).

I misunderstood the name of groups/category/topic…

On the screenshot you can see a list of maybe 15 subcategories : TB, MT, RD,MA…)

For example, the URL of the first subcategory is : https://forum.MYWEBSITE.com/c/question-formactions/TBD/14

So I guess would like to invite new members automatically to some private subcategory, and Not some Group.

On the URL I gave you, I would like to give only access to the subcategory “TBD” for example.

And, another user will have Only access to another one.

Do you know if it’s possible to do it ?

Maybe I just have to replace the field “group_names” on Zapier, by the one for Categories ?

1 « J'aime »

Groups and Categories are a source of much confusion in Discourse!

  1. Groups are collections of people.
  2. Categories are collections of topics. You can’t invite people to them per se. But access to these is controlled by groups.

To achieve what you want, make a group. In the relevant category security settings, add access for that group. Use that group for the invites API. Done.

3 « J'aime »

Thanks for your reply Nathan !

I will try that and post my result here :wink:

2 « J'aime »

Your solution works fine, thanks ! :smiley:

I have another question, maybe you (or @simon) could help me in that :

With Zapier, is there a way to give access to a New group, for people who are already members of Discourse ?

Basically, here is my workflow :

1/ A member joins one of my courses

2/ Zapier takes his email from my learning platform, and send him an invite to my Discourse + add him automatically to the right group, related to the course he just joined.

3/ Sometime, a few months later, this same member will join Another course : So, with my actual process, my Zapier will send him a new invite to Discours, whereas he is already a member, so the invite will not work.

I would need Zapier to give him access to a new group, if he is already a member of my Discourse.

Do you see a way to do that ?

1 « J'aime »