lp.MessageNew(func(_ context.Context, obj events.MessageNewObject) {
var admins = 380236476
var event = 2000000001
if obj.Message.FromID == admins{
if obj.Message.PeerID == event {
b := params.NewMessagesDeleteBuilder()
b.DeleteForAll(true)
b.MessageIDs(obj.Message.ID)
_, err := vk.MessagesDelete(b.Params)
if err != nil {
log.Fatal(err)
}
}
}
})
./main.go:68:18: cannot use test (type int) as type []int in argument to b.MessageIDs
package main
import (
"context"
"log"
"github.com/SevereCloud/vksdk/v2/api"
"github.com/SevereCloud/vksdk/v2/api/params"
"github.com/SevereCloud/vksdk/v2/longpoll-bot"
"github.com/SevereCloud/vksdk/v2/events"
)
func main() {
token := "token"
vk := api.NewVK(token)
group, err := vk.GroupsGetByID(api.Params{})
if err != nil {
log.Fatal(err)
}
lp, err := longpoll.NewLongPoll(vk, group[0].ID)
if err != nil {
log.Fatal(err)
}
lp.MessageNew(func(_ context.Context, obj events.MessageNewObject) {
var admins = 380236476
var event = 2000000001
if obj.Message.FromID == admins{
if obj.Message.PeerID == event {
b := params.NewMessagesDeleteBuilder()
b.DeleteForAll(true)
b.MessageIDs([]int{obj.Message.ID})
_, err := vk.MessagesDelete(b.Params)
if err != nil {
log.Fatal(err)
}
}
}
})
log.Println("Start longpoll")
if err := lp.Run(); err != nil {
log.Fatal(err)
}
}
fmt.Printf("obj.Message.ID: %v\n", obj.Message.ID)
b.MessageIDs([]int{obj.Message.ID})
// MessageIDs parameter.
func (b *MessagesDeleteBuilder) MessageIDs(v []int) *MessagesDeleteBuilder {
b.Params["message_ids"] = v
return b
}
fmt.Printf("b.Params: %+v\n", b.Params)
lp.MessageNew(func(_ context.Context, obj events.MessageNewObject) {
var admins = 380236476
var event = 2000000001
if obj.Message.FromID == admins{
if obj.Message.PeerID == event {
b := params.NewMessagesDeleteBuilder()
b.DeleteForAll(true)
b.MessageIDs([]int{obj.Message.ConversationMessageID})
_, err := vk.MessagesDelete(b.Params)
if err != nil {
log.Fatal(err)
}
}
}
})
2021/05/23 01:57:47 api: Access denied: message can not be found
(object.MessagesMessage) {
AdminAuthorID: (int) 0,
Action: (object.MessagesMessageAction) {
ConversationMessageID: (int) 0,
Email: (string) "",
MemberID: (int) 0,
Message: (string) "",
Photo: (object.MessagesMessageActionPhoto) {
Photo100: (string) "",
Photo200: (string) "",
Photo50: (string) ""
},
Text: (string) "",
Type: (string) ""
},
Attachments: ([]object.MessagesMessageAttachment) {
},
ConversationMessageID: (int) 15116,
Date: (int) 1621701349,
FromID: (int) 259189762,
FwdMessages: ([]object.MessagesMessage) {
},
ReplyMessage: (*object.MessagesMessage)(),
Geo: (object.BaseMessageGeo) {
Coordinates: (object.BaseGeoCoordinates) {
Latitude: (float64) 0,
Longitude: (float64) 0
},
Place: (object.BasePlace) {
Address: (string) "",
Checkins: (int) 0,
City: (interface {}) ,
Country: (interface {}) ,
Created: (int) 0,
ID: (int) 0,
Icon: (string) "",
Latitude: (float64) 0,
Longitude: (float64) 0,
Title: (string) "",
Type: (string) "",
IsDeleted: (object.BaseBoolInt) false,
TotalCheckins: (int) 0,
Updated: (int) 0,
CategoryObject: (object.BaseCategoryObject) {
ID: (int) 0,
Title: (string) "",
Icons: ([]object.BaseImage)
}
},
Showmap: (int) 0,
Type: (string) ""
},
PinnedAt: (int) 0,
ID: (int) 0,
Deleted: (object.BaseBoolInt) false,
Important: (object.BaseBoolInt) false,
IsHidden: (object.BaseBoolInt) false,
IsCropped: (object.BaseBoolInt) false,
Out: (object.BaseBoolInt) false,
WasListened: (object.BaseBoolInt) false,
Keyboard: (object.MessagesKeyboard) {
AuthorID: (int) 0,
Buttons: ([][]object.MessagesKeyboardButton) ,
OneTime: (object.BaseBoolInt) false,
Inline: (object.BaseBoolInt) false
},
Template: (object.MessagesTemplate) {
Type: (string) "",
Elements: ([]object.MessagesTemplateElement)
},
Payload: (string) "",
PeerID: (int) 2000000001,
RandomID: (int) 0,
Ref: (string) "",
RefSource: (string) "",
Text: (string) (len=26) "test",
UpdateTime: (int) 0,
MembersCount: (int) 0,
ExpireTTL: (int) 0
}
lp.MessageNew(func(_ context.Context, obj events.MessageNewObject) {
var admins = 380236475
var event = 2000000001
if obj.Message.FromID != admins{
if obj.Message.PeerID == event {
b := params.NewMessagesDeleteBuilder()
b.DeleteForAll(true)
b.MessageIDs([]int{obj.Message.ID})
_, err := vk.MessagesDelete(b.Params)
if err != nil {
log.Fatal(err)
}
}
}
})
2021/05/23 14:03:38 api: One of the parameters specified was missing or invalid: message_ids or conversation_message_ids is undefined
2021/05/23 14:05:58 api: Access denied: message can not be found
Здравствуйте, Данил!
У таких сообщений нет глобальных id. Для бота нужно использовать поле conversation_message_id и метод https://vk.com/dev/messages.getByConversationMessageId, чтобы получить такое сообщение. В https://vk.com/dev/messages.delete соответственно тоже есть поле conversation_message_ids, нужно использовать именно его.
С уважением,
Команда ВКонтакте
// исходя из логики в их мануале и того, что там есть
// NewMessagesGetByConversationMessageIDBuilder и NewMessagesDeleteBuilder
// по идее должен быть или NewMessagesDeleteByConversationMessageIDBuilder
// или MessagesDeleteBuilder должен быть метод ConversationMessageIDs
// Но нет ни того, ни другого.
// Возможно еще не сделали :)
// Будем работать с тем, что есть.
b := params.NewMessagesDeleteBuilder()
b.DeleteForAll(true)
// судя по их мануалу https://vk.com/dev/messages.delete
// они понимают параметр conversation_message_ids
// пропишем этот параметр в params сами, раз у MessagesDeleteBuilder этого параметра нет.
params = b.Params
params["conversation_message_ids"] = []int{obj.Message.ConversationMessageID}
_, err := vk.MessagesDelete(params)
./main.go:56:10: use of package params without selector
./main.go:57:9: use of package params without selector
./main.go:59:30: use of package params without selector
https://pkg.go.dev/github.com/SevereCloud/vksdk/v2...
api: One of the parameters specified was missing or invalid: message_ids or conversation_message_ids is undefined
2021/05/28 17:30:32 api: One of the parameters specified was missing or invalid: conversation_message_ids requests peer_id parameter
2021/05/28 17:59:02 map[conversation_message_ids:[15382] delete_for_all:true]
2021/05/28 17:59:02 api: One of the parameters specified was missing or invalid: conversation_message_ids requests peer_id parameter
lp.MessageNew(func(_ context.Context, obj events.MessageNewObject) {
b := params.NewMessagesGetByConversationMessageIDBuilder()
if obj.Message.FromID != 380236476 {
b.PeerID(2000000001)
b.ConversationMessageIDs([]int{obj.Message.ConversationMessageID})
_, err := vk.MessagesDelete(b.Params)
// log.Printf("%s, %s" , b.Params)
if err != nil {
log.Fatal(err)
}
}
})
2021/05/28 22:07:50 api: Access denied: message can not be deleted (3)
exit status 1
map[conversation_message_ids:[%!s(int=15510)] peer_id:%!s(int=2000000001)], %!s(MISSING)
getParamsBuilder := params.NewMessagesGetByConversationMessageIDBuilder()
getParamsBuilder.ConversationMessageIDs([]int{obj.Message.ConversationMessageID})
response, err := vk.MessagesGetByConversationMessageID(getParamsBuilder.Params)
// if err != nil ...
ids := []int{}
for _, item := range response.Items {
fmt.Printf("%+v\n\n", item) // посмотрите что вообще приходит и что там за данные
ids = append(ids, item.ID)
}
delParamsBuilder := params.NewMessagesDeleteBuilder()
delParamsBuilder.DeleteForAll(true)
delParamsBuilder.MessageIDs(ids)
_, err := vk.MessagesDelete(delParamsBuilder.Params)
./funcs_vk.go:101:12: no new variables on left side of :=
_, err := vk.MessagesDelete(delParamsBuilder.Params)
{AdminAuthorID:0 Action:{ConversationMessageID:0 Email: MemberID:0 Message: Photo:{Photo100: Photo200: Photo50:} Text: Type:} Attachments:[] ConversationMessageID:15533 Date:1622211865 FromID:380236476 FwdMessages:[] ReplyMessage: Geo:{Coordinates:{Latitude:0 Longitude:0} Place:{Address: Checkins:0 City: Country: Created:0 ID:0 Icon: Latitude:0 Longitude:0 Title: Type: IsDeleted:false TotalCheckins:0 Updated:0 CategoryObject:{ID:0 Title: Icons:[]}} Showmap:0 Type:} PinnedAt:0 ID:0 Deleted:false Important:false IsHidden:false IsCropped:false Out:false WasListened:false Keyboard:{AuthorID:0 Buttons:[] OneTime:false Inline:false} Template:{Type: Elements:[]} Payload: PeerID:2000000001 RandomID:0 Ref: RefSource: Text:test UpdateTime:0 MembersCount:0 ExpireTTL:0}
(api.MessagesGetByConversationMessageIDResponse) {
Count: (int) 1,
Items: ([]object.MessagesMessage) (len=1 cap=4) {
(object.MessagesMessage) {
AdminAuthorID: (int) 0,
Action: (object.MessagesMessageAction) {
ConversationMessageID: (int) 0,
Email: (string) "",
MemberID: (int) 0,
Message: (string) "",
Photo: (object.MessagesMessageActionPhoto) {
Photo100: (string) "",
Photo200: (string) "",
Photo50: (string) ""
},
Text: (string) "",
Type: (string) ""
},
Attachments: ([]object.MessagesMessageAttachment) {
},
ConversationMessageID: (int) 15534,
Date: (int) 1622212093,
FromID: (int) 380236476,
FwdMessages: ([]object.MessagesMessage) {
},
ReplyMessage: (*object.MessagesMessage)(),
Geo: (object.BaseMessageGeo) {
Coordinates: (object.BaseGeoCoordinates) {
Latitude: (float64) 0,
Longitude: (float64) 0
},
Place: (object.BasePlace) {
Address: (string) "",
Checkins: (int) 0,
City: (interface {}) ,
Country: (interface {}) ,
Created: (int) 0,
ID: (int) 0,
Icon: (string) "",
Latitude: (float64) 0,
Longitude: (float64) 0,
Title: (string) "",
Type: (string) "",
IsDeleted: (object.BaseBoolInt) false,
TotalCheckins: (int) 0,
Updated: (int) 0,
CategoryObject: (object.BaseCategoryObject) {
ID: (int) 0,
Title: (string) "",
Icons: ([]object.BaseImage)
}
},
Showmap: (int) 0,
Type: (string) ""
},
PinnedAt: (int) 0,
ID: (int) 0,
Deleted: (object.BaseBoolInt) false,
Important: (object.BaseBoolInt) false,
IsHidden: (object.BaseBoolInt) false,
IsCropped: (object.BaseBoolInt) false,
Out: (object.BaseBoolInt) false,
WasListened: (object.BaseBoolInt) false,
Keyboard: (object.MessagesKeyboard) {
AuthorID: (int) 0,
Buttons: ([][]object.MessagesKeyboardButton) ,
OneTime: (object.BaseBoolInt) false,
Inline: (object.BaseBoolInt) false
},
Template: (object.MessagesTemplate) {
Type: (string) "",
Elements: ([]object.MessagesTemplateElement)
},
Payload: (string) "",
PeerID: (int) 2000000001,
RandomID: (int) 0,
Ref: (string) "",
RefSource: (string) "",
Text: (string) (len=4) "test",
UpdateTime: (int) 0,
MembersCount: (int) 0,
ExpireTTL: (int) 0
}
},
ExtendedResponse: (object.ExtendedResponse) {
Profiles: ([]object.UsersUser) ,
Groups: ([]object.GroupsGroup)
}
}
lp.MessageNew(func(_ context.Context, obj events.MessageNewObject) {
if obj.Message.FromID != 380236476 {
users, err := vk.MessagesDelete(api.Params{
"conversation_message_ids": obj.Message.ConversationMessageID,
"delete_for_all": 1,
"peer_id": 2000000001,
})
if err != nil {
log.Fatal(err)
}
log.Print(users)
}
})
2021/05/30 14:16:46 map[15545:1]
lp.MessageNew(func(_ context.Context, obj events.MessageNewObject) {
if obj.Message.Text == "test" {
admins, err := vk.MessagesGetConversationMembers(api.Params{
"peer_id": 2000000001,
})
if err != nil {
log.Fatal(err)
}
spew.Dump(admins.Items)
}})
([]struct { MemberID int "json:\"member_id\""; JoinDate int "json:\"join_date\""; InvitedBy int "json:\"invited_by\""; IsOwner object.BaseBoolInt "json:\"is_owner,omitempty\""; IsAdmin object.BaseBoolInt "json:\"is_admin,omitempty\""; CanKick object.BaseBoolInt "json:\"can_kick,omitempty\"" }) (len=16 cap=19) {
(struct { MemberID int "json:\"member_id\""; JoinDate int "json:\"join_date\""; InvitedBy int "json:\"invited_by\""; IsOwner object.BaseBoolInt "json:\"is_owner,omitempty\""; IsAdmin object.BaseBoolInt "json:\"is_admin,omitempty\""; CanKick object.BaseBoolInt "json:\"can_kick,omitempty\"" }) {
MemberID: (int) 380236476,
JoinDate: (int) 1610476128,
InvitedBy: (int) 380236476,
IsOwner: (object.BaseBoolInt) true,
IsAdmin: (object.BaseBoolInt) true,
CanKick: (object.BaseBoolInt) false
},