├── Controllers
└── UltraMessageController.cs
├── HookData.cs
├── LICENSE
├── Program.cs
├── Properties
└── launchSettings.json
├── README.md
├── WebHookExample.csproj
├── WebHookExample.csproj.user
├── WebHookExample.sln
└── appsettings.json
/Controllers/UltraMessageController.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Mvc;
2 | using System.Text.Json;
3 |
4 | namespace WebHookExample.Properties
5 | {
6 | [Route("api/")]
7 | [ApiController]
8 | public class UltraMessageController : ControllerBase
9 | {
10 | [HttpPost]
11 | public IActionResult Post([FromBody] HookData data)
12 | {
13 | Console.WriteLine(JsonSerializer.Serialize(data));
14 | return Ok(data);
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/HookData.cs:
--------------------------------------------------------------------------------
1 | using System.Text.Json.Serialization;
2 |
3 | namespace WebHookExample
4 | {
5 | public class Data
6 | {
7 | [JsonPropertyName("id")]
8 | public string Id { get; set; } = null!;
9 |
10 | [JsonPropertyName("from")]
11 | public string From { get; set; } = null!;
12 |
13 | [JsonPropertyName("to")]
14 | public string To { get; set; } = null!;
15 |
16 | [JsonPropertyName("ack")]
17 | public string? Ack { get; set; }
18 |
19 | [JsonPropertyName("type")]
20 | public string Type { get; set; } = null!;
21 |
22 | [JsonPropertyName("body")]
23 | public string Body { get; set; } = null!;
24 |
25 | [JsonPropertyName("media")]
26 | public string? Media { get; set; }
27 |
28 | [JsonPropertyName("fromMe")]
29 | public bool FromMe { get; set; }
30 |
31 | [JsonPropertyName("isForwarded")]
32 | public bool IsForwarded { get; set; }
33 |
34 | [JsonPropertyName("time")]
35 | public long Time { get; set; }
36 | }
37 | public class HookData
38 | {
39 | [JsonPropertyName("event_type")]
40 | public string EventType { get; set; } = null!;
41 |
42 | [JsonPropertyName("instanceId")]
43 | public string InstanceId { get; set; } = null!;
44 |
45 | [JsonPropertyName("data")]
46 | public Data? Data { get; set; }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022 Ultramsg
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/Program.cs:
--------------------------------------------------------------------------------
1 | var builder = WebApplication.CreateBuilder(args);
2 | builder.Services.AddControllers();
3 | var app = builder.Build();
4 | app.UseHttpsRedirection();
5 | app.UseAuthorization();
6 | app.MapControllers();
7 | app.Run();
8 |
--------------------------------------------------------------------------------
/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "https://json.schemastore.org/launchsettings.json",
3 | "iisSettings": {
4 | "windowsAuthentication": false,
5 | "anonymousAuthentication": true,
6 | "iisExpress": {
7 | "applicationUrl": "http://localhost:34184",
8 | "sslPort": 44350
9 | }
10 | },
11 | "profiles": {
12 | "WebHookExample": {
13 | "commandName": "Project",
14 | "dotnetRunMessages": true,
15 | "applicationUrl": "https://localhost:6000;http://localhost:6001",
16 | "environmentVariables": {
17 | "ASPNETCORE_ENVIRONMENT": "Development"
18 | }
19 | },
20 | "IIS Express": {
21 | "commandName": "IISExpress",
22 | "environmentVariables": {
23 | "ASPNETCORE_ENVIRONMENT": "Development"
24 | }
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # [Ultramsg.com](https://ultramsg.com/?utm_source=github&utm_medium=csharp&utm_campaign=webhook) whatsapp-webhook-csharp
2 |
3 | simple project to handle incoming WhatsApp message using c#.
4 |
5 | by [Ultramsg.com](https://ultramsg.com/?utm_source=github&utm_medium=csharp&utm_campaign=webhook)
6 |
7 | # Dependencies
8 |
9 | for local development purposes, a tunneling service is required. This example uses ngrok , You can download ngrok from [here](https://ngrok.com/download).
10 |
11 | # run project
12 | ```
13 | dotnet watch
14 | ```
15 | # Start ngrok
16 | ```
17 | ngrok http https://localhost:6000
18 | ```
19 |
20 | # set Webhook URL
21 |
22 | Go to your ultramsg account for set Webhook URL after copying the ngrok url and add /api/ Route like this :
23 |
24 | ```
25 | https://61-44-136-9.ngrok.io/api/
26 | ```
27 | and enable this option "Webhook on Received" .
28 | # receive WhatsApp messages
29 |
30 | now You should be able to receive WhatsApp webhooks .
31 |
32 | # Example json response
33 | ```
34 | {
35 | "event_type": "message_received",
36 | "instanceId": "1150",
37 | "id": "",
38 | "referenceId": "",
39 | "data": {
40 | "id": "false_10172127174@c.us_3EB02E5EB89CE03FB034",
41 | "from": "10172127174@c.us",
42 | "to": "10172127170@c.us",
43 | "author": "",
44 | "pushname": "E.St",
45 | "ack": "",
46 | "type": "chat",
47 | "body": "Hi",
48 | "media": "",
49 | "fromMe": false,
50 | "isForwarded": false,
51 | "isMentioned": false,
52 | "quotedMsg": {},
53 | "mentionedIds": [],
54 | "time": 1650002663
55 | }
56 | }
57 | ```
58 |
59 |
60 | # Support
61 | Use **Issues** to contact me
62 |
--------------------------------------------------------------------------------
/WebHookExample.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net6.0
5 | enable
6 | enable
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/WebHookExample.csproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | ApiControllerEmptyScaffolder
5 | root/Common/Api
6 |
7 |
--------------------------------------------------------------------------------
/WebHookExample.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.1.32414.318
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebHookExample", "WebHookExample.csproj", "{6D756EDB-92B3-47F2-899C-F89EB403C019}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {6D756EDB-92B3-47F2-899C-F89EB403C019}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {6D756EDB-92B3-47F2-899C-F89EB403C019}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {6D756EDB-92B3-47F2-899C-F89EB403C019}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {6D756EDB-92B3-47F2-899C-F89EB403C019}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {DE559E52-4837-42D0-9972-6CC660DD8E65}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft.AspNetCore": "Warning"
6 | }
7 | },
8 | "AllowedHosts": "*"
9 | }
10 |
--------------------------------------------------------------------------------