Change DB to mysql
This commit is contained in:
parent
9d67df6d0a
commit
d0bccbde81
@ -40,8 +40,9 @@ func BindUser(m *tb.Message) string {
|
||||
u.refreshToken = refresh
|
||||
//TG的Data传递最高64bytes,一些msid超过了报错BUTTON_DATA_INVALID (0),采取md5
|
||||
u.msId = Get16MD5Encode(gjson.Get(info, "id").String())
|
||||
u.uptime = time.Now()
|
||||
u.other = SetJsonValue("{}", "alias", alias)
|
||||
u.uptime = time.Now().Unix()
|
||||
fmt.Println(u.uptime)
|
||||
u.other = alias
|
||||
//u.other = SetJsonValue(u.other, "sign", Get16MD5Encode(u.msId))
|
||||
//MS User Is Exist
|
||||
if MSUserIsExist(u.tgId, u.msId) {
|
||||
@ -90,7 +91,7 @@ func SignTask() {
|
||||
continue
|
||||
}
|
||||
fmt.Println(u.msId + " Sign OK!")
|
||||
u.uptime = time.Now()
|
||||
u.uptime = time.Now().Unix()
|
||||
if ok, err := UpdateData(db, u); !ok {
|
||||
fmt.Printf("%s Update Data ERROR: %s\n", u.msId, err)
|
||||
}
|
||||
|
||||
2
go.mod
2
go.mod
@ -3,7 +3,7 @@ module main
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/mattn/go-sqlite3 v2.0.3+incompatible
|
||||
github.com/go-sql-driver/mysql v1.5.0
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/robfig/cron/v3 v3.0.0
|
||||
github.com/spf13/viper v1.6.2
|
||||
|
||||
5
go.sum
5
go.sum
@ -22,6 +22,8 @@ github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeME
|
||||
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
||||
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
|
||||
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
|
||||
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
|
||||
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
||||
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
||||
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
|
||||
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
|
||||
@ -99,6 +101,8 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
|
||||
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
|
||||
github.com/techoner/gophp v0.2.0 h1:vVFoS2XC/NZariagUOhAGqC8p/Ws8R8ARYpj/9lTIAo=
|
||||
github.com/techoner/gophp v0.2.0/go.mod h1:NOxB/qoTl4+G82CkQp60T18Az5rOTufCqm83x9i/u7M=
|
||||
github.com/tidwall/gjson v1.6.0 h1:9VEQWz6LLMUsUl6PueE49ir4Ka6CzLymOAZDxpFsTDc=
|
||||
github.com/tidwall/gjson v1.6.0/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
|
||||
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
|
||||
@ -137,6 +141,7 @@ golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5h
|
||||
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884=
|
||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
|
||||
10
handle.go
10
handle.go
@ -3,9 +3,9 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/tidwall/gjson"
|
||||
tb "gopkg.in/tucnak/telebot.v2"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -40,11 +40,13 @@ func bStart(m *tb.Message) {
|
||||
}
|
||||
func bMy(m *tb.Message) {
|
||||
data := QueryDataByTG(db, m.Chat.ID)
|
||||
fmt.Println(data)
|
||||
var inlineKeys [][]tb.InlineButton
|
||||
for _, u := range data {
|
||||
fmt.Println(u)
|
||||
inlineBtn := tb.InlineButton{
|
||||
Unique: "my" + u.msId,
|
||||
Text: gjson.Get(u.other, "alias").String(),
|
||||
Text: u.other,
|
||||
Data: u.msId,
|
||||
}
|
||||
bot.Handle(&inlineBtn, bMyInlineBtn)
|
||||
@ -57,7 +59,7 @@ func bMyInlineBtn(c *tb.Callback) {
|
||||
fmt.Println(c.Data)
|
||||
r := QueryDataByMS(db, c.Data)
|
||||
u := r[0]
|
||||
bot.Send(c.Message.Chat, "信息\n别名:"+gjson.Get(u.other, "alias").String()+"\nMS_ID(MD5): "+u.msId+"\n最近更新时间: "+u.uptime.Format("2006-01-02 15:04:05"))
|
||||
bot.Send(c.Message.Chat, "信息\n别名:"+u.other+"\nMS_ID(MD5): "+u.msId+"\n最近更新时间: "+time.Unix(u.uptime, 0).Format("2006-01-02 15:04:05"))
|
||||
bot.Respond(c)
|
||||
}
|
||||
func bBind(m *tb.Message) {
|
||||
@ -76,7 +78,7 @@ func bUnBind(m *tb.Message) {
|
||||
for _, u := range data {
|
||||
inlineBtn := tb.InlineButton{
|
||||
Unique: "unbind" + u.msId,
|
||||
Text: gjson.Get(u.other, "alias").String(),
|
||||
Text: u.other,
|
||||
Data: u.msId,
|
||||
}
|
||||
bot.Handle(&inlineBtn, bUnBindInlineBtn)
|
||||
|
||||
8
main.go
8
main.go
@ -19,8 +19,8 @@ var (
|
||||
)
|
||||
|
||||
const (
|
||||
dbDriverName = "sqlite3"
|
||||
dbName = "./data.db"
|
||||
dbDriverName = "mysql"
|
||||
//dbName = "./data.db"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -50,10 +50,6 @@ func init() {
|
||||
bot, err = tb.NewBot(botsettings)
|
||||
CheckErr(err)
|
||||
|
||||
//sqlite init
|
||||
db, err = sql.Open(dbDriverName, dbName)
|
||||
CheckErr(err)
|
||||
CreateTB(db)
|
||||
}
|
||||
func main() {
|
||||
BotStart()
|
||||
|
||||
40
mysql.go
40
mysql.go
@ -3,19 +3,36 @@ package main
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"time"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/spf13/viper"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type MSData struct {
|
||||
tgId int64
|
||||
refreshToken string
|
||||
msId string
|
||||
uptime time.Time
|
||||
uptime int64
|
||||
other string
|
||||
}
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
viper.SetConfigName("config")
|
||||
viper.AddConfigPath(".")
|
||||
err = viper.ReadInConfig()
|
||||
CheckErr(err)
|
||||
host := viper.GetString("mysql.host")
|
||||
user := viper.GetString("mysql.user")
|
||||
port := viper.GetString("mysql.port")
|
||||
pwd := viper.GetString("mysql.user")
|
||||
database := viper.GetString("mysql.database")
|
||||
path := strings.Join([]string{user, ":", pwd, "@tcp(", host, ":", port, ")/", database, "?charset=utf8"}, "")
|
||||
//fmt.Println(path)
|
||||
db, err = sql.Open(dbDriverName, path)
|
||||
fmt.Println(db.Ping())
|
||||
CheckErr(err)
|
||||
CreateTB(db)
|
||||
}
|
||||
|
||||
//update data by msId
|
||||
@ -72,9 +89,10 @@ func QueryDataByMS(db *sql.DB, msId string) []MSData {
|
||||
var result = make([]MSData, 0)
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var refresht, othert, msidt string
|
||||
var tgIdt int64
|
||||
var uptimet time.Time
|
||||
var (
|
||||
tgIdt, uptimet int64
|
||||
refresht, othert, msidt string
|
||||
)
|
||||
rows.Scan(&tgIdt, &refresht, &msidt, &uptimet, &othert)
|
||||
//fmt.Println(string(tgNamet) + "=>" + uptimet.Format("2006-01-02 15:04:05"))
|
||||
result = append(result, MSData{tgIdt, refresht, msidt, uptimet, othert})
|
||||
@ -89,8 +107,7 @@ func QueryDataAll(db *sql.DB) []MSData {
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var refresht, othert, msidt string
|
||||
var tgIdt int64
|
||||
var uptimet time.Time
|
||||
var tgIdt, uptimet int64
|
||||
rows.Scan(&tgIdt, &refresht, &msidt, &uptimet, &othert)
|
||||
//fmt.Println(string(tgNamet) + "=>" + uptimet.Format("2006-01-02 15:04:05"))
|
||||
result = append(result, MSData{tgIdt, refresht, msidt, uptimet, othert})
|
||||
@ -106,8 +123,7 @@ func QueryDataByTG(db *sql.DB, tgId int64) []MSData {
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var refresht, othert, msidt string
|
||||
var tgIdt int64
|
||||
var uptimet time.Time
|
||||
var tgIdt, uptimet int64
|
||||
rows.Scan(&tgIdt, &refresht, &msidt, &uptimet, &othert)
|
||||
result = append(result, MSData{tgIdt, refresht, msidt, uptimet, othert})
|
||||
}
|
||||
@ -116,12 +132,12 @@ func QueryDataByTG(db *sql.DB, tgId int64) []MSData {
|
||||
func CreateTB(db *sql.DB) (bool, error) {
|
||||
|
||||
sqltable := `
|
||||
create table if not exists "users"
|
||||
create table if not exists users
|
||||
(
|
||||
tg_id INTEGER,
|
||||
refresh_token TEXT,
|
||||
ms_id TEXT,
|
||||
uptime DATE,
|
||||
uptime INTEGER,
|
||||
other TEXT
|
||||
);`
|
||||
_, err := db.Exec(sqltable)
|
||||
|
||||
32
util.go
32
util.go
@ -3,12 +3,10 @@ package main
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -53,36 +51,6 @@ func GetURLValue(Url, key string) string {
|
||||
return query.Get(key)
|
||||
}
|
||||
|
||||
//return result
|
||||
func SetJsonValue(sjson, key, value string) (RJson string) {
|
||||
var m map[string]interface{}
|
||||
m = make(map[string]interface{})
|
||||
err := json.Unmarshal([]byte(sjson), &m)
|
||||
CheckErr(err)
|
||||
m[key] = value
|
||||
data, _ := json.Marshal(m)
|
||||
return string(data)
|
||||
}
|
||||
|
||||
//Returns a json
|
||||
func MarshalMSData(u MSData) string {
|
||||
type MSDataOnlyString struct {
|
||||
TgId string `json:"tgId"`
|
||||
RefreshToken string `json:"refreshToken"`
|
||||
MsId string `json:"msId"`
|
||||
Uptime string `json:"uptime"`
|
||||
Other string `json:"other"`
|
||||
}
|
||||
var MSNoTime MSDataOnlyString
|
||||
MSNoTime.TgId = strconv.FormatInt(u.tgId, 10)
|
||||
MSNoTime.RefreshToken = u.refreshToken
|
||||
MSNoTime.MsId = u.msId
|
||||
MSNoTime.Uptime = u.uptime.Format("2006-01-02 15:04:05")
|
||||
MSNoTime.Other = u.other
|
||||
result, _ := json.Marshal(MSNoTime)
|
||||
return string(result)
|
||||
}
|
||||
|
||||
//返回一个32位md5加密后的字符串
|
||||
func GetMD5Encode(data string) string {
|
||||
h := md5.New()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user