Created
August 28, 2018 10:04
-
-
Save hpcslag/7f7535e25c09cb80a80c724cb89a74ea to your computer and use it in GitHub Desktop.
每日關心 telegram bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"regexp" | |
"strings" | |
"github.com/go-telegram-bot-api/telegram-bot-api" | |
) | |
func main() { | |
bot, err := tgbotapi.NewBotAPI("xxxxxxxxxxxxxxxxxxxxxxxxxx") | |
if err != nil { | |
log.Fatal(err) | |
} | |
bot.Debug = true | |
log.Printf("Authorized on account %s", bot.Self.UserName) | |
u := tgbotapi.NewUpdate(0) | |
u.Timeout = 10 | |
updates, err := bot.GetUpdatesChan(u) | |
//(?m)(?<=每日關心)(.*?)(?=的|身|健) | |
re := regexp.MustCompile("(?:每日關心)(.*?)(?:的|身|健)") | |
nameList := make(map[string]string) | |
for update := range updates { | |
if update.Message == nil { | |
continue | |
} | |
if update.Message.Command() == "set" { | |
args := strings.Split(update.Message.Text, " ") | |
if len(args) < 2 { | |
continue | |
} | |
name := strings.Replace(args[1], " ", "", -1) | |
nameList[name] = args[2] | |
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "好,"+args[2]+"。") | |
msg.ReplyToMessageID = update.Message.MessageID | |
bot.Send(msg) | |
continue | |
} | |
go func() { | |
thisUpdate := update | |
//log.Printf("[%s] %s", thisUpdate.Message.From.UserName, thisUpdate.Message.Text) | |
matchArray := re.FindStringSubmatch(thisUpdate.Message.Text) | |
if len(matchArray) < 2 { | |
return | |
} | |
name := strings.Replace(matchArray[1], " ", "", -1) | |
if _, exist := nameList[name]; !exist { | |
msg := tgbotapi.NewMessage(thisUpdate.Message.Chat.ID, "不曉得") | |
msg.ReplyToMessageID = thisUpdate.Message.MessageID | |
bot.Send(msg) | |
} else { | |
msg := tgbotapi.NewMessage(thisUpdate.Message.Chat.ID, nameList[name]) | |
msg.ReplyToMessageID = thisUpdate.Message.MessageID | |
bot.Send(msg) | |
} | |
}() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment