Skip to content

Commit 8e64290

Browse files
ernadoclaude
andcommitted
feat(send): implement SendDice/Location/Venue/Contact/Poll
A shared sendMedia helper applies the send options and dispatches a message.MediaOption. SendDice (defaults to the die), SendLocation, SendVenue, SendContact and SendPoll (>=2 options) build on it. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 026f73b commit 8e64290

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

send_other.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package botapi
2+
3+
import (
4+
"context"
5+
6+
"github.com/gotd/td/telegram/message"
7+
"github.com/gotd/td/tg"
8+
)
9+
10+
// sendMedia applies the send options to a builder and sends a media message.
11+
func (b *Bot) sendMedia(ctx context.Context, chat ChatID, media message.MediaOption, opts ...SendOption) (*Message, error) {
12+
var cfg sendConfig
13+
for _, o := range opts {
14+
o(&cfg)
15+
}
16+
17+
peer, err := b.resolveInputPeer(ctx, chat)
18+
if err != nil {
19+
return nil, err
20+
}
21+
22+
builder := &b.sender.To(peer).Builder
23+
builder, err = b.applySendConfig(builder, cfg)
24+
if err != nil {
25+
return nil, err
26+
}
27+
28+
resp, err := builder.Media(ctx, media)
29+
return b.sentMessage(ctx, peer, resp, err)
30+
}
31+
32+
// SendDice sends an animated emoji with a random value. A zero emoji defaults
33+
// to the die (🎲).
34+
func (b *Bot) SendDice(ctx context.Context, chat ChatID, emoji DiceEmoji, opts ...SendOption) (*Message, error) {
35+
if emoji == "" {
36+
emoji = DiceDie
37+
}
38+
return b.sendMedia(ctx, chat, message.MediaDice(string(emoji)), opts...)
39+
}
40+
41+
// SendLocation sends a point on the map.
42+
func (b *Bot) SendLocation(ctx context.Context, chat ChatID, latitude, longitude float64, opts ...SendOption) (*Message, error) {
43+
return b.sendMedia(ctx, chat, message.GeoPoint(latitude, longitude, 0), opts...)
44+
}
45+
46+
// SendVenue sends information about a venue.
47+
func (b *Bot) SendVenue(
48+
ctx context.Context, chat ChatID, latitude, longitude float64, title, address string, opts ...SendOption,
49+
) (*Message, error) {
50+
return b.sendMedia(ctx, chat, message.Venue(latitude, longitude, 0, title, address), opts...)
51+
}
52+
53+
// SendContact sends a phone contact.
54+
func (b *Bot) SendContact(ctx context.Context, chat ChatID, phoneNumber, firstName, lastName string, opts ...SendOption) (*Message, error) {
55+
return b.sendMedia(ctx, chat, message.Contact(tg.InputMediaContact{
56+
PhoneNumber: phoneNumber,
57+
FirstName: firstName,
58+
LastName: lastName,
59+
}), opts...)
60+
}
61+
62+
// SendPoll sends a native poll. At least two options are required.
63+
func (b *Bot) SendPoll(ctx context.Context, chat ChatID, question string, options []string, opts ...SendOption) (*Message, error) {
64+
if len(options) < 2 {
65+
return nil, &Error{Code: 400, Description: "Bad Request: poll must have at least 2 option"}
66+
}
67+
answers := make([]message.PollAnswerOption, len(options))
68+
for i, o := range options {
69+
answers[i] = message.PollAnswer(o)
70+
}
71+
poll := message.Poll(question, answers[0], answers[1], answers[2:]...)
72+
return b.sendMedia(ctx, chat, poll, opts...)
73+
}

0 commit comments

Comments
 (0)