func botman(b *tgbotapi.BotAPI, u tgbotapi.UpdateConfig) error { const defaultMessage = `Choose your destiny!` keyboard := tgbotapi.NewKeyboardButtonRow( tgbotapi.NewKeyboardButton(`/easy`), tgbotapi.NewKeyboardButton(`/hard`)) updates, err := b.GetUpdatesChan(u) if err != nil { return err } for update := range updates { chatID := update.Message.Chat.ID text := update.Message.Text if b.Debug == true { log.Printf("[%s] %s", update.Message.From.UserName, text) } switch text { case "/start": { msg := tgbotapi.NewMessage(chatID, defaultMessage) msg.ReplyMarkup = tgbotapi.NewReplyKeyboard(keyboard) b.Send(msg) } case "/stop": { msg := tgbotapi.NewMessage(chatID, `SeeYa!`) msg.ReplyMarkup = tgbotapi.ReplyKeyboardHide{HideKeyboard: true} b.Send(msg) } case "/easy": { msg := tgbotapi.NewMessage(chatID, genPass(10)) b.Send(msg) } case "/hard": { msg := tgbotapi.NewMessage(chatID, genPass(20)) b.Send(msg) } default: { msg := tgbotapi.NewMessage(chatID, defaultMessage) b.Send(msg) } } } return nil }
func sendToChat(bot *tgbotapi.BotAPI, message string) { chatid, err := strconv.Atoi(os.Getenv("TG_CHAT_ID")) if err != nil { log.Fatal(err) } msgconf := tgbotapi.NewMessage(int64(chatid), message) bot.Send(msgconf) }
func main() { godotenv.Load() bot, err := tgbotapi.NewBotAPI(os.Getenv("TG_TOKEN")) if err != nil { log.Fatal("Error creating bot: " + err.Error()) } u := tgbotapi.NewUpdate(0) u.Timeout = 60 updates, err := bot.GetUpdatesChan(u) if err != nil { log.Panic(err) } // Mumble config mumbleParsedURL, err := url.Parse(os.Getenv("MUMBLE_URL")) if err != nil { log.Fatal(err) } if mumbleParsedURL.User.Username() == "" { log.Fatal("Mumble URL must include a username") } mumbleConf := gumble.NewConfig() if !strings.ContainsRune(mumbleParsedURL.Host, ':') { // If address does not specify port... mumbleConf.Address = mumbleParsedURL.Host + ":64738" } else { mumbleConf.Address = mumbleParsedURL.Host } mumbleConf.Username = mumbleParsedURL.User.Username() if pass, ok := mumbleParsedURL.User.Password(); ok { mumbleConf.Password = pass } client := gumble.NewClient(mumbleConf) client.Attach(gumbleutil.Listener{ UserChange: func(e *gumble.UserChangeEvent) { if e.Type.Has(gumble.UserChangeConnected) { fmt.Println("User connected!") sendToChat(bot, fmt.Sprintf("%s connected #cgsnotify", e.User.Name)) } }, Disconnect: func(e *gumble.DisconnectEvent) { switch { case e.Type.Has(gumble.DisconnectError): sendToChat(bot, "Disconnected from mumble due to error, reconnecting in 5s") time.Sleep(5 * time.Second) e.Client.Connect() case e.Type.Has(gumble.DisconnectKicked): sendToChat(bot, "I just got kicked from mumble - rejoining out of spite") e.Client.Connect() case e.Type.Has(gumble.DisconnectBanned): sendToChat(bot, "I just got banned from mumble! Rude!") } }, }) if err := client.Connect(); err != nil { panic(err) } for update := range updates { log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text) switch update.Message.Text { case "/users": var users []string for _, user := range client.Users { if user.Session != client.Self.Session { // Skip adding ourself to the online users users = append(users, user.Name) } } if len(users) == 0 { users = []string{"Noone"} } var isare string if len(users) < 2 { isare = "is" } else { isare = "are" } bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, fmt.Sprintf("%s %s online", prettyListify(users), isare))) case "/chatid": bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, fmt.Sprintf("This chat's ID: %d", update.Message.Chat.ID))) } } }