Moodle Discord integration

Moodle Discord integration

by Tiago Goncalves -
Number of replies: 1

Hello,

I'm new to moodle development and I'm trying to make an integration with discord.

The idea is users login using their discord account and after they complete a course/activity moodle automatically makes a call to a discord webhook where to write something in a room that a bot picks up and adds the user to the room that corresponds to the course.

I was able to use OAuth2 with some custom code (not sure if I should be changing the code for this or if there is a better way) to add the user to moodle and save some info I get from discord. But now I don't know how to make the request when a user completes a course/activity. I was thinking of using the plugin WebHooks (https://moodle.org/plugins/local_webhooks/versions). Is this the best way to go about this?

 

Average of ratings: -
In reply to Tiago Goncalves

Re: Moodle Discord integration

by Francis Devine -
Picture of Core developers
Hi Tiago

I've implemented similar types of things before, but the local_webhooks plugin looks like it would work for the use case, although you would need to extend it to support the discord authentication.

more generically you can listen to events by subscribing to specific events in a plugin
https://docs.moodle.org/dev/Events_API

I'd then use the event listener to generate an adhoc task - https://docs.moodle.org/dev/Task_API#Adhoc_tasks (you want the event listeners to do as little as possible so they're fast, as they are called quite often and during the users session, the adhoc task will run in the background cron and therefore has the luxury of time, at the cost of a little latency of having to wait for the next cron run. Which on a modern site should be running at least every minute).

That adhoc task would then be responsible for calling the relevant discord webhook.


The tool trigger plugin does very similar things - https://moodle.org/plugins/tool_trigger so you may be able to browse the source for that https://github.com/catalyst/moodle-tool_trigger to get an idea of the code required.