Added basic subscribe and unsubscribe

This commit is contained in:
Amalvy Arthur 2018-12-31 00:48:46 +01:00
parent dea2b4cfb4
commit ef2866e89b
3 changed files with 125 additions and 57 deletions

View File

@ -19,21 +19,25 @@ import (
func main() {
registeredCommands := map[string]func(*tb.Message){
tb.OnText: commands.OnText,
"/hello": commands.Hello,
"/sponge": commands.Sponge,
"/git": commands.Git,
"/framapad": commands.Framapad,
"/setgender": commands.SetGender,
"/gender": commands.Gender,
"/roll": commands.Dice,
"/trump": commands.LastTrumpTweet,
"/trends": commands.TwitterTrends,
"/chaos": commands.TwitterSJW,
"/apero": commands.AperoTime,
"/quote": commands.Quote,
"/save": commands.Save,
"/getsaved": commands.GetSaved,
tb.OnText: commands.OnText,
"/hello": commands.Hello,
"/sponge": commands.Sponge,
"/git": commands.Git,
"/framapad": commands.Framapad,
"/setgender": commands.SetGender,
"/gender": commands.Gender,
"/roll": commands.Dice,
"/trump": commands.LastTrumpTweet,
"/trends": commands.TwitterTrends,
"/chaos": commands.TwitterSJW,
"/apero": commands.AperoTime,
"/quote": commands.Quote,
"/subscribe": commands.Subscribe,
"/unsubscribe": commands.Unsubscribe,
"/listsubscribers": commands.ListSubscribers,
"/publish": commands.Publish,
"/unpublish": commands.Unpublish,
"/retrieve": commands.Retrieve,
}
if err := settings.LoadSettings("settings.json", "settings_custom.json"); err != nil {

View File

@ -1,42 +0,0 @@
/*
* @Author: Amalvy Arthur
*/
package commands
import (
"log"
"../shared"
tb "gopkg.in/tucnak/telebot.v2"
)
func Save(m *tb.Message) {
if m.ReplyTo == nil {
shared.Bot.Send(m.Chat, "Please reply to a message to save it")
return
}
defer shared.Bot.Send(m.Chat, "Message sauvegardé : "+m.ReplyTo.Text)
savedMessages, exists := shared.ChatData.Get(m.Chat.ID, "saved_messages")
if !exists {
log.Println("no messages yet")
messageList := []string{m.ReplyTo.Text}
shared.ChatData.Set(m.Chat.ID, "saved_messages", messageList)
return
}
log.Println(append(savedMessages.([]string), m.ReplyTo.Text))
shared.ChatData.Set(m.Chat.ID, "saved_messages",
append(savedMessages.([]string), m.ReplyTo.Text))
}
func GetSaved(m *tb.Message) {
if _, exists := shared.ChatData.Get(m.Chat.ID, "saved_messages"); !exists {
shared.Bot.Send(m.Chat, "Aucun message sauvegardé")
}
shared.Bot.Send(m.Chat, "Some messages exists")
savedMessages, _ := shared.ChatData.Get(m.Chat.ID, "saved_messages")
for _, message := range savedMessages.([]string) {
shared.Bot.Send(m.Chat, "message : "+message)
}
}

106
commands/subscribe.go Normal file
View File

@ -0,0 +1,106 @@
/*
* @Author: Amalvy Arthur
*/
// This module use a property named "subscribed_chats" in the shared Users structure.
// It consists of a list of chatID as a string, chatID are separated by ":" (unix PATH style)
package commands
import (
"strconv"
"strings"
"../shared"
tb "gopkg.in/tucnak/telebot.v2"
)
// Subscribe user sending the command to the current chat
// Command syntax : /subscribe
func Subscribe(m *tb.Message) {
if m.Chat.Type != "group" && m.Chat.Type != "supergroup" {
shared.Bot.Send(m.Chat, "Cette commande n'est pas authorisée pour ce type de chat")
return
}
userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats")
if !exists {
shared.Bot.Send(m.Chat, "Abonnement au chat : "+m.Chat.Title)
shared.Users.Set(m.Sender.Username, "subscribed_chats", strconv.FormatInt(m.Chat.ID, 10))
return
}
splittedChats := strings.Split(userSubscribedChats, ":")
for _, chatID := range splittedChats {
if chatID == strconv.FormatInt(m.Chat.ID, 10) {
shared.Bot.Send(m.Chat, "Vous êtes déjà abonné à ce chat : "+m.Chat.Title)
return
}
}
shared.Bot.Send(m.Chat, "Abonnement au chat : "+m.Chat.Title)
if len(userSubscribedChats) != 0 {
shared.Users.Set(m.Sender.Username, "subscribed_chats", userSubscribedChats+":"+strconv.FormatInt(m.Chat.ID, 10))
} else {
shared.Users.Set(m.Sender.Username, "subscribed_chats", strconv.FormatInt(m.Chat.ID, 10))
}
}
// Unsubscribe user sending the command from the current chat
// Command syntax : /unsubscribe
func Unsubscribe(m *tb.Message) {
if m.Chat.Type != "group" && m.Chat.Type != "supergroup" {
shared.Bot.Send(m.Chat, "Cette commande n'est pas authorisée pour ce type de chat")
return
}
userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats")
if !exists || len(userSubscribedChats) == 0 {
shared.Bot.Send(m.Chat, "Vous n'êtes abonné à aucun chat")
return
}
splittedChats := strings.Split(userSubscribedChats, ":")
for i, chatID := range splittedChats {
if chatID == strconv.FormatInt(m.Chat.ID, 10) {
shared.Bot.Send(m.Chat, "désabonnement du chat : "+m.Chat.Title)
splittedChats = append(splittedChats[:i], splittedChats[i+1:]...)
break
}
}
shared.Users.Set(m.Sender.Username, "subscribed_chats", strings.Join(splittedChats, ":"))
}
func ListSubscribers(m *tb.Message) {
// if m.Chat.Type != "group" && m.Chat.Type != "supergroup" {
// shared.Bot.Send(m.Chat, "Cette commande n'est pas authorisée pour ce type de chat")
// return
// }
}
func Publish(m *tb.Message) {
if m.ReplyTo == nil {
shared.Bot.Send(m.Chat, "Please reply to a message to save it")
return
}
defer shared.Bot.Send(m.Chat, "Message publié : "+m.ReplyTo.Text)
savedMessages, exists := shared.ChatData.Get(m.Chat.ID, "saved_messages")
if !exists {
messageList := []string{m.ReplyTo.Text}
shared.ChatData.Set(m.Chat.ID, "saved_messages", messageList)
return
}
shared.ChatData.Set(m.Chat.ID, "saved_messages",
append(savedMessages.([]string), m.ReplyTo.Text))
}
func Unpublish(m *tb.Message) {
}
func Retrieve(m *tb.Message) {
if _, exists := shared.ChatData.Get(m.Chat.ID, "saved_messages"); !exists {
shared.Bot.Send(m.Chat, "Aucun message sauvegardé")
return
}
savedMessages, _ := shared.ChatData.Get(m.Chat.ID, "saved_messages")
for _, message := range savedMessages.([]string) {
shared.Bot.Send(m.Chat, "message : "+message)
}
}