tencent-im/example/main.go
2022-09-27 11:31:23 +08:00

64 lines
1.8 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @Author: Echo
* @Author:1711788888@qq.com
* @Date: 2021/8/31 15:14
* @Desc: TODO
*/
package main
import (
"fmt"
"log"
"net/http"
"git.echol.cn/loser/tencent-im"
"git.echol.cn/loser/tencent-im/account"
"git.echol.cn/loser/tencent-im/callback"
)
func main() {
tim := im.NewIM(&im.Options{
AppId: 1400579830, // 无效的AppId,请勿直接使用
AppSecret: "0d2a321b087fdb8fd5ed5ea14fe0489139086eb1b03541283fc9feeab8f2bfd3", // 无效的AppSecret,请勿直接使用
UserId: "administrator", // 管理员用户账号请在腾讯云IM后台设置管理账号
})
// 导入账号
if err := tim.Account().ImportAccount(&account.Account{
UserId: "test1",
Nickname: "测试账号1",
FaceUrl: "https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png",
}); err != nil {
if e, ok := err.(im.Error); ok {
fmt.Println(fmt.Sprintf("import account failed, code:%d, message:%s.", e.Code(), e.Message()))
} else {
fmt.Println(fmt.Sprintf("import account failed:%s.", err.Error()))
}
}
fmt.Println("import account success.")
// 注册回调事件
tim.Callback().Register(callback.EventAfterFriendAdd, func(ack callback.Ack, data interface{}) {
fmt.Printf("%+v", data.(callback.AfterFriendAdd))
_ = ack.AckSuccess(0)
})
// 注册回调事件
tim.Callback().Register(callback.EventAfterFriendDelete, func(ack callback.Ack, data interface{}) {
fmt.Printf("%+v", data.(callback.AfterFriendDelete))
_ = ack.AckSuccess(0)
})
// 开启监听
http.HandleFunc("/callback", func(writer http.ResponseWriter, request *http.Request) {
tim.Callback().Listen(writer, request)
})
// 启动服务器
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal("ListenAndServe: ", err)
}
}