This repository was archived by the owner on Aug 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
880b0fd
commit 8f2c587
Showing
15 changed files
with
263 additions
and
2,260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.idea/ | ||
config.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package bot | ||
|
||
import ( | ||
"gopkg.in/telegram-bot-api.v4" | ||
"log" | ||
"github.com/jhonata-menezes/kartolafc-backend/cmd" | ||
"regexp" | ||
"github.com/jhonata-menezes/kartolafc-backend/notification" | ||
"errors" | ||
) | ||
|
||
func Run(channelNotification chan *notification.MessageNotification) { | ||
// se nao tiver configurado nenhuma chave, bot nao e iniciado. | ||
if cmd.Config.BotKey == "" { | ||
log.Println("bot desabilitado") | ||
return | ||
} | ||
bot, err := tgbotapi.NewBotAPI(cmd.Config.BotKey) | ||
if err != nil { | ||
log.Panic(err) | ||
} | ||
//bot.Debug = true | ||
log.Printf("Authorized on account %s", bot.Self.UserName) | ||
|
||
u := tgbotapi.NewUpdate(0) | ||
u.Timeout = 60 | ||
|
||
updates, err := bot.GetUpdatesChan(u) | ||
|
||
for update := range updates { | ||
if update.Message == nil { | ||
continue | ||
} | ||
if update.Message.From.ID != cmd.Config.BotIdClient { | ||
continue | ||
} | ||
log.Printf("[ %s %s ] %s \n", update.Message.From.UserName, update.Message.From.ID, update.Message.Text) | ||
|
||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "comando não encontrado") | ||
msg.ReplyToMessageID = update.Message.MessageID | ||
command := TelegramGetCommand(update.Message.Text) | ||
|
||
switch command { | ||
case "/n": | ||
notify, err := getNotificacao(update.Message.Text) | ||
if err == nil { | ||
channelNotification <- ¬ify | ||
msg.Text = "Notificaçao enviada" | ||
} else { | ||
msg.Text = "padrao incorreto" | ||
} | ||
break | ||
} | ||
|
||
bot.Send(msg) | ||
} | ||
} | ||
|
||
|
||
func TelegramGetCommand(s string) string { | ||
regComando, _ := regexp.Compile("(/[\\w-_]+) ?") | ||
if regComando.MatchString(s) { | ||
match := regComando.FindStringSubmatch(s) | ||
return match[1] | ||
} | ||
return "" | ||
} | ||
|
||
func getNotificacao(s string) (notification.MessageNotification, error) { | ||
regComando, _ := regexp.Compile("(/[\\w-_]+) ?\n(.*?)\n(.*?)\n(.*?)\n(.*?)$") | ||
if regComando.MatchString(s) { | ||
match := regComando.FindStringSubmatch(s) | ||
return notification.MessageNotification{match[2], match[3], match[4], "", nil, match[5]}, nil | ||
} | ||
log.Println("errou") | ||
return notification.MessageNotification{}, errors.New("nao foi possivel parsear") | ||
} | ||
|
||
func TelegramCommandGetMessage(s string) string { | ||
regMensagem, _ := regexp.Compile("(/[\\w-_]+) (.*?)$") | ||
if regMensagem.MatchString(s) { | ||
match := regMensagem.FindStringSubmatch(s) | ||
return match[2] | ||
} | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,31 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"os" | ||
"io/ioutil" | ||
"log" | ||
"encoding/json" | ||
) | ||
|
||
var ServerBind string | ||
var MongoDBIpPort string | ||
type configTemplate struct { | ||
ServerBind string `json:"bind"` | ||
MongoDB string `json:"mongodb"` | ||
VapidPrivate string `json:"vapidPrivate"` | ||
BotKey string `json:"botKey"` | ||
BotIdClient int `json:"botClientId"` | ||
} | ||
|
||
var Config configTemplate | ||
|
||
func init() { | ||
cmd := cobra.Command{} | ||
cmd.Flags().StringVarP(&ServerBind, "bind", "b", "0.0.0.0:5015", "bind de interface ip:porta") | ||
cmd.Flags().StringVarP(&MongoDBIpPort, "mongodb", "m", "127.0.0.1:27017", "conexao mongo") | ||
//"Kartola FC é um wrapper da API do cartolafc" | ||
c, err := ioutil.ReadFile("./config.json") | ||
if err != nil { | ||
log.Println("arquivo de configuraçao nao existe") | ||
} | ||
|
||
if err := cmd.Execute(); err != nil { | ||
os.Exit(1) | ||
err = json.Unmarshal(c, &Config) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"bind": "0.0.0.0:5015", | ||
"mongodb": "127.0.0.1:27017", | ||
"vapidPrivate": "", | ||
"botKey": "", | ||
"botClientId": 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.