Beispiel #1
0
func Create(bot *core.Gobot, config map[string]interface{}) {
	token, ok := config["token"].(string)
	if !ok {
		log.Println("Could not find token in config. Twitter plugin won't work")
		return
	}

	bot.ListenFor("^describe (\\w+)", func(msg core.Message, matches []string) core.Response {
		described, err := describe(token, matches[1])
		if err != nil {
			return bot.Error(err)
		}
		msg.Send(described)
		return bot.Stop()
	})

	bot.ListenFor("twitter\\.com/(\\w+)/status/(\\d+)", func(msg core.Message, matches []string) core.Response {
		user, tweet, err := getTweet(token, matches[2])
		if err != nil {
			return bot.Error(err)
		}
		if err == nil && tweet != "" {
			// Split multi-line tweets into separate PRIVMSG calls
			fields := strings.FieldsFunc(tweet, func(r rune) bool {
				return r == '\r' || r == '\n'
			})
			for _, field := range fields {
				if field != "" {
					msg.Send(fmt.Sprintf("%s: %s", user, field))
				}
			}
		}
		return bot.KeepGoing()
	})
}
Beispiel #2
0
func Create(bot *core.Gobot, config map[string]interface{}) {
	// matches is actually a map[string]string
	matches, found := config["matches"]
	if !found {
		log.Printf("Can't find matcher/matches plugin conf. Plugin will not run.")
		return
	}

	switch matches := matches.(type) {
	case map[string]interface{}:
		for pattern, replacement := range matches {
			switch replacement := replacement.(type) {
			case string:
				bot.ListenFor(pattern, func(msg core.Message, matches []string) core.Response {
					msg.Send(replacement)
					return bot.KeepGoing()
				})
			}
		}
	}

}
Beispiel #3
0
func Create(bot *core.Gobot, config map[string]interface{}) {
	if err := pluginState.Load(&markov); err != nil {
		log.Printf("Could not load plugin state: %s", err)
	}

	bot.ListenFor("^ *markov *$", func(msg core.Message, matches []string) core.Response {
		mutex.Lock()
		defer mutex.Unlock()
		output, err := generateRandom()
		if err != nil {
			return bot.Error(err)
		}
		msg.Send(output)
		return bot.KeepGoing()
	})

	// generate a chain for the specified user
	bot.ListenFor("^ *markov +(.+)", func(msg core.Message, matches []string) core.Response {
		mutex.Lock()
		defer mutex.Unlock()
		output, err := generate(matches[1])
		if err != nil {
			return bot.Error(err)
		}
		msg.Send(output)
		return bot.KeepGoing()
	})

	// listen to everything
	bot.ListenFor("(.*)", func(msg core.Message, matches []string) core.Response {
		mutex.Lock()
		defer mutex.Unlock()
		user := msg.User
		text := matches[0]
		record(user, text)
		return bot.KeepGoing()
	})
}