Skip to content

Commit

Permalink
A better command parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
ihciah committed Apr 20, 2018
1 parent 3abf04b commit 82d90f7
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 115 deletions.
2 changes: 1 addition & 1 deletion channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (c *Channel) PushModule(control chan int, module_id int, followings []strin
for {
log.Printf("Will check for update for module %s-%s:%s", c.ID, MakeModuleLabeler().Module2Str(module_id), strings.Join(followings, ","))
next_start := time.After(wait_time)
func (){
func() {
defer func() {
if err := recover(); err != nil {
log.Println("Panic!", err)
Expand Down
52 changes: 26 additions & 26 deletions fetchers/tumblr.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@ import (
"github.com/patrickmn/go-cache"
"log"
"strconv"
"time"
"strings"
"time"
)

type TumblrPosts struct {
Meta struct {
Status int `json:"status"`
Msg string `json:"msg"`
Status int `json:"status"`
Msg string `json:"msg"`
} `json:"meta"`
Response struct {
Posts []struct {
Type string `json:"type"`
BlogName string `json:"blog_name"`
ID int64 `json:"id"`
PostURL string `json:"post_url"`
Slug string `json:"slug"`
Date string `json:"date"`
Timestamp int `json:"timestamp"`
State string `json:"state"`
Format string `json:"format"`
ShortURL string `json:"short_url"`
IsBlocksPostFormat bool `json:"is_blocks_post_format"`
SourceURL string `json:"source_url,omitempty"`
SourceTitle string `json:"source_title,omitempty"`
Caption string `json:"caption,omitempty"`
Type string `json:"type"`
BlogName string `json:"blog_name"`
ID int64 `json:"id"`
PostURL string `json:"post_url"`
Slug string `json:"slug"`
Date string `json:"date"`
Timestamp int `json:"timestamp"`
State string `json:"state"`
Format string `json:"format"`
ShortURL string `json:"short_url"`
IsBlocksPostFormat bool `json:"is_blocks_post_format"`
SourceURL string `json:"source_url,omitempty"`
SourceTitle string `json:"source_title,omitempty"`
Caption string `json:"caption,omitempty"`
Reblog struct {
Comment string `json:"comment"`
TreeHTML string `json:"tree_html"`
Expand All @@ -41,18 +41,18 @@ type TumblrPosts struct {
Post struct {
ID interface{} `json:"id"`
} `json:"post"`
ContentRaw string `json:"content_raw"`
Content string `json:"content"`
ContentRaw string `json:"content_raw"`
Content string `json:"content"`
} `json:"trail"`
VideoURL string `json:"video_url,omitempty"`
ThumbnailURL string `json:"thumbnail_url,omitempty"`
ThumbnailWidth int `json:"thumbnail_width,omitempty"`
ThumbnailHeight int `json:"thumbnail_height,omitempty"`
Duration int `json:"duration,omitempty"`
VideoType string `json:"video_type,omitempty"`
DisplayAvatar bool `json:"display_avatar"`
PhotosetLayout string `json:"photoset_layout,omitempty"`
Photos []struct {
VideoType string `json:"video_type,omitempty"`
DisplayAvatar bool `json:"display_avatar"`
PhotosetLayout string `json:"photoset_layout,omitempty"`
Photos []struct {
Caption string `json:"caption"`
OriginalSize struct {
URL string `json:"url"`
Expand Down Expand Up @@ -113,11 +113,11 @@ func (f *TumblrFetcher) getUserTimeline(user string, time int64) ([]ReplyMessage
if len(p.Trail) > 1 {
// We should get the original message id
msgid_str, ok := p.Trail[0].Post.ID.(string)
if ok && msgid_str != ""{
if ok && msgid_str != "" {
msgid = msgid_str
}
msgid_int64, ok := p.Trail[0].Post.ID.(int64)
if ok && msgid_int64 != 0{
if ok && msgid_int64 != 0 {
msgid = strconv.FormatInt(msgid_int64, 10)
}
}
Expand All @@ -131,7 +131,7 @@ func (f *TumblrFetcher) getUserTimeline(user string, time int64) ([]ReplyMessage
res := make([]Resource, 0, len(p.Photos))
for _, photo := range p.Photos {
tType := TIMAGE
if strings.HasSuffix(strings.ToLower(photo.OriginalSize.URL), ".gif"){
if strings.HasSuffix(strings.ToLower(photo.OriginalSize.URL), ".gif") {
tType = TVIDEO
}
res = append(res, Resource{photo.OriginalSize.URL, tType})
Expand Down
46 changes: 7 additions & 39 deletions tg_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
tb "github.com/ihciah/telebot"
f "github.com/ihciah/tg_channel_bot/fetchers"
"log"
"strconv"
"strings"
)
Expand Down Expand Up @@ -58,31 +57,8 @@ func (TGBOT *TelegramBot) handle_v2ex(m *tb.Message) {
TGBOT.SendAll(m.Sender, fetcher.GetPushAtLeastOne(strconv.Itoa(m.Sender.ID), []string{}))
}

func (TGBOT *TelegramBot) handle_twitter(m *tb.Message) {
var fetcher f.Fetcher = &TGBOT.FetcherConfigs.Twitter
fetcher.Init(TGBOT.Database)
TGBOT.SendAll(m.Sender, fetcher.GetPushAtLeastOne(strconv.Itoa(m.Sender.ID), []string{}))
}

func (TGBOT *TelegramBot) handle_twitter_media_test_only(m *tb.Message) {
var fetcher f.Fetcher = &TGBOT.FetcherConfigs.Twitter
fetcher.Init(TGBOT.Database)
TGBOT.SendAll(m.Sender, fetcher.GetPushAtLeastOne(strconv.Itoa(m.Sender.ID), []string{"cchaai", "MisaCat33"}))
}

func (TGBOT *TelegramBot) handle_twitter_channel_test_only(_ *tb.Message) {
var fetcher f.Fetcher = &TGBOT.FetcherConfigs.Twitter
channel := "@FEWSAWD"
fetcher.Init(TGBOT.Database)
chat, err := TGBOT.Bot.ChatByID(channel)
if err != nil {
log.Println("Error when start chat.", err)
}
TGBOT.SendAll(chat, fetcher.GetPush(channel, []string{"cchaai", "MisaCat33", "fleia0124"}))
}

func (TGBOT *TelegramBot) handle_controller(m *tb.Message) {
handlers := map[string]func(string, *tb.Message) string{
handlers := map[string]func([]string, *tb.Message) string{
"addchannel": TGBOT.requireSuperAdmin(TGBOT.h_addchannel),
"delchannel": TGBOT.requireSuperAdmin(TGBOT.h_delchannel),
"listchannel": TGBOT.requireSuperAdmin(TGBOT.h_listchannel),
Expand All @@ -100,22 +76,14 @@ func (TGBOT *TelegramBot) handle_controller(m *tb.Message) {
available_commands = append(available_commands, c)
}

var cmd, params string
commands := strings.SplitN(strings.Trim(m.Text, " \t\n"), " ", 2)
_, command_in := handlers[m.Text]
if command_in {
cmd, params = m.Text, ""
} else if len(commands) == 2 {
cmd, params = commands[0], commands[1]
var cmd string
var params []string
commands := strings.Fields(m.Text)
if _, command_in := handlers[commands[0]]; command_in {
cmd, params = commands[0], commands[1:]
TGBOT.Send(m.Sender, f.ReplyMessage{Caption: handlers[cmd](params, m)})
} else {
TGBOT.Send(m.Sender, f.ReplyMessage{Caption: "Command Format: CMD (params)"})
}

h_func, ok := handlers[cmd]
if !ok {
reply := "Unrecognized command.\nAvailable commands: \n" + strings.Join(available_commands, "\n")
TGBOT.Send(m.Sender, f.ReplyMessage{Caption: reply})
return
}
TGBOT.Send(m.Sender, f.ReplyMessage{Caption: h_func(params, m)})
}
Loading

0 comments on commit 82d90f7

Please sign in to comment.