Skip to content

Commit

Permalink
feat: database
Browse files Browse the repository at this point in the history
  • Loading branch information
dukeyunz committed Jun 4, 2020
1 parent 865c098 commit b6b0dbb
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 18 deletions.
17 changes: 12 additions & 5 deletions api/bot-webhook.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
package handler

import (
"context"
"encoding/json"
"fund/command"
"fund/db"
"fund/log"
"github.com/globalsign/mgo/bson"
. "github.com/go-telegram-bot-api/telegram-bot-api"
"io/ioutil"
"net/http"
"os"
)

var bot, _ = NewBotAPI(os.Getenv("BOT_TOKEN"))
func Handler(w http.ResponseWriter, req *http.Request) {
bot, err := NewBotAPI(os.Getenv("BOT_TOKEN"))
if err != nil {
log.Error("Init Bot %+v", err)
}

func init() {
bot.Debug = true
log.Debug("Authorized on account %s", bot.Self.UserName)
}

func Handler(w http.ResponseWriter, req *http.Request) {
dBase := db.NewDB(os.Getenv("MGO_PWD"))
dBase.ListDatabaseNames(context.TODO(), bson.M{})

bytes, _ := ioutil.ReadAll(req.Body)
var update Update
err := json.Unmarshal(bytes, &update)
err = json.Unmarshal(bytes, &update)
if err != nil {
log.Error("Unmarshal update: ", err)
}
Expand Down
6 changes: 0 additions & 6 deletions command/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ func Command(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
msg.ReplyToMessageID = update.Message.MessageID
msg.ParseMode = tgbotapi.ModeHTML
//msg.ParseMode = tgbotapi.ModeMarkdown
log.Debug("Bot Token: %+v", bot.Token)
bot.Send(msg)


msg2 := tgbotapi.NewMessage(update.Message.Chat.ID+1, "this is test msg")
bot.Send(msg2)

log.Debug("[%s] %s", update.Message.From.UserName, update.Message.Text)
}
3 changes: 2 additions & 1 deletion command/command.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
fund - 获取基金信息
bitcoin - 获取比特币信息
index - 获取指数信息
index - 获取指数信息
bond - 获取中国国债信息
5 changes: 2 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func ViperEnvVariable(key string) string {
}

func GetWatches() []string {
return strings.Split(WatchFund, "-")
}

const WatchFund = "270042-110003-110023-000854-006223-162703-001592-161725-675011-110007-002351-161716-000311-240014"
return strings.Split("", "-")
}
35 changes: 35 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package db

import (
"context"
"fund/log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)

func NewDB(pwd string) *mongo.Client {
uri := "mongodb+srv://chengqian"+":"+pwd+"@cluster0-01hyt.azure.mongodb.net/fund?retryWrites=true&w=majority"

ctx := context.TODO()
clientOptions := options.Client().ApplyURI(uri)

client, err := mongo.NewClient(clientOptions)
if err != nil {
log.Error("New Client %+v", err)
}

err = client.Connect(ctx)
if err != nil {
log.Error("Connect %+v", err)
}

err = client.Ping(ctx, readpref.Primary())
if err != nil {
log.Error("Ping %+v", err)
}

log.Debug("Connected to MongoDB!")

return client
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ module fund
go 1.14

require (
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
github.com/olekukonko/tablewriter v0.0.4
github.com/sirupsen/logrus v1.6.0
github.com/spf13/viper v1.7.0
github.com/stretchr/objx v0.1.1 // indirect
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
github.com/yanyiwu/gojieba v1.1.2
go.mongodb.org/mongo-driver v1.3.4
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
)
85 changes: 84 additions & 1 deletion go.sum

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import "fund/bot"
import (
"fund/config"
"fund/db"
)

func main() {
// TODO: Email
Expand All @@ -9,8 +12,11 @@ func main() {
// Subject: "Fund notification",
//}

pwd := config.ViperEnvVariable("MGO_PWD")
db.NewDB(pwd)

// TODO: Bot
bot.Bot()
//bot.Bot()

// TODO: Monthly history
//fundCode := "161716"
Expand Down

2 comments on commit b6b0dbb

@vercel
Copy link

@vercel vercel bot commented on b6b0dbb Jun 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sysu-yunz
Copy link
Owner

@sysu-yunz sysu-yunz commented on b6b0dbb Jun 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.