2017-11-18 13:06:20 +00:00
|
|
|
package telebot
|
|
|
|
|
2021-11-05 20:31:32 +00:00
|
|
|
import (
|
2021-11-07 23:20:05 +00:00
|
|
|
"encoding/json"
|
2021-11-05 20:31:32 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
2017-11-18 13:06:20 +00:00
|
|
|
|
2020-04-26 11:13:06 +00:00
|
|
|
// User object represents a Telegram user, bot.
|
2017-11-18 13:06:20 +00:00
|
|
|
type User struct {
|
2021-07-27 12:12:10 +00:00
|
|
|
ID int64 `json:"id"`
|
2017-11-18 13:06:20 +00:00
|
|
|
|
2019-01-27 19:00:48 +00:00
|
|
|
FirstName string `json:"first_name"`
|
|
|
|
LastName string `json:"last_name"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
LanguageCode string `json:"language_code"`
|
2019-05-19 22:36:15 +00:00
|
|
|
IsBot bool `json:"is_bot"`
|
2020-04-26 11:13:06 +00:00
|
|
|
|
|
|
|
// Returns only in getMe
|
|
|
|
CanJoinGroups bool `json:"can_join_groups"`
|
|
|
|
CanReadMessages bool `json:"can_read_all_group_messages"`
|
|
|
|
SupportsInline bool `json:"supports_inline_queries"`
|
2017-11-18 13:06:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Recipient returns user ID (see Recipient interface).
|
|
|
|
func (u *User) Recipient() string {
|
2021-07-27 12:12:10 +00:00
|
|
|
return strconv.FormatInt(u.ID, 10)
|
2017-11-18 13:06:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Chat object represents a Telegram user, bot, group or a channel.
|
|
|
|
type Chat struct {
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
|
2020-04-26 15:19:49 +00:00
|
|
|
// See ChatType and consts.
|
2017-11-18 13:06:20 +00:00
|
|
|
Type ChatType `json:"type"`
|
|
|
|
|
|
|
|
// Won't be there for ChatPrivate.
|
|
|
|
Title string `json:"title"`
|
|
|
|
|
|
|
|
FirstName string `json:"first_name"`
|
|
|
|
LastName string `json:"last_name"`
|
|
|
|
Username string `json:"username"`
|
2020-04-26 11:13:06 +00:00
|
|
|
|
2020-04-26 15:41:52 +00:00
|
|
|
// Still shows whether the user is a member
|
|
|
|
// of the chat at the moment of the request.
|
|
|
|
Still bool `json:"is_member,omitempty"`
|
|
|
|
|
2020-04-26 11:13:06 +00:00
|
|
|
// Returns only in getChat
|
2021-05-23 16:55:49 +00:00
|
|
|
Bio string `json:"bio,omitempty"`
|
|
|
|
Photo *ChatPhoto `json:"photo,omitempty"`
|
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
InviteLink string `json:"invite_link,omitempty"`
|
|
|
|
PinnedMessage *Message `json:"pinned_message,omitempty"`
|
|
|
|
Permissions *Rights `json:"permissions,omitempty"`
|
|
|
|
SlowMode int `json:"slow_mode_delay,omitempty"`
|
|
|
|
StickerSet string `json:"sticker_set_name,omitempty"`
|
|
|
|
CanSetStickerSet bool `json:"can_set_sticker_set,omitempty"`
|
|
|
|
LinkedChatID int64 `json:"linked_chat_id,omitempty"`
|
|
|
|
ChatLocation *ChatLocation `json:"location,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ChatLocation struct {
|
|
|
|
Location Location `json:"location,omitempty"`
|
|
|
|
Address string `json:"address,omitempty"`
|
2020-04-26 11:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ChatPhoto object represents a chat photo.
|
|
|
|
type ChatPhoto struct {
|
|
|
|
// File identifiers of small (160x160) chat photo
|
2020-06-19 20:34:37 +00:00
|
|
|
SmallFileID string `json:"small_file_id"`
|
|
|
|
SmallUniqueID string `json:"small_file_unique_id"`
|
2020-04-26 11:13:06 +00:00
|
|
|
|
|
|
|
// File identifiers of big (640x640) chat photo
|
2020-06-19 20:34:37 +00:00
|
|
|
BigFileID string `json:"big_file_id"`
|
|
|
|
BigUniqueID string `json:"big_file_unique_id"`
|
2017-11-18 13:06:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Recipient returns chat ID (see Recipient interface).
|
|
|
|
func (c *Chat) Recipient() string {
|
|
|
|
return strconv.FormatInt(c.ID, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChatMember object represents information about a single chat member.
|
|
|
|
type ChatMember struct {
|
2017-11-21 15:28:22 +00:00
|
|
|
Rights
|
|
|
|
|
2021-05-23 16:55:49 +00:00
|
|
|
User *User `json:"user"`
|
|
|
|
Role MemberStatus `json:"status"`
|
|
|
|
Title string `json:"custom_title"`
|
|
|
|
Anonymous bool `json:"is_anonymous"`
|
2017-11-21 15:28:22 +00:00
|
|
|
|
|
|
|
// Date when restrictions will be lifted for the user, unix time.
|
|
|
|
//
|
|
|
|
// If user is restricted for more than 366 days or less than
|
|
|
|
// 30 seconds from the current time, they are considered to be
|
|
|
|
// restricted forever.
|
|
|
|
//
|
2020-07-25 21:01:39 +00:00
|
|
|
// Use tele.Forever().
|
2017-11-21 15:28:22 +00:00
|
|
|
//
|
|
|
|
RestrictedUntil int64 `json:"until_date,omitempty"`
|
2017-11-18 13:06:20 +00:00
|
|
|
}
|
2020-05-05 19:24:40 +00:00
|
|
|
|
|
|
|
// ChatID represents a chat or an user integer ID, which can be used
|
|
|
|
// as recipient in bot methods. It is very useful in cases where
|
|
|
|
// you have special group IDs, for example in your config, and don't
|
2020-07-25 21:01:39 +00:00
|
|
|
// want to wrap it into *tele.Chat every time you send messages.
|
2020-05-05 19:24:40 +00:00
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2020-07-25 21:01:39 +00:00
|
|
|
// group := tele.ChatID(-100756389456)
|
2020-05-05 19:24:40 +00:00
|
|
|
// b.Send(group, "Hello!")
|
|
|
|
//
|
|
|
|
// type Config struct {
|
2020-07-25 21:01:39 +00:00
|
|
|
// AdminGroup tele.ChatID `json:"admin_group"`
|
2020-05-05 19:24:40 +00:00
|
|
|
// }
|
|
|
|
// b.Send(conf.AdminGroup, "Hello!")
|
|
|
|
//
|
|
|
|
type ChatID int64
|
|
|
|
|
|
|
|
// Recipient returns chat ID (see Recipient interface).
|
|
|
|
func (i ChatID) Recipient() string {
|
|
|
|
return strconv.FormatInt(int64(i), 10)
|
|
|
|
}
|
2021-11-05 20:31:32 +00:00
|
|
|
|
|
|
|
// ChatJoinRequest represents a join request sent to a chat.
|
|
|
|
type ChatJoinRequest struct {
|
|
|
|
// Chat to which the request was sent.
|
|
|
|
Chat *Chat `json:"chat"`
|
|
|
|
|
|
|
|
// Sender is the user that sent the join request.
|
2021-11-05 20:40:51 +00:00
|
|
|
Sender *User `json:"from"`
|
2021-11-05 20:31:32 +00:00
|
|
|
|
|
|
|
// Unixtime, use ChatJoinRequest.Time() to get time.Time.
|
|
|
|
Unixtime int64 `json:"date"`
|
|
|
|
|
|
|
|
// Bio of the user, optional.
|
|
|
|
Bio string `json:"bio"`
|
|
|
|
|
|
|
|
// InviteLink is the chat invite link that was used by
|
|
|
|
//the user to send the join request, optional.
|
|
|
|
InviteLink *ChatInviteLink `json:"invite_link"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Time returns the moment of chat join request sending in local time.
|
|
|
|
func (r ChatJoinRequest) Time() time.Time {
|
|
|
|
return time.Unix(r.Unixtime, 0)
|
|
|
|
}
|
2021-11-07 23:20:05 +00:00
|
|
|
|
|
|
|
// CreateInviteLink creates an additional invite link for a chat.
|
|
|
|
func (b *Bot) CreateInviteLink(chat Recipient, link *ChatInviteLink) (*ChatInviteLink, error) {
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": chat.Recipient(),
|
|
|
|
}
|
|
|
|
if link != nil {
|
|
|
|
params["name"] = link.Name
|
|
|
|
|
|
|
|
if link.ExpireUnixtime != 0 {
|
|
|
|
params["expire_date"] = strconv.FormatInt(link.ExpireUnixtime, 10)
|
|
|
|
}
|
|
|
|
if link.MemberLimit > 0 {
|
|
|
|
params["member_limit"] = strconv.Itoa(link.MemberLimit)
|
|
|
|
} else if link.JoinRequest {
|
|
|
|
params["creates_join_request"] = "true"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := b.Raw("createChatInviteLink", params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp struct {
|
|
|
|
Result ChatInviteLink `json:"result"`
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
|
|
return nil, wrapError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &resp.Result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EditInviteLink edits a non-primary invite link created by the bot.
|
|
|
|
func (b *Bot) EditInviteLink(chat Recipient, link *ChatInviteLink) (*ChatInviteLink, error) {
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": chat.Recipient(),
|
|
|
|
}
|
|
|
|
if link != nil {
|
|
|
|
params["invite_link"] = link.InviteLink
|
|
|
|
params["name"] = link.Name
|
|
|
|
|
|
|
|
if link.ExpireUnixtime != 0 {
|
|
|
|
params["expire_date"] = strconv.FormatInt(link.ExpireUnixtime, 10)
|
|
|
|
}
|
|
|
|
if link.MemberLimit > 0 {
|
|
|
|
params["member_limit"] = strconv.Itoa(link.MemberLimit)
|
|
|
|
} else if link.JoinRequest {
|
|
|
|
params["creates_join_request"] = "true"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := b.Raw("editChatInviteLink", params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp struct {
|
|
|
|
Result ChatInviteLink `json:"result"`
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
|
|
return nil, wrapError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &resp.Result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RevokeInviteLink revokes an invite link created by the bot.
|
|
|
|
func (b *Bot) RevokeInviteLink(chat Recipient, link string) (*ChatInviteLink, error) {
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": chat.Recipient(),
|
|
|
|
"invite_link": link,
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := b.Raw("revokeChatInviteLink", params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp struct {
|
|
|
|
Result ChatInviteLink `json:"result"`
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
|
|
return nil, wrapError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &resp.Result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApproveChatJoinRequest approves a chat join request.
|
|
|
|
func (b *Bot) ApproveChatJoinRequest(chat Recipient, user *User) error {
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": chat.Recipient(),
|
|
|
|
"user_id": user.Recipient(),
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := b.Raw("approveChatJoinRequest", params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return extractOk(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeclineChatJoinRequest declines a chat join request.
|
|
|
|
func (b *Bot) DeclineChatJoinRequest(chat Recipient, user *User) error {
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": chat.Recipient(),
|
|
|
|
"user_id": user.Recipient(),
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := b.Raw("declineChatJoinRequest", params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return extractOk(data)
|
|
|
|
}
|