Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add UpdateCommand & UpdateFeedByID #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@
*.out

bot-config.json
.idea
bot-config.yaml
.idea

bot.db
50 changes: 46 additions & 4 deletions commands/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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))
}
35 changes: 35 additions & 0 deletions feeds/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions telegram-rss-bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down