Skip to content

Commit

Permalink
Add code to upload files to Google drive
Browse files Browse the repository at this point in the history
  • Loading branch information
gaowanliang committed Jan 11, 2021
1 parent 677fdb3 commit 22708e6
Show file tree
Hide file tree
Showing 5 changed files with 667 additions and 5 deletions.
57 changes: 52 additions & 5 deletions Telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ func uploadFiles(chatMsgID int, chatMsg string, bot *tgBotApi.BotAPI) {
uploadDFToOneDrive("./info/onedrive/" + b[1])
default:
switch b[1] {
case "1":
case "1": // 閫夋嫨OneDrive
_, err := os.Stat("./info/onedrive")
if err != nil {
err = os.MkdirAll("./info/onedrive", os.ModePerm)
Expand Down Expand Up @@ -442,6 +442,53 @@ func uploadFiles(chatMsgID int, chatMsg string, bot *tgBotApi.BotAPI) {
Keyboards = append(Keyboards, inlineKeyBoardRow)
}
text += locText("selectAccount")
case "2": // 閫夋嫨Google drive
_, err := os.Stat("./info/googleDrive")
if err != nil {
err = os.MkdirAll("./info/googleDrive", os.ModePerm)
dropErr(err)
}
dir, _ := ioutil.ReadDir("./info/googleDrive")
if len(dir) == 0 {
text = locText("noOneDriveInfo")
inlineKeyBoardRow := make([]tgBotApi.InlineKeyboardButton, 0)
inlineKeyBoardRow = append(inlineKeyBoardRow, tgBotApi.NewInlineKeyboardButtonData(locText("yes"), "onedrive~new"+":9"))
inlineKeyBoardRow = append(inlineKeyBoardRow, tgBotApi.NewInlineKeyboardButtonData(locText("no"), "onedrive~cancel"+":9"))
Keyboards = append(Keyboards, inlineKeyBoardRow)
} else {
text = locText("accountsAreCurrentlyLogin")
files := make([]string, 0)
rd, err := ioutil.ReadDir("./info/googleDrive/")
dropErr(err)
index := 1
for _, fi := range rd {
if !fi.IsDir() {
if strings.HasSuffix(strings.ToLower(fi.Name()), ".json") {
files = append(files, fi.Name())
text += fmt.Sprintf("%d.%s\n", index, fi.Name())
index++
}
}
}
inlineKeyBoardRow := make([]tgBotApi.InlineKeyboardButton, 0)
index = 1
for _, name := range files {
inlineKeyBoardRow = append(inlineKeyBoardRow, tgBotApi.NewInlineKeyboardButtonData(fmt.Sprint(index), "gdInfo~"+name+":9"))
if index%7 == 0 {
Keyboards = append(Keyboards, inlineKeyBoardRow)
inlineKeyBoardRow = make([]tgBotApi.InlineKeyboardButton, 0)
}
index++
}
if len(inlineKeyBoardRow) != 0 {
Keyboards = append(Keyboards, inlineKeyBoardRow)
}
inlineKeyBoardRow = make([]tgBotApi.InlineKeyboardButton, 0)
inlineKeyBoardRow = append(inlineKeyBoardRow, tgBotApi.NewInlineKeyboardButtonData(locText("createNewAcc"), "onedrive~new"+":9"))
inlineKeyBoardRow = append(inlineKeyBoardRow, tgBotApi.NewInlineKeyboardButtonData(locText("cancel"), "upload~cancel"+":9"))
Keyboards = append(Keyboards, inlineKeyBoardRow)
}
text += locText("selectAccount")
case "Upload":
//CopyFiles(copyFiles)
bot.Send(tgBotApi.NewDeleteMessage(myID, MessageID))
Expand Down Expand Up @@ -838,9 +885,9 @@ func tgBot(BotKey string, wg *sync.WaitGroup) {
dropErr(err)
}
var re = regexp.MustCompile(`(?m)code=(.*?)&`)
judgeLegal:=re.FindStringSubmatch(update.Message.Text)
judgeLegal := re.FindStringSubmatch(update.Message.Text)
//log.Println(judgeLegal)
if len(judgeLegal)>=2{
if len(judgeLegal) >= 2 {
isFileChanClean := false
for !isFileChanClean {
select {
Expand All @@ -852,8 +899,8 @@ func tgBot(BotKey string, wg *sync.WaitGroup) {
FileControlChan <- "close"
go uploadFiles(update.Message.MessageID, update.Message.Text, bot)
FileControlChan <- "onedrive~create"
}else{
msg.Text="鎮ㄨ緭鍏ョ殑OneDrive OAuth2閾炬帴鏈夎锛岃閲嶆柊杈撳叆"
} else {
msg.Text = "鎮ㄨ緭鍏ョ殑OneDrive OAuth2閾炬帴鏈夎锛岃閲嶆柊杈撳叆"
}

} else if !download(update.Message.Text) {
Expand Down
1 change: 1 addition & 0 deletions src/gdupload/credentials.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"installed":{"client_id":"413585947449-217qe10vfis0o4od73jjs103p3teih7k.apps.googleusercontent.com","project_id":"quickstart-1609508510551","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"IJdLG98l4KuTzcNE2KA2qfZ5","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}
9 changes: 9 additions & 0 deletions src/gdupload/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module gdupload

go 1.14

require (
golang.org/x/net v0.0.0-20200822124328-c89045814202
golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5
google.golang.org/api v0.30.0
)

0 comments on commit 22708e6

Please sign in to comment.