github.com/LagrangeDev/LagrangeGo@v0.0.0-20240512064304-ad4a85e10cb4/main.go (about)

     1  // nolint
     2  package main
     3  
     4  import (
     5  	"fmt"
     6  	"os"
     7  	"os/signal"
     8  	"path"
     9  	"strings"
    10  	"syscall"
    11  	"time"
    12  
    13  	"github.com/LagrangeDev/LagrangeGo/client"
    14  	"github.com/LagrangeDev/LagrangeGo/client/auth"
    15  	"github.com/LagrangeDev/LagrangeGo/message"
    16  	"github.com/LagrangeDev/LagrangeGo/utils"
    17  	"github.com/mattn/go-colorable"
    18  	"github.com/sirupsen/logrus"
    19  )
    20  
    21  var (
    22  	dumpspath = "dump"
    23  )
    24  
    25  func main() {
    26  	appInfo := auth.AppList["linux"]
    27  	deviceInfo := &auth.DeviceInfo{
    28  		Guid:          "cfcd208495d565ef66e7dff9f98764da",
    29  		DeviceName:    "Lagrange-DCFCD07E",
    30  		SystemKernel:  "Windows 10.0.22631",
    31  		KernelVersion: "10.0.22631",
    32  	}
    33  
    34  	qqclient := client.NewClient(0, "https://sign.lagrangecore.org/api/sign", appInfo)
    35  	qqclient.SetLogger(protocolLogger{})
    36  	qqclient.UseDevice(deviceInfo)
    37  	data, err := os.ReadFile("sig.bin")
    38  	if err != nil {
    39  		logrus.Warnln("read sig error:", err)
    40  	} else {
    41  		sig, err := auth.UnmarshalSigInfo(data, true)
    42  		if err != nil {
    43  			logrus.Warnln("load sig error:", err)
    44  		} else {
    45  			qqclient.UseSig(sig)
    46  		}
    47  	}
    48  
    49  	qqclient.GroupMessageEvent.Subscribe(func(client *client.QQClient, event *message.GroupMessage) {
    50  		if event.ToString() == "114514" {
    51  			img, _ := os.ReadFile("testgroup.png")
    52  			_, err := client.SendGroupMessage(event.GroupCode, []message.IMessageElement{&message.GroupImageElement{Stream: img}})
    53  			if err != nil {
    54  				return
    55  			}
    56  		}
    57  	})
    58  
    59  	qqclient.PrivateMessageEvent.Subscribe(func(client *client.QQClient, event *message.PrivateMessage) {
    60  		img, _ := os.ReadFile("testprivate.png")
    61  		_, err := client.SendPrivateMessage(event.Sender.Uin, []message.IMessageElement{&message.FriendImageElement{Stream: img}})
    62  		if err != nil {
    63  			return
    64  		}
    65  	})
    66  
    67  	err = qqclient.Login("", "qrcode.png")
    68  	if err != nil {
    69  		logrus.Errorln("login err:", err)
    70  		return
    71  	}
    72  
    73  	defer qqclient.Release()
    74  
    75  	defer func() {
    76  		data, err = qqclient.Sig().Marshal()
    77  		if err != nil {
    78  			logrus.Errorln("marshal sig.bin err:", err)
    79  			return
    80  		}
    81  		err = os.WriteFile("sig.bin", data, 0644)
    82  		if err != nil {
    83  			logrus.Errorln("write sig.bin err:", err)
    84  			return
    85  		}
    86  		logrus.Infoln("sig saved into sig.bin")
    87  	}()
    88  
    89  	// setup the main stop channel
    90  	mc := make(chan os.Signal, 2)
    91  	signal.Notify(mc, os.Interrupt, syscall.SIGTERM)
    92  	for {
    93  		switch <-mc {
    94  		case os.Interrupt, syscall.SIGTERM:
    95  			return
    96  		}
    97  	}
    98  }
    99  
   100  // protocolLogger from https://github.com/Mrs4s/go-cqhttp/blob/a5923f179b360331786a6509eb33481e775a7bd1/cmd/gocq/main.go#L501
   101  type protocolLogger struct{}
   102  
   103  const fromProtocol = "Lgr -> "
   104  
   105  func (p protocolLogger) Info(format string, arg ...any) {
   106  	logger.Infof(fromProtocol+format, arg...)
   107  }
   108  
   109  func (p protocolLogger) Warning(format string, arg ...any) {
   110  	logger.Warnf(fromProtocol+format, arg...)
   111  }
   112  
   113  func (p protocolLogger) Debug(format string, arg ...any) {
   114  	logger.Debugf(fromProtocol+format, arg...)
   115  }
   116  
   117  func (p protocolLogger) Error(format string, arg ...any) {
   118  	logger.Errorf(fromProtocol+format, arg...)
   119  }
   120  
   121  func (p protocolLogger) Dump(data []byte, format string, arg ...any) {
   122  	message := fmt.Sprintf(format, arg...)
   123  	if _, err := os.Stat(dumpspath); err != nil {
   124  		err = os.MkdirAll(dumpspath, 0o755)
   125  		if err != nil {
   126  			logger.Errorf("出现错误 %v. 详细信息转储失败", message)
   127  			return
   128  		}
   129  	}
   130  	dumpFile := path.Join(dumpspath, fmt.Sprintf("%v.dump", time.Now().Unix()))
   131  	logger.Errorf("出现错误 %v. 详细信息已转储至文件 %v 请连同日志提交给开发者处理", message, dumpFile)
   132  	_ = os.WriteFile(dumpFile, data, 0o644)
   133  }
   134  
   135  const (
   136  	// 定义颜色代码
   137  	colorReset  = "\x1b[0m"
   138  	colorRed    = "\x1b[31m"
   139  	colorYellow = "\x1b[33m"
   140  	colorGreen  = "\x1b[32m"
   141  	colorBlue   = "\x1b[34m"
   142  	colorWhite  = "\x1b[37m"
   143  )
   144  
   145  var logger = logrus.New()
   146  
   147  func init() {
   148  	logger.SetLevel(logrus.TraceLevel)
   149  	logger.SetFormatter(&ColoredFormatter{})
   150  	logger.SetOutput(colorable.NewColorableStdout())
   151  }
   152  
   153  type ColoredFormatter struct{}
   154  
   155  func (f *ColoredFormatter) Format(entry *logrus.Entry) ([]byte, error) {
   156  	// 获取当前时间戳
   157  	timestamp := time.Now().Format("2006-01-02 15:04:05")
   158  
   159  	// 根据日志级别设置相应的颜色
   160  	var levelColor string
   161  	switch entry.Level {
   162  	case logrus.DebugLevel:
   163  		levelColor = colorBlue
   164  	case logrus.InfoLevel:
   165  		levelColor = colorGreen
   166  	case logrus.WarnLevel:
   167  		levelColor = colorYellow
   168  	case logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel:
   169  		levelColor = colorRed
   170  	default:
   171  		levelColor = colorWhite
   172  	}
   173  
   174  	return utils.S2B(fmt.Sprintf("[%s] [%s%s%s]: %s\n",
   175  		timestamp, levelColor, strings.ToUpper(entry.Level.String()), colorReset, entry.Message)), nil
   176  }