Skip to content

Commit d099496

Browse files
authored
Update samples and documentation (#9)
1 parent ac031eb commit d099496

File tree

55 files changed

+630
-132
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+630
-132
lines changed

CONTRIBUTING.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Contributing
2+
3+
First off, thank you for considering contributing to IFTTT.NET. It's people like you that make IFTTT.NET a great tool and, for open source in general, the best tool for sharing knowledge and skills.
4+
5+
Following these guidelines helps to communicate that you respect my time as the developer managing and developing this open source project. In return, I should reciprocate that respect in addressing your issue, assessing changes, and helping you finalize your pull requests.
6+
7+
Any contribution is very much welcome and will improve the toolkit for the community. Whether it be fixing typpos ;), sharing tutorials, improving documentation, submitting issues, requesting features or, of course, writing code, I'll be more than happy to be on the receiving end!
8+
9+
If you do want to get involved but don't know where to start, you can check this website: [First Timers Only](https://www.firsttimersonly.com/).
10+
11+
One thing I'd like to avoid: please, don't use the issue tracker for support questions.\
12+
[Stack Overflow](https://stackoverflow.com/questions/ask) is a much better platform for this, and if you [tag me or poke me on SO](https://stackoverflow.com/users/1538048/invvard), I'll be glad to give you a hand.
13+
14+
I'll leave the basic rules here for now.\
15+
Thank you for considering contributing, whether you go forward or not!

README.md

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,143 @@
1-
Content upcoming soon...
1+
# IFTTT.NET: Simplifying IFTTT integration for .NET Developers
2+
3+
**IFTTT.NET** is a powerful .NET library designed to streamline your interaction with the **[IFTTT](https://ifttt.com/explore)** platform. Whether you're building web applications, services, or IoT solutions, our toolkit empowers you to seamlessly integrate IFTTT into your projects.
4+
5+
## Why Use IFTTT.NET?
6+
7+
- **Boilerplate-Free**: Say goodbye to repetitive code! Our library abstracts away the complexities of IFTTT integration, allowing you to focus on what matters most.
8+
- **Minimal Effort**: Get started quickly. IFTTT.NET simplifies the process of setting up triggers, actions, and applets.
9+
- **Modern API**: We've designed IFTTT.NET to be intuitive and easy to use.
10+
11+
## Getting Started
12+
13+
>    **TL;DR**\
14+
>    Go to the [Samples](https://github.com/Invvard/InvvardDev.IFTTT.NET/tree/main/samples) folder and check out the sample projects.
15+
16+
### Setup an IFTTT Trigger
17+
18+
1. **Install package**: Add our NuGet package [InvvardDev.Ifttt](https://www.nuget.org/packages/InvvardDev.Ifttt) to your project.
19+
2. **Create a Trigger class**: add a class implementing the `ITrigger` interface with the TriggerAttribute on top. This will allow IFTTT.NET to find, recognize and handle your Trigger:
20+
21+
```csharp
22+
[Trigger("trigger_slug")]
23+
public class YourNewTrigger : ITrigger
24+
{
25+
[DataField("trigger_field_slug")]
26+
public string YourTriggerFieldName { get; init; } = default!;
27+
28+
public Task ExecuteAsync(TriggerRequest triggerRequest, CancellationToken cancellationToken = default)
29+
{
30+
// Your trigger logic goes here
31+
32+
return Task.CompletedTask;
33+
}
34+
}
35+
```
36+
37+
The `TriggerAttribute` is used to define the Trigger slug. The `DataFieldAttribute` is used to define the Trigger field slug.\
38+
The `ExecuteAsync` method is the entry point for your Trigger logic. It will be called by IFTTT.NET when the Trigger is activated.
39+
3. **Add the test setup**: one of the mandatory step for publishing an IFTTT service is to have a test setup endpoint.\
40+
Just add a class implementing the `ITestSetup` interface.
41+
42+
```csharp
43+
public class TestSetup : ITestSetup
44+
{
45+
public Task<ProcessorPayload> PrepareSetupListing()
46+
{
47+
var processors = new ProcessorPayload { Triggers = new Processors() };
48+
49+
processors.Triggers
50+
.AddProcessor("trigger_slug")
51+
.AddDataField("trigger_slug", "trigger_field_slug", "some_value");
52+
53+
return Task.FromResult(processors);
54+
}
55+
}
56+
```
57+
58+
The `PrepareSetupListing` method is used to define the test setup payload. It will be called by IFTTT.NET when the test setup endpoint is called.\
59+
You can find the complete list of required Triggers, actions and queries in your service dashboard, under the `API` tab, `Endpoint tests` section, `Test setup` tab and click on either "**View** or **Download** a scaffold JSON response for your service".
60+
61+
4. **Configure startup**: In your application startup logic, configure the IFTTT.NET client:
62+
63+
```csharp
64+
var builder = WebApplication.CreateBuilder(args);
65+
66+
builder.Services
67+
.AddIftttToolkit("<your-service-key>")
68+
.AddTestSetupService<TestSetup>()
69+
.AddTriggerAutoMapper()
70+
.AddTriggers();
71+
72+
var app = builder.Build();
73+
74+
app.ConfigureIftttToolkit()
75+
.UseServiceKeyAuthentication()
76+
.ConfigureTriggers();
77+
78+
app.Run();
79+
```
80+
81+
5. **Run your application**: That's it! Your Trigger is now ready to be used in IFTTT Applets.
82+
83+
### Swagger support
84+
85+
Swagger is a good way to test your Triggers and Actions. Just add the `AddSwaggerGen` and `UseSwagger` methods to your application startup logic (see step 4).
86+
87+
```csharp
88+
builder.Services.AddSwaggerGen();
89+
[...]
90+
app.UseSwagger().UseSwaggerUI();
91+
```
92+
93+
In the event that you want to run Swagger with the Service Key Authentication activated, configure SwaggerGen like this:
94+
95+
```csharp
96+
builder.Services.AddSwaggerGen(options => options.AddIftttServiceKeyScheme());
97+
```
98+
99+
It will add a new security scheme to the Swagger UI, allowing you to test your Triggers and actions with the Service Key Authentication.
100+
101+
## Real time notifications
102+
103+
Next, you can use the real time notification feature to make your Trigger more responsive.\
104+
When you have a new data to send to IFTTT, prepare your Trigger identities or User IDs and call `ITriggerHook.SendNotification`.\
105+
Here is an example of a controller sending a notification to IFTTT with Trigger identities:
106+
107+
```csharp
108+
[ApiController]
109+
[Route("api/[controller]")]
110+
public class RealTimeNotificationController(ITriggerHook realTimeHook) : ControllerBase
111+
{
112+
[HttpPost]
113+
public async Task NotifyAsync(CancellationToken cancellationToken = default)
114+
{
115+
var notificationRequest = new List<RealTimeNotificationModel>
116+
{
117+
RealTimeNotificationModel.CreateTriggerIdentity("trigger_identity_12345"),
118+
RealTimeNotificationModel.CreateTriggerIdentity("trigger_identity_67890"),
119+
};
120+
121+
await realTimeHook.SendNotification(notificationRequest, cancellationToken);
122+
}
123+
}
124+
```
125+
126+
If you use User IDs, just call `RealTimeNotificationModel.CreateUserId("<user_id>")` instead of `RealTimeNotificationModel.CreateTriggerIdentity("<trigger_identity>")`.
127+
128+
## Roadmap
129+
130+
As of today, the library is still in its early stages and only supports Triggers, basic Data field and Service Key Authentication. We are working on adding support for Data field validation, User Authenticated integration and other advanced features .\
131+
Fortunately, IFTTT is designed in such a way that once Triggers are implemented, Actions and Queries should be easy to implement.
132+
133+
## Contributing
134+
135+
Check the [contributing introduction](./CONTRIBUTING.md).
136+
137+
## Glossary
138+
139+
As IFTTT is designed in a way that Trigger, Action and Query have a common structure.\
140+
So in order to be as generic as possible, here are some definitions of the terms used in the library:
141+
142+
- **Processor**: refers to a Trigger, an Action or a Query.
143+
- **Data field**: a field that is used to pass data to or from a Trigger, an Action or a Query.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Threading.Tasks;
2+
using InvvardDev.Ifttt.Samples.Trigger.Triggers;
3+
using InvvardDev.Ifttt.Toolkit;
4+
5+
namespace InvvardDev.Ifttt.Samples.Trigger.Core;
6+
7+
public class TestSetup : ITestSetup
8+
{
9+
public Task<ProcessorPayload> PrepareSetupListing()
10+
{
11+
var processors = new ProcessorPayload { Triggers = new Processors() };
12+
13+
processors.Triggers
14+
.AddProcessor(NugetPackageUpdatedTrigger.TriggerSlug)
15+
.AddDataField(NugetPackageUpdatedTrigger.TriggerSlug, "nuget_package_to_watch", "Microsoft.Extensions.DependencyInjection")
16+
.AddDataField(NugetPackageUpdatedTrigger.TriggerSlug, "updated_version", "1.2.3")
17+
.AddDataField(NugetPackageUpdatedTrigger.TriggerSlug, "updated_date", DateTime.Now.ToLongDateString());
18+
19+
return Task.FromResult(processors);
20+
}
21+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="InvvardDev.Ifttt.Trigger" Version="0.1.0" />
10-
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0"/>
11-
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
9+
<PackageReference Include="InvvardDev.Ifttt" Version="0.1.1-dev" />
10+
<PackageReference Include="Microsoft.OpenApi" Version="1.6.13" />
11+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
1212
</ItemGroup>
1313

1414
<ItemGroup>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace InvvardDev.Ifttt.Samples.Trigger.Models;
4+
5+
public class ClientIftttOptions
6+
{
7+
public const string DefaultSectionName = nameof(ClientIftttOptions);
8+
9+
[Required]
10+
public required string ServiceKey { get; init; } = string.Empty;
11+
12+
public bool BypassServiceKey { get; init; }
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using InvvardDev.Ifttt.Samples.Trigger.Triggers;
2+
using InvvardDev.Ifttt.Toolkit;
3+
using InvvardDev.Ifttt.Toolkit.Attributes;
4+
5+
namespace InvvardDev.Ifttt.Samples.Trigger.Models;
6+
7+
[TriggerFields(NugetPackageUpdatedTrigger.TriggerSlug)]
8+
public class WatchedNugetTriggerFields : TriggerFieldsBase
9+
{
10+
[DataField("nuget_package_name")]
11+
public string NugetPackageName { get; init; } = default!;
12+
13+
[DataField("updated_version")]
14+
public string UpdatedVersion { get; init; } = default!;
15+
16+
[DataField("updated_date")]
17+
public DateTime UpdatedDate { get; init; }
18+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using InvvardDev.Ifttt.Hosting;
2+
using InvvardDev.Ifttt.Samples.Trigger.Core;
3+
using InvvardDev.Ifttt.Samples.Trigger.Models;
4+
using Microsoft.AspNetCore.Builder;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.DependencyInjection;
7+
8+
var builder = WebApplication.CreateBuilder(args);
9+
10+
var clientIftttOptions = builder.Configuration.GetSection(ClientIftttOptions.DefaultSectionName).Get<ClientIftttOptions>();
11+
12+
builder.Services.AddSwaggerGen(options => options.AddIftttServiceKeyScheme());
13+
14+
builder.Services
15+
.AddIftttToolkit(clientIftttOptions.ServiceKey)
16+
.AddTestSetupService<TestSetup>()
17+
.AddTriggerAutoMapper()
18+
.AddTriggers();
19+
20+
var app = builder.Build();
21+
22+
app.UseRouting();
23+
24+
app.ConfigureIftttToolkit()
25+
.UseServiceKeyAuthentication()
26+
.ConfigureTriggers();
27+
28+
app.UseSwagger()
29+
.UseSwaggerUI();
30+
31+
app.Run();

0 commit comments

Comments
 (0)