How to make custom trigger with Azure Functions

Here is the project created this time.

This time I created a trigger to execute the function when Slack’s message was written using Slack’s Real Time Messaging API.

RTM is a real-time API using WebSocket, but it is a bothersome task to implement so far, so I used a library called SlackConnector this time.

In the sample program, the latest version of SlackConnector is not used. It is because it seemed that it took time to resolve because MissingMethodException occurred when I tried it the latest version.

Classes that need to be created

・Implementation class of IExtensionConfigProvider

・Implementation class of ITriggerBindingProvider

・Implementation class of ITriggerBinding

・Implementation class of IListener

・Arribute class

Attribute class

It’s really simple and enough. I have an AccessToken in a property with an Attribute for attaching to a parameter. Using this AccessToken as a key, pull the value from the environment variable and use it to connect to RTM.

Implementation class of IExtensionConfigProvider

The Initialize method of the implementation class of this interface is called when the job host starts. By registering the ITriggerBindingProvider in this function, TryCreateAsync of Provider registered when the host performs parameter binding of faction will be called.

Implementation class of ITriggerBindingProvider

As explained earlier, the TryCreateAsync function is called when the host searches the function and binds the parameters.

So if the Attribute of the parameter is correct, it returns the instant of ITriggerBinding and if it is irrelevant Attribute it will be null.

Implementation class of ITriggerBinding

The main task of this class is to bind the data from the listener to the parameters when instructing the execution of the function by listener class instantiation generation and listener.

The CreateListnerAsync method is simple. We should return the implementation of IListner.

The BindAsync function is also not complicated. It is okay to return with something like this. In this example, you can specify string and SlackMessage (Class of SlackConnector) as parameters of Trigger.

Implementation class of IListener

IListner needs to implement StartAsync, StopAsync, Cancel, Dispose.

This time StartAsync does not think about anything and RTM connection is done and StopAsync disconnects RTM.

When TryExexuteAsync method is executed, the function is executed via the BindAsync function of TriggerBinding.

For example ServiceBusTrigger etc is executing AbandonAsync if execution of function fails according to the execution result after TryExecuteAsync, CompleteAsync if it succeeded. So the queue is completed after function execution.

Actually running the sample

This implementation is a sample only, so there are bad things.
If an instance of Functions scale out, the function will be executed multiple times in one message.
So when using this implementation you need to use SIngletonAttribute in listener mode or implement equivalent.

If you can make a trigger with Azure Functions, your dream will spread.