Sending Messages
Learn how to send different types of messages using Signal.Bot.
Basic Text Messages
The simplest way to send a message:
csharp
var client = new SignalBotClient("http://localhost:8080");
await client.SendMessageAsync(
number: "+1234567890",
message: "Hello from Signal.Bot!",
recipients: new[] { "+0987654321" }
);1
2
3
4
5
6
7
2
3
4
5
6
7
Sending to Multiple Recipients
Send the same message to multiple people:
csharp
await client.SendMessageAsync(
number: "+1234567890",
message: "Broadcast message",
recipients: new[]
{
"+0987654321",
"+1111111111",
"+2222222222"
}
);1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
Sending to Groups
Send a message to a Signal group:
csharp
await client.SendMessageAsync(
number: "+1234567890",
message: "Hello everyone!",
groupId: "your-group-id-here"
);1
2
3
4
5
2
3
4
5
TIP
You can get the group ID by listing your groups with ListGroupsAsync. See the Groups guide for more details.
Messages with Attachments
Send messages with files attached:
csharp
// Send a single attachment
await client.SendMessageAsync(
number: "+1234567890",
message: "Check out this image!",
recipients: new[] { "+0987654321" },
attachments: new[] { "/path/to/image.jpg" }
);
// Send multiple attachments
await client.SendMessageAsync(
number: "+1234567890",
message: "Here are some files",
recipients: new[] { "+0987654321" },
attachments: new[]
{
"/path/to/document.pdf",
"/path/to/image.jpg",
"/path/to/video.mp4"
}
);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
WARNING
Make sure the file paths are absolute and accessible by the signal-cli-rest-api Docker container. You may need to mount volumes appropriately.
Messages with Mentions
Mention specific users in group chats:
csharp
await client.SendMessageAsync(
number: "+1234567890",
message: "Hey @user, check this out!",
groupId: "your-group-id",
mentions: new[]
{
new Mention
{
Number = "+0987654321",
Start = 4,
Length = 5
}
}
);1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
Quote Replies
Reply to a specific message:
csharp
await client.SendMessageAsync(
number: "+1234567890",
message: "I agree!",
recipients: new[] { "+0987654321" },
quoteTimestamp: originalMessageTimestamp,
quoteAuthor: "+0987654321",
quoteMessage: "Original message text"
);1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
Reactions
React to messages with emojis:
csharp
await client.SendReactionAsync(
number: "+1234567890",
recipient: "+0987654321",
reaction: "👍",
targetAuthor: "+0987654321",
targetTimestamp: messageTimestamp
);
// Remove a reaction
await client.SendReactionAsync(
number: "+1234567890",
recipient: "+0987654321",
reaction: "",
targetAuthor: "+0987654321",
targetTimestamp: messageTimestamp,
remove: true
);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Typing Indicators
Show typing indicators to let users know you're preparing a response:
csharp
// Start typing
await client.SendTypingIndicatorAsync(
number: "+1234567890",
recipient: "+0987654321"
);
// Simulate some processing
await Task.Delay(2000);
// Send the actual message
await client.SendMessageAsync(
number: "+1234567890",
message: "Here's your response!",
recipients: new[] { "+0987654321" }
);
// Stop typing (automatically stopped when message is sent)
await client.SendTypingIndicatorAsync(
number: "+1234567890",
recipient: "+0987654321",
stop: true
);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Read Receipts
Mark messages as read:
csharp
await client.SendReceiptAsync(
number: "+1234567890",
recipient: "+0987654321",
timestamps: new[] { messageTimestamp },
type: ReceiptType.Read
);1
2
3
4
5
6
2
3
4
5
6
Error Handling
Always handle potential errors when sending messages:
csharp
try
{
await client.SendMessageAsync(
number: "+1234567890",
message: "Hello!",
recipients: new[] { "+0987654321" }
);
Console.WriteLine("Message sent successfully!");
}
catch (HttpRequestException ex)
{
Console.WriteLine($"Network error: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Error sending message: {ex.Message}");
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Best Practices
1. Use Async/Await Properly
csharp
// ✅ Good
await client.SendMessageAsync(...);
// ❌ Bad - blocks the thread
client.SendMessageAsync(...).Wait();1
2
3
4
5
2
3
4
5
2. Handle Cancellation
csharp
var cts = new CancellationTokenSource();
try
{
await client.SendMessageAsync(
number: "+1234567890",
message: "Hello!",
recipients: new[] { "+0987654321" },
cancellationToken: cts.Token
);
}
catch (OperationCanceledException)
{
Console.WriteLine("Operation was cancelled");
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
3. Validate Phone Numbers
csharp
public bool IsValidPhoneNumber(string number)
{
// Must start with + and contain only digits after
return number.StartsWith("+") &&
number.Substring(1).All(char.IsDigit) &&
number.Length >= 10;
}
var recipient = "+0987654321";
if (IsValidPhoneNumber(recipient))
{
await client.SendMessageAsync(...);
}1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
4. Batch Operations Carefully
csharp
// Don't spam - add delays between messages
foreach (var recipient in recipients)
{
await client.SendMessageAsync(
number: botNumber,
message: "Broadcast",
recipients: new[] { recipient }
);
await Task.Delay(1000); // 1 second delay
}1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
Next Steps
- Learn about receiving messages
- Work with groups
- Explore attachments in detail
