chore: rename telegram init to telegram auth

This commit is contained in:
David Chang 2020-12-06 13:59:47 +08:00
parent 58aadd9f45
commit 9f92fcf2e4
4 changed files with 15 additions and 15 deletions

View File

@ -41,7 +41,7 @@ Add your dotenv file:
SLACK_TOKEN=
TELEGRAM_BOT_TOKEN=
TELEGRAM_INIT_TOKEN=
TELEGRAM_AUTH_TOKEN=
BINANCE_API_KEY=
BINANCE_API_SECRET=
@ -221,11 +221,11 @@ streambook.BindStream(stream)
- input bot username. This should be global unique. ex. `PeqFqJxP_bbgo_bot`
- Botfather return bot token. Keep bot token safe
- Set `TELEGRAM_BOT_TOKEN` in `.env.local`
- Set `TELEGRAM_INIT_TOKEN` in `.env.local`. Generate your own init token. ex. 92463901, or kx2UX@eM
- Set `TELEGRAM_AUTH_TOKEN` in `.env.local`. Generate your own auth token. ex. 92463901, or kx2UX@eM
- Run bbgo
- In telegram: search your bot `PeqFqJxP_bbgo_bot`
- /start
- /init 92463901
- /auth 92463901
- done! your session will route to telegram
## Support

View File

@ -40,7 +40,7 @@ func init() {
RootCmd.PersistentFlags().String("slack-error-channel", "bbgo-error", "slack error channel")
RootCmd.PersistentFlags().String("telegram-bot-token", "", "telegram bot token from bot father")
RootCmd.PersistentFlags().String("telegram-init-token", "", "telegram init token")
RootCmd.PersistentFlags().String("telegram-auth-token", "", "telegram auth token")
RootCmd.PersistentFlags().String("binance-api-key", "", "binance api key")
RootCmd.PersistentFlags().String("binance-api-secret", "", "binance api secret")

View File

@ -124,11 +124,11 @@ func runConfig(basectx context.Context, userConfig *bbgo.Config) error {
// for telegram
telegramBotToken := viper.GetString("telegram-bot-token")
telegramInitToken := viper.GetString("telegram-init-token")
if len(telegramBotToken) > 0 && len(telegramInitToken) > 0 {
telegramAuthToken := viper.GetString("telegram-auth-token")
if len(telegramBotToken) > 0 && len(telegramAuthToken) > 0 {
log.Infof("adding telegram notifier")
var notifier = telegramnotifier.New(telegramBotToken, telegramInitToken)
var notifier = telegramnotifier.New(telegramBotToken, telegramAuthToken)
// start telegram bot
go notifier.Bot.Start()

View File

@ -17,7 +17,7 @@ type Notifier struct {
type NotifyOption func(notifier *Notifier)
// start bot daemon
func New(botToken, initToken string, options ...NotifyOption) *Notifier {
func New(botToken, authToken string, options ...NotifyOption) *Notifier {
notifier := &Notifier{
chatUser: &tb.User{},
@ -44,20 +44,20 @@ func New(botToken, initToken string, options ...NotifyOption) *Notifier {
bot.Handle("/help", func(m *tb.Message) {
helpMsg := `
help - print help message
init - initialize telegram bot with initToken. ex. /init my-token
auth - authorize current telegram user to access telegram bot with authToken. ex. /auth my-token
info - print information about current chat
`
bot.Send(m.Sender, helpMsg)
})
// init check initToken and then set sender id
bot.Handle("/init", func(m *tb.Message) {
// auth check authToken and then set sender id
bot.Handle("/auth", func(m *tb.Message) {
log.Info("Receive message: ", m) //debug
if m.Payload == initToken {
if m.Payload == authToken {
notifier.chatUser = m.Sender
bot.Send(m.Sender, "Bot initialized")
bot.Send(m.Sender, "User authorized")
} else {
bot.Send(m.Sender, "Error: bot intialize failed. Init token not match!")
bot.Send(m.Sender, "Error: User authorization failed. Auth token does not match!")
}
})
@ -84,7 +84,7 @@ func (n *Notifier) Notify(format string, args ...interface{}) {
func (n *Notifier) NotifyTo(channel, format string, args ...interface{}) {
if n.chatUser.ID == 0 {
log.Warningf("Telegram bot not initialized. Skip notification")
log.Warningf("Telegram bot has no authenticated user. Skip notification")
return
}