Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test4 #39

Merged
merged 61 commits into from
Nov 3, 2023
Merged
Changes from 1 commit
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
fad0d50
Compiled main.go and pushed changes
Hoshinonyaruko Oct 23, 2023
fda2572
test
Hoshinonyaruko Oct 23, 2023
34cfac1
适配了频道私聊,用bolt数据库取代ini
Hoshinonyaruko Oct 23, 2023
7fc1f10
适配了nonebot2
Hoshinonyaruko Oct 24, 2023
28f9a73
Merge branch 'main' of https://github.com/Hoshinonyaruko/Gensokyo
Hoshinonyaruko Oct 24, 2023
9fadda4
适配私域
Hoshinonyaruko Oct 24, 2023
1336c6a
add license
Hoshinonyaruko Oct 24, 2023
b094949
add a lot
Hoshinonyaruko Oct 25, 2023
f4787f5
Resolved merge conflicts
Hoshinonyaruko Oct 25, 2023
86698c6
trss support
Hoshinonyaruko Oct 26, 2023
045f3e9
fix
Hoshinonyaruko Oct 26, 2023
618561c
add action
Hoshinonyaruko Oct 26, 2023
7e803aa
add action
Hoshinonyaruko Oct 26, 2023
ece4cef
add action
Hoshinonyaruko Oct 26, 2023
f864a50
fixbug
Hoshinonyaruko Oct 26, 2023
4564731
add wss
Hoshinonyaruko Oct 26, 2023
070e4df
merge
Hoshinonyaruko Oct 26, 2023
84e8fe9
bugfix
Hoshinonyaruko Oct 26, 2023
0e4fd50
fix action
Hoshinonyaruko Oct 26, 2023
fe2234a
fix action again
Hoshinonyaruko Oct 26, 2023
cf67a11
fix action againnn
Hoshinonyaruko Oct 26, 2023
0875837
fa
Hoshinonyaruko Oct 26, 2023
3a3c33f
fix
Hoshinonyaruko Oct 26, 2023
8789fa8
add a lot
Hoshinonyaruko Oct 27, 2023
7943466
add ws server token
Hoshinonyaruko Oct 28, 2023
4660983
merge
Hoshinonyaruko Oct 28, 2023
2a1026a
bugifx
Hoshinonyaruko Oct 28, 2023
4186970
fix
Hoshinonyaruko Oct 28, 2023
2f26828
fixat
Hoshinonyaruko Oct 28, 2023
007e1af
bugfix
Hoshinonyaruko Oct 28, 2023
c41fd77
bugfix
Hoshinonyaruko Oct 28, 2023
d8354b7
test
Hoshinonyaruko Oct 28, 2023
6e3c63c
test
Hoshinonyaruko Oct 28, 2023
f08d9d7
test2
Hoshinonyaruko Oct 28, 2023
4dd6e15
add url service
Hoshinonyaruko Oct 31, 2023
1f8c174
add url service
Hoshinonyaruko Oct 31, 2023
240f569
add url service
Hoshinonyaruko Oct 31, 2023
b191cb7
bugfix
Hoshinonyaruko Oct 31, 2023
42daab3
merge
Hoshinonyaruko Oct 31, 2023
8fec03f
fix
Hoshinonyaruko Oct 31, 2023
cca528b
fix
Hoshinonyaruko Oct 31, 2023
463a182
fix
Hoshinonyaruko Oct 31, 2023
e9187a1
bug fix
Hoshinonyaruko Nov 1, 2023
5ba2815
fix
Hoshinonyaruko Nov 1, 2023
c16e1af
fix
Hoshinonyaruko Nov 1, 2023
ec7863d
test
Hoshinonyaruko Nov 2, 2023
e8a7e73
ok
Hoshinonyaruko Nov 2, 2023
122d17d
add webui
Hoshinonyaruko Nov 2, 2023
4e9c209
merge
Hoshinonyaruko Nov 2, 2023
b502d61
add image_compress
Hoshinonyaruko Nov 3, 2023
3519e3e
merge
Hoshinonyaruko Nov 3, 2023
530ff96
bug fix
Hoshinonyaruko Nov 3, 2023
155a701
fix cq code
Hoshinonyaruko Nov 3, 2023
2f5ee4a
fixbug
Hoshinonyaruko Nov 3, 2023
afa03e3
bugfix
Hoshinonyaruko Nov 3, 2023
fdd1a73
merge
Hoshinonyaruko Nov 3, 2023
fe39cd2
bugfix
Hoshinonyaruko Nov 3, 2023
be08cbe
bugfix
Hoshinonyaruko Nov 3, 2023
931dfef
merge
Hoshinonyaruko Nov 3, 2023
73819c8
bugfix
Hoshinonyaruko Nov 3, 2023
b27f062
bugfix
Hoshinonyaruko Nov 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
适配了频道私聊,用bolt数据库取代ini
  • Loading branch information
Hoshinonyaruko committed Oct 23, 2023
commit 34cfac146145e90e6713e9847d218f986bf0ebc3
292 changes: 246 additions & 46 deletions Processor/Processor.go
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@
package Processor

import (
"context"
"encoding/json"
"fmt"
"log"
@@ -21,16 +20,8 @@ import (
"github.com/tencent-connect/botgo/websocket/client"
)

var compatibilityMapping *idmap.IniMapping
var err error

func init() {
compatibilityMapping, err = idmap.NewIniMapping()
if err != nil {
log.Fatalf("Failed to initialize IniMapping: %v", err)
}
}

// Processor 结构体用于处理消息
type Processor struct {
Api openapi.OpenAPI // API 类型
@@ -45,6 +36,7 @@ type Sender struct {
UserID int64 `json:"user_id"`
}

// 频道信息事件
type OnebotChannelMessage struct {
ChannelID string `json:"channel_id"`
GuildID string `json:"guild_id"`
@@ -63,7 +55,7 @@ type OnebotChannelMessage struct {
Echo string `json:"echo"`
}

// OnebotGroupMessage represents the message structure for group messages.
// 群信息事件
type OnebotGroupMessage struct {
RawMessage string `json:"raw_message"`
MessageID int `json:"message_id"`
@@ -82,6 +74,29 @@ type OnebotGroupMessage struct {
UserID int64 `json:"user_id"`
}

// 私聊信息事件
type OnebotPrivateMessage struct {
RawMessage string `json:"raw_message"`
MessageID int `json:"message_id"` // Can be either string or int depending on logic
MessageType string `json:"message_type"`
PostType string `json:"post_type"`
SelfID int64 `json:"self_id"` // Can be either string or int depending on logic
Sender PrivateSender `json:"sender"`
SubType string `json:"sub_type"`
Time int64 `json:"time"`
Avatar string `json:"avatar"`
Echo string `json:"echo"`
Message interface{} `json:"message"` // For array format
MessageSeq int `json:"message_seq,omitempty"` // Optional field
Font int `json:"font,omitempty"` // Optional field
UserID int64 `json:"user_id"` // Can be either string or int depending on logic
}

type PrivateSender struct {
Nickname string `json:"nickname"`
UserID int64 `json:"user_id"` // Can be either string or int depending on logic
}

func FoxTimestamp() int64 {
return time.Now().Unix()
}
@@ -157,9 +172,11 @@ func (p *Processor) ProcessGuildATMessage(data *dto.WSATMessageData) error {

} else {
// GlobalChannelToGroup为true时的处理逻辑
//将频道转化为一个群
//获取s
s := client.GetGlobalS()
compatibilityMapping.WriteConfig(data.ChannelID, "guild_id", data.GuildID)
//将channelid写入ini,可取出guild_id todo 比ini更好的储存方式
idmap.WriteConfig(data.ChannelID, "guild_id", data.GuildID)
//转换at
messageText := handlers.RevertTransformedText(data.Content)
//转换appid
@@ -247,7 +264,7 @@ func (p *Processor) ProcessGroupMessage(data *dto.WSGroupATMessageData) error {
// 获取s
s := client.GetGlobalS()

compatibilityMapping.WriteConfig(data.ChannelID, "guild_id", data.GuildID)
idmap.WriteConfig(data.ChannelID, "guild_id", data.GuildID)

// 转换at
messageText := handlers.RevertTransformedText(data.Content)
@@ -320,48 +337,231 @@ func (p *Processor) ProcessChannelDirectMessage(data *dto.WSDirectMessageData) e
// 打印data结构体
//PrintStructWithFieldNames(data)

// 从私信中提取必要的信息
// 从私信中提取必要的信息 这是测试回复需要用到
//recipientID := data.Author.ID
ChannelID := data.ChannelID
//ChannelID := data.ChannelID
//sourece是源头频道
GuildID := data.GuildID

// 创建私信通道 发主动私信才需要创建
// dm, err := p.Api.CreateDirectMessage(
// context.Background(), &dto.DirectMessageToCreate{
// SourceGuildID: sourceGuildID,
// RecipientID: recipientID,
// },
// )
// if err != nil {
// log.Println("Error creating direct message channel:", err)
// return nil
// }
//GuildID := data.GuildID

timestamp := time.Now().Unix() // 获取当前时间的int64类型的Unix时间戳
timestampStr := fmt.Sprintf("%d", timestamp)
//获取当前的s值 当前ws连接所收到的信息条数
s := client.GetGlobalS()
if !p.Settings.GlobalChannelToGroup {
// 把频道类型的私信转换成普通ob11的私信

dm := &dto.DirectMessage{
GuildID: GuildID,
ChannelID: ChannelID,
CreateTime: timestampStr,
}
//转换appidstring
AppIDString := strconv.FormatUint(p.Settings.AppID, 10)
echostr := AppIDString + "_" + strconv.FormatInt(s, 10)

PrintStructWithFieldNames(dm)
//将真实id转为int userid64
userid64, err := idmap.StoreID(data.Author.ID)
if err != nil {
log.Fatalf("Error storing ID: %v", err)
}
//将真实id写入数据库,可取出ChannelID
idmap.WriteConfig(data.Author.ID, "channel_id", data.ChannelID)
//将channelid写入数据库,可取出guild_id
idmap.WriteConfig(data.ChannelID, "guild_id", data.GuildID)

//收到私聊信息调用的具体还原步骤
//1,idmap还原真实userid,
//2,通过idmap获取channelid,
//3,通过idmap用channelid获取guildid,
//发信息使用的是guildid
//todo 优化数据库读写次数
messageID64, err := idmap.StoreID(data.ID)
if err != nil {
log.Fatalf("Error storing ID: %v", err)
}
messageID := int(messageID64)

privateMsg := OnebotPrivateMessage{
RawMessage: data.Content,
Message: data.Content,
MessageID: messageID,
MessageType: "private",
PostType: "message",
SelfID: int64(p.Settings.AppID),
UserID: userid64,
Sender: PrivateSender{
Nickname: data.Member.Nick,
UserID: userid64,
},
SubType: "friend",
Time: time.Now().Unix(),
Avatar: data.Author.Avatar,
Echo: echostr,
}

// 将当前s和appid和message进行映射
echo.AddMsgID(AppIDString, s, data.ID)
echo.AddMsgType(AppIDString, s, "guild_private")

// 调试
PrintStructWithFieldNames(privateMsg)

// Convert OnebotGroupMessage to map and send
privateMsgMap := structToMap(privateMsg)
err = p.Wsclient.SendMessage(privateMsgMap)
if err != nil {
return fmt.Errorf("error sending group message via wsclient: %v", err)
}
} else {
if !p.Settings.GlobalChannelToGroup {
//将频道私信作为普通频道信息

// 将时间字符串转换为时间戳
t, err := time.Parse(time.RFC3339, string(data.Timestamp))
if err != nil {
return fmt.Errorf("error parsing time: %v", err)
}
//获取s
s := client.GetGlobalS()
//转换at
messageText := handlers.RevertTransformedText(data.Content)
//转换appid
AppIDString := strconv.FormatUint(p.Settings.AppID, 10)
//构造echo
echostr := AppIDString + "_" + strconv.FormatInt(s, 10)
//映射str的userid到int
userid64, err := idmap.StoreID(data.Author.ID)
if err != nil {
log.Printf("Error storing ID: %v", err)
return nil
}
//OnebotChannelMessage
onebotMsg := OnebotChannelMessage{
ChannelID: data.ChannelID,
GuildID: data.GuildID,
Message: messageText,
RawMessage: messageText,
MessageID: data.ID,
MessageType: "guild",
PostType: "message",
SelfID: int64(p.Settings.AppID),
UserID: userid64,
SelfTinyID: "",
Sender: Sender{
Nickname: data.Member.Nick,
TinyID: "",
UserID: userid64,
},
SubType: "channel",
Time: t.Unix(),
Avatar: data.Author.Avatar,
Echo: echostr,
}

//将当前s和appid和message进行映射
echo.AddMsgID(AppIDString, s, data.ID)
//通过echo始终得知真实的事件类型,来对应调用正确的api
echo.AddMsgType(AppIDString, s, "guild_private")

//调试
PrintStructWithFieldNames(onebotMsg)

// 将 onebotMsg 结构体转换为 map[string]interface{}
msgMap := structToMap(onebotMsg)

// 使用 wsclient 发送消息
err = p.Wsclient.SendMessage(msgMap)
if err != nil {
return fmt.Errorf("error sending message via wsclient: %v", err)
}
} else {
//将频道信息转化为群信息(特殊需求情况下)
//将channelid写入ini,可取出guild_id
idmap.WriteConfig(data.ChannelID, "guild_id", data.GuildID)
//转换at
messageText := handlers.RevertTransformedText(data.Content)
//转换appid
AppIDString := strconv.FormatUint(p.Settings.AppID, 10)
//构造echo
echostr := AppIDString + "_" + strconv.FormatInt(s, 10)
//把频道号作为群号
channelIDInt, err := strconv.Atoi(data.ChannelID)
if err != nil {
// handle error, perhaps return it
return fmt.Errorf("failed to convert ChannelID to int: %v", err)
}
//映射str的userid到int
userid64, err := idmap.StoreID(data.Author.ID)
if err != nil {
log.Printf("Error storing ID: %v", err)
return nil
}
//userid := int(userid64)
//映射str的messageID到int
messageID64, err := idmap.StoreID(data.ID)
if err != nil {
log.Printf("Error storing ID: %v", err)
return nil
}
messageID := int(messageID64)
//todo 判断array模式 然后对Message处理成array格式
groupMsg := OnebotGroupMessage{
RawMessage: messageText,
Message: messageText,
MessageID: messageID,
GroupID: int64(channelIDInt),
MessageType: "group",
PostType: "message",
SelfID: int64(p.Settings.AppID),
UserID: userid64,
Sender: Sender{
Nickname: data.Member.Nick,
UserID: userid64,
},
SubType: "normal",
Time: time.Now().Unix(),
Avatar: data.Author.Avatar,
Echo: echostr,
}
//将当前s和appid和message进行映射
echo.AddMsgID(AppIDString, s, data.ID)
echo.AddMsgType(AppIDString, s, "guild_private")

//调试
PrintStructWithFieldNames(groupMsg)

// Convert OnebotGroupMessage to map and send
groupMsgMap := structToMap(groupMsg)
err = p.Wsclient.SendMessage(groupMsgMap)
if err != nil {
return fmt.Errorf("error sending group message via wsclient: %v", err)
}
}

// 发送默认回复
toCreate := &dto.MessageToCreate{
Content: "默认私信回复",
MsgID: data.ID,
}
_, err = p.Api.PostDirectMessage(
context.Background(), dm, toCreate,
)
if err != nil {
log.Println("Error sending default reply:", err)
return nil
}

//return nil

//下面是测试时候固定代码
//发私信给机器人4条机器人不回,就不能继续发了

// timestamp := time.Now().Unix() // 获取当前时间的int64类型的Unix时间戳
// timestampStr := fmt.Sprintf("%d", timestamp)

// dm := &dto.DirectMessage{
// GuildID: GuildID,
// ChannelID: ChannelID,
// CreateTime: timestampStr,
// }

// PrintStructWithFieldNames(dm)

// // 发送默认回复
// toCreate := &dto.MessageToCreate{
// Content: "默认私信回复",
// MsgID: data.ID,
// }
// _, err = p.Api.PostDirectMessage(
// context.Background(), dm, toCreate,
// )
// if err != nil {
// log.Println("Error sending default reply:", err)
// return nil
// }

return nil
}

3 changes: 1 addition & 2 deletions botgo/openapi/v2/openapi.go
Original file line number Diff line number Diff line change
@@ -93,8 +93,7 @@ func (o *openAPIv2) setupClient() {
func(c *resty.Client, r *resty.Request) error {
// 设置授权方案为 "QQBot"
c.SetAuthScheme("QQBot")
c.SetAuthToken("Sv0g5ZqFKn1E7iSBYxQzzWb7ky0X-W6P6QtGRJy1cgPm8bqGLMl73b9_72kyR9y1mBE-OvXsBMpA")
//c.SetAuthToken(o.token.GetAccessToken())
c.SetAuthToken(o.token.GetAccessToken())
return nil
},
).
4 changes: 2 additions & 2 deletions botgo/token/authtoken.go
Original file line number Diff line number Diff line change
@@ -96,11 +96,11 @@ func (atoken *AuthTokenInfo) StartRefreshAccessToken(ctx context.Context, tokenU
return
}

//测试用
// 测试用
// func (atoken *AuthTokenInfo) StartRefreshAccessToken(ctx context.Context, tokenURL, appID, clientSecrent string) (err error) {
// // 创建一个固定的token信息
// fixedTokenInfo := AccessTokenInfo{
// Token: "Sv0g5ZqFKn1E7iSBYxQzzWb7ky0X-W6P6QtGRJy1cgPm8bqGLMl73b9_72kyR9y1mBE-OvXsBMpA",
// Token: "SMvPWUBuXwSyAj1UmvTKVcK7D0iEaBrmbTKVNaJFMk9S5RmpgnGvNOOOTvrqG64NhuER-e-jK2IT",
// ExpiresIn: 3600, // 这里假设token的有效时间是3600秒,你可以根据需要调整
// }
// atoken.setAuthToken(fixedTokenInfo)
2 changes: 1 addition & 1 deletion botgo/token/token.go
Original file line number Diff line number Diff line change
@@ -98,7 +98,7 @@ func (t *Token) GetAccessToken() string {
// GetAccessToken 取得测试鉴权Token
// func (t *Token) GetAccessToken() string {
// // 固定的token值
// return "Sv0g5ZqFKn1E7iSBYxQzzWb7ky0X-W6P6QtGRJy1cgPm8bqGLMl73b9_72kyR9y1mBE-OvXsBMpA"
// return "SMvPWUBuXwSyAj1UmvTKVcK7D0iEaBrmbTKVNaJFMk9S5RmpgnGvNOOOTvrqG64NhuER-e-jK2IT"
// }

// UpAccessToken 更新accessToken
Loading
Oops, something went wrong.