1. golang 接入Discord做消息推送

discord

discord 是非常优秀的聊天工具,游戏开黑还有非常多的开发组也在使用discord作为沟通聊天工具

第三方库:github.com/andersfylling/disgord

package main

import (
    "github.com/go-redis/redis/v7"
    "github.com/zhaojunlike/logger"
    "os"
    "zhaojunlike/cacher"
    "zhaojunlike/snkrs/discord"
)

var rds = cacher.NewGtCache(&redis.Options{
    Addr:     "",
    Password: xx",
    DB:       0,
})

func init() {
    _ = logger.SetLogger(`
    {   
        "File": {                 
            "filename": "./logs/discord/app.log", 
            "level": "TRAC",        
            "daily": true,        
            "maxlines": -1,   
            "maxsize": 1,         
            "maxdays": -1,      
            "append": true,    
            "permit": "0660"  
        }
    }
`)
}

func main() {
    config, err := discord.GetConfigFromConfigFile("./conf/config.discord.json")
    if err != nil {
        panic(err)
    }
    var opt = &discord.DisOption{
        Brand:    config.Brand,
        Version:  config.Version,
        BotToken: config.BotToken, //dev
        Proxy:    config.Proxy,
        Rds:      rds,
    }
    var bot = discord.NewDisBot(opt)
    defer bot.Destroy()
    err = bot.Start()
    if err != nil {
        logger.Error("Start 失败:", err)
        os.Exit(-1)
    }
    logger.Crit("Discord Notice Bot 启动成功,版本:%v", config.Version)
    _ = bot.GetClient().DisconnectOnInterrupt() //接受退出指令
}

Discrod 包装结构体

type DisBot struct {
    opt    *DisOption
    client *disgord.Client
    rds    *cacher.GtCache
}

func NewDisBot(opt *DisOption) *DisBot {
    var bot = &DisBot{opt: opt}
    var conf = disgord.Config{
        BotToken: opt.BotToken,
        Logger:   disgord.DefaultLogger(false), // debug=false
    }
    if opt.Proxy != "" {
        conf.Proxy, _ = proxy.SOCKS5("tcp", opt.Proxy, nil, nil)
    }
    bot.client = disgord.New(conf)
    bot.rds = opt.Rds
    return bot
}
func (bot *DisBot) Start() error {
    var err = bot.client.Connect(context.Background())
    if err != nil {
        return err
    }
    go bot.listenPublish()
    bot.client.On(disgord.EvtMessageCreate, bot.printMessage)
    bot.client.On(disgord.EvtReady, func() {

    })
    return nil
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将Golang接入ChatGPT,您可以使用AI的GPT-3 API来实现。以下是一些步骤和示例代码***一步骤。 2. 在Golang中,您可以使用HTTP请求库来与GPT-3 API进行通信。一个常用的库是`net/http`。 3. 在您的代码中,您需要构建一个HTTP POST请求,将您的输入文本发送给GPT-3 API,并接收返回的响应。 下面是一个简单的示例代码,演示如何使用Golang发送请求并接收响应: ```go package main import ( "fmt" "io/ioutil" "net/http" "strings" ) func main() { apiKey := "YOUR_API_KEY" url := "https://api.openai.com/v1/engines/davinci-codex/completions" input := "你想要问的问题" payload := strings.NewReader(fmt.Sprintf(`{ "prompt": "%s", "max_tokens": 50 }`, input)) req, _ := http.NewRequest("POST", url, payload) req.Header.Add("Content-Type", "application/json") req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", apiKey)) res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := ioutil.ReadAll(res.Body) fmt.Println(string(body)) } ``` 请确保将`YOUR_API_KEY`替换为您在OpenAI网站上获取的API密钥。 4. 在上述代码中,我们使用了`davinci-codex`引擎,您可以根据您需求选择其他引擎。`max_tokens`参数用于指定生成的响应的最大长度。 5. 运行代码后,您将收到来自GPT-3 API的响应,其中包含生成的文本。 这是一个简单的示例,您可以根据自己的需求进行修改和扩展。请确保遵循OpenAI的使用政策和指导方针。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值