mirror of
https://git.klmp200.net/ALFRED/ALFRED.git
synced 2025-01-18 18:46:44 +01:00
126 lines
3.0 KiB
Go
126 lines
3.0 KiB
Go
/*
|
|
* @Author: Bartuccio Antoine
|
|
* @Date: 2018-07-25 18:51:38
|
|
* @Last Modified by: Bartuccio Antoine
|
|
* @Last Modified time: 2019-01-04 10:47:25
|
|
*/
|
|
|
|
package commands
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.klmp200.net/ALFRED/ALFRED/settings"
|
|
"git.klmp200.net/ALFRED/ALFRED/shared"
|
|
|
|
"github.com/dghubble/go-twitter/twitter"
|
|
"github.com/dghubble/oauth1"
|
|
tb "gopkg.in/tucnak/telebot.v2"
|
|
)
|
|
|
|
var client *twitter.Client
|
|
|
|
func initTwitter() {
|
|
config := oauth1.NewConfig(
|
|
settings.Settings["twitter consumer key"].(string),
|
|
settings.Settings["twitter consumer secret"].(string),
|
|
)
|
|
token := oauth1.NewToken(
|
|
settings.Settings["twitter access token"].(string),
|
|
settings.Settings["twitter access secret"].(string),
|
|
)
|
|
|
|
http_client := config.Client(oauth1.NoContext, token)
|
|
client = twitter.NewClient(http_client)
|
|
}
|
|
|
|
func testOrInitTwitter() {
|
|
if client == nil {
|
|
initTwitter()
|
|
}
|
|
}
|
|
|
|
func twitterCommunicationError(m *tb.Message) {
|
|
shared.Bot.Send(m.Chat, "Désolé, les serveurs de twitter sont injoignables.")
|
|
}
|
|
|
|
// LastTrumpTweet sends the last tweet of Donald Trump
|
|
func LastTrumpTweet(m *tb.Message) {
|
|
testOrInitTwitter()
|
|
user, _, err := client.Users.Show(&twitter.UserShowParams{ScreenName: "realDonaldTrump"})
|
|
if err != nil {
|
|
twitterCommunicationError(m)
|
|
return
|
|
}
|
|
timeline, _, err := client.Timelines.UserTimeline(&twitter.UserTimelineParams{ScreenName: "realDonaldTrump"})
|
|
if err != nil {
|
|
twitterCommunicationError(m)
|
|
return
|
|
}
|
|
response := []string{
|
|
user.Name,
|
|
"\nFollowers : ",
|
|
strconv.Itoa(user.FollowersCount),
|
|
"\nStatus : ",
|
|
user.Description,
|
|
"\n ---",
|
|
"\n" + timeline[0].Text,
|
|
"\n ---",
|
|
"\n" + timeline[0].Source,
|
|
}
|
|
shared.Bot.Send(m.Chat, strings.Join(response, " "))
|
|
}
|
|
|
|
// TwitterTrends sends the french twitter trends
|
|
func TwitterTrends(m *tb.Message) {
|
|
testOrInitTwitter()
|
|
trends, _, err := client.Trends.Place(int64(615702), nil)
|
|
if err != nil {
|
|
twitterCommunicationError(m)
|
|
return
|
|
}
|
|
message := "Voici les dernières tendances en France"
|
|
for _, trend := range trends[0].Trends {
|
|
message += "\n" + trend.Name
|
|
}
|
|
shared.Bot.Send(m.Chat, message)
|
|
}
|
|
|
|
// TwitterSJW sends lasts messages with #SJW
|
|
func TwitterSJW(m *tb.Message) {
|
|
testOrInitTwitter()
|
|
lastUse, exists := shared.ChatData.Get(m.Chat.ID, "last chaos use")
|
|
if exists {
|
|
var date *time.Time
|
|
if lastUseConverted, isString := lastUse.(string); isString {
|
|
parsedDate, err := time.Parse(time.RFC3339, lastUseConverted)
|
|
if err == nil {
|
|
date = &parsedDate
|
|
}
|
|
}
|
|
|
|
if lastUseConverted, isDate := lastUse.(time.Time); isDate {
|
|
date = &lastUseConverted
|
|
}
|
|
|
|
if date != nil || time.Now().Before(date.Add(time.Hour*24)) {
|
|
shared.Bot.Send(m.Chat, "Arioch ne répondra pas à votre appel.")
|
|
return
|
|
}
|
|
}
|
|
shared.ChatData.Set(m.Chat.ID, "last chaos use", time.Now())
|
|
|
|
tweets, _, err := client.Search.Tweets(&twitter.SearchTweetParams{
|
|
Query: "#SJW",
|
|
})
|
|
if err != nil {
|
|
twitterCommunicationError(m)
|
|
return
|
|
}
|
|
for _, tweet := range tweets.Statuses {
|
|
shared.Bot.Send(m.Chat, tweet.Text)
|
|
}
|
|
}
|