diff --git a/.gitignore b/.gitignore index a0caebe..264c402 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,7 @@ *.out bot-config.json -.idea \ No newline at end of file +bot-config.yaml +.idea + +bot.db diff --git a/commands/command.go b/commands/command.go index 8eaac61..6dd69ea 100644 --- a/commands/command.go +++ b/commands/command.go @@ -35,13 +35,47 @@ func AddCommand(Bot *tgbotapi.BotAPI, update *tgbotapi.Update) { } } +func UpdateCommand(Bot *tgbotapi.BotAPI, update *tgbotapi.Update) { + + commandArguments := strings.Split(update.Message.CommandArguments(), " ") + + if len(commandArguments) < 2 { + log.Debug("Not enough arguments\\. We need \"/update id url\"") + return + } + + feedUrl := commandArguments[1] + chatid := update.Message.Chat.ID + userid := update.Message.From.ID + feedid, err := strconv.Atoi(commandArguments[0]) + if err != nil { + txt := fmt.Sprintf("Fail parse to int id %s", commandArguments[0]) + log.Debug(txt) + replies.SimpleMessage(Bot, chatid, update.Message.MessageID, txt) + return + } + + err = feeds.UpdateFeedByID(Bot, feedid, feedUrl, chatid, userid) + if err != nil { + replies.SimpleMessage(Bot, chatid, update.Message.MessageID, err.Error()) + return + } + txt := "" + + if err == nil { + txt = fmt.Sprintf("The feed with the url [%s] was successfully updated to this channel\\!", replies.FilterMessageChars(feedUrl)) + replies.SimpleMessage(Bot, chatid, update.Message.MessageID, txt) + } +} + func ListCommand(Bot *tgbotapi.BotAPI, update *tgbotapi.Update) { chatid := update.Message.Chat.ID userid := update.Message.From.ID feedres, err := feeds.ListFeeds(userid, chatid) if err != nil { - panic(err) + log.Debugf("Fail to ListFeeds, got %v", err) + return } replies.ListOfFeeds(Bot, feedres, chatid, update.Message.MessageID) @@ -51,13 +85,20 @@ func DeleteCommand(Bot *tgbotapi.BotAPI, update *tgbotapi.Update) { commandArguments := strings.Split(update.Message.CommandArguments(), " ") if len(commandArguments) < 1 { - panic("Not enough arguments\\. We need \"/delete id\"") + log.Debug("Not enough arguments\\. We need \"/delete id\"") + return } - feedid, _ := strconv.Atoi(commandArguments[0]) chatid := update.Message.Chat.ID userid := update.Message.From.ID - err := feeds.DeleteFeedByID(feedid, chatid, userid) + feedid, err := strconv.Atoi(commandArguments[0]) + if err != nil { + txt := fmt.Sprintf("Fail parse to int id %s", commandArguments[0]) + log.Debug(txt) + replies.SimpleMessage(Bot, chatid, update.Message.MessageID, txt) + return + } + err = feeds.DeleteFeedByID(feedid, chatid, userid) if err != nil { txt := fmt.Sprintf("There is no feed with the id [%d]\\!", feedid) @@ -75,6 +116,7 @@ func HelpCommand(Bot *tgbotapi.BotAPI, update *tgbotapi.Update) { /add %FeedName %URL - With this you can add a new feed for the current channel, both the name and the url parameters are required /list - With this command you are able to list all the existing feeds with their ID numbers /delete %ID - With this command you are able to delete an added feed if you do not need it anymore. The ID parameter is required and you can get it from the /list command +/update %ID %URL - With this command you are able to update a feed with a new url. The ID parameter and url are required and you can get id from the /list command ` replies.SimpleMessage(Bot, update.Message.Chat.ID, update.Message.MessageID, replies.FilterMessageChars(txt)) } diff --git a/feeds/feed.go b/feeds/feed.go index f1e4cdc..ec618fb 100644 --- a/feeds/feed.go +++ b/feeds/feed.go @@ -3,6 +3,7 @@ package feeds import ( "database/sql" "errors" + "fmt" "github.com/0x111/telegram-rss-bot/conf" "github.com/0x111/telegram-rss-bot/db" "github.com/0x111/telegram-rss-bot/models" @@ -57,6 +58,40 @@ func AddFeed(Bot *tgbotapi.BotAPI, name string, url string, chatid int64, userid return nil } +// Update a feed by id +func UpdateFeedByID(Bot *tgbotapi.BotAPI, feedid int, url string, chatid int64, userid int) error { + if err := FeedExistsByID(feedid, chatid, userid); err != nil { + return fmt.Errorf("There is no feed with the id [%d]\\!", feedid) + } + + DB := db.GetDB() + + // Check if user is providing a valid feed URL + if err := isValidFeed(url); err != nil { + log.WithFields(log.Fields{"error": err}).Debug("Invalid feed!") + return fmt.Errorf("The feed you are trying to add is not a valid feed URL\\!") + } + + stmt, err := DB.Prepare("UPDATE feeds SET url = ? WHERE id = ?") + defer stmt.Close() + + if err != nil { + log.WithFields(log.Fields{"error": err}).Error("There was an error while preparing the query!") + return err + } + + _, err = stmt.Exec(url, feedid) + + if err != nil { + log.WithFields(log.Fields{"error": err}).Error("There was an error while executing the query!") + return err + } + + log.Debug("Feed updated successfully!") + + return nil +} + // Delete a feed by a feedID parameter func DeleteFeedByID(feedid int, chatid int64, userid int) error { if err := FeedExistsByID(feedid, chatid, userid); err != nil { diff --git a/telegram-rss-bot.go b/telegram-rss-bot.go index c8f4bf1..ffca64f 100644 --- a/telegram-rss-bot.go +++ b/telegram-rss-bot.go @@ -66,6 +66,11 @@ func main() { commands.AddCommand(Bot, &update) } + // handle update command + if update.Message.IsCommand() && update.Message.Command() == "update" { + commands.UpdateCommand(Bot, &update) + } + // handle delete command if update.Message.IsCommand() && update.Message.Command() == "delete" { commands.DeleteCommand(Bot, &update)