forked from soxtoby/SlackNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlackMessage.cs
59 lines (49 loc) · 2.23 KB
/
SlackMessage.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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using SlackNet.Blocks;
namespace SlackNet.Bot;
public class SlackMessage : IMessage
{
private readonly ISlackBot _bot;
public SlackMessage(ISlackBot bot) => _bot = bot;
public Conversation Conversation { get; set; }
[Obsolete("Use Conversation instead")]
public Hub Hub { get; set; }
public User User { get; set; }
public string Text { get; set; }
public string Ts { get; set; }
[JsonIgnore]
public DateTime Timestamp => Ts.ToDateTime().GetValueOrDefault();
public string ThreadTs { get; set; }
[JsonIgnore]
public DateTime ThreadTimestamp => Ts.ToDateTime().GetValueOrDefault();
public IList<Attachment> Attachments { get; set; } = new List<Attachment>();
public IList<File> Files { get; set; } = new List<File>();
public IList<Block> Blocks { get; set; } = new List<Block>();
public bool IsInThread => ThreadTs != null;
public bool MentionsBot =>
Text.IndexOf(_bot.Id, StringComparison.OrdinalIgnoreCase) >= 0
|| Text.IndexOf(_bot.Name, StringComparison.OrdinalIgnoreCase) >= 0
|| Conversation?.IsIm == true;
public Task ReplyWith(string text, bool createThread = false, CancellationToken? cancellationToken = null) =>
ReplyWith(new BotMessage { Text = text }, createThread, cancellationToken);
public async Task ReplyWith(Func<Task<BotMessage>> createReply, bool createThread = false, CancellationToken? cancellationToken = null)
{
await _bot.WhileTyping(Conversation.Id, async () =>
{
var reply = await createReply().ConfigureAwait(false);
if (reply != null)
await ReplyWith(reply, createThread, cancellationToken).ConfigureAwait(false);
}).ConfigureAwait(false);
}
public async Task ReplyWith(BotMessage message, bool createThread = false, CancellationToken? cancellationToken = null)
{
if (message == null) throw new ArgumentNullException(nameof(message));
message.ReplyTo = this;
message.CreateThread = createThread;
await _bot.Send(message, cancellationToken).ConfigureAwait(false);
}
}