This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
TaskHelper.cs
114 lines (107 loc) · 4.45 KB
/
TaskHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using Bogus;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using TeamsToDoAppConnector.Models;
namespace TeamsToDoAppConnector.Utils
{
public static class TaskHelper
{
public static TaskItem CreateTaskItem()
{
var faker = new Faker();
return new TaskItem()
{
Title = faker.Commerce.ProductName(),
Description = faker.Lorem.Sentence(),
Assigned = $"{faker.Name.FirstName()} {faker.Name.LastName()}",
Guid = Guid.NewGuid().ToString()
};
}
public static async Task PostTaskNotification(string webhook, TaskItem item, string title)
{
string cardJson = GetConnectorCardJson(item, title);
await PostCardAsync(webhook, cardJson);
}
public static async Task PostWelcomeMessage(string webhookUrl)
{
string cardJson = @"{
""@type"": ""MessageCard"",
""summary"": ""Welcome Message"",
""sections"": [{
""activityTitle"": ""Welcome Message"",
""text"": ""Teams ToDo connector has been set up. We will send you notification whenever new task is added in [Task Manager Portal]("+AppSettings.BaseUrl+ @" ).""}]}";
await PostCardAsync(webhookUrl, cardJson);
}
private static async Task PostCardAsync(string webhook, string cardJson)
{
//prepare the http POST
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var content = new StringContent(cardJson, System.Text.Encoding.UTF8, "application/json");
using (var response = await client.PostAsync(webhook, content))
{
// Check response.IsSuccessStatusCode and take appropriate action if needed.
}
}
public static string GetConnectorCardJson(TaskItem task, string title)
{
//prepare the json payload
return @"
{
'summary': 'A task is added.',
'sections': [
{
'activityTitle': 'Task "+ title + @"!',
'facts': [
{
'name': 'Title:',
'value': '" + task.Title + @"'
},
{
'name': 'Description:',
'value': '" + task.Description + @"'
},
{
'name': 'Assigned To:',
'value': '" + task.Assigned + @"'
}
]
}
],
'potentialAction': [
{
'@context': 'http://schema.org',
'@type': 'ViewAction',
'name': 'View Task Details',
'target': [
'" + AppSettings.BaseUrl + "/task/detail/" + task.Guid + @"'
]
},
{
'@type': 'ActionCard',
'name': 'Update Title',
'inputs': [
{
'@type': 'TextInput',
'id': 'title',
'isMultiline': true,
'title': 'Please enter new title'
}
],
'actions': [
{
'@type': 'HttpPOST',
'name': 'Update Title',
'isPrimary': true,
'target': '" + AppSettings.BaseUrl + "/task/update?id=" + task.Guid + @"',
'body': '{""Title"":""{{title.value}}""}',
'bodyContentType': 'application/json'
}
]
}
]}";
}
}
}