Beispiel #1
0
func main() {
	var lastTweet uint64 = 0
	var config Config

	// Load up config
	if err := gcfg.ReadFileInto(&config, "TwitterHAL.gcfg"); err != nil {
		fmt.Printf("Config error: %s\n", err)
		os.Exit(1)
	}

	// Set to a quite large value to not start counting down instantly
	var responseCountdown int = 1000

	// Set up Twitter Client
	var client *twittergo.Client = getTwitter(config)

	// Set up MegaHAL
	replyOpts := cobe.ReplyOptions{0, nil}
	megahal, _ := cobe.OpenCobe2Brain("twitterhal-brainfile")

	for true {
		// Print tweets
		for _, tweet := range fetchNewTweets(client, lastTweet) {
			fmt.Printf("\n\n======================\n")

			// Create a response to recieved tweet
			response := formatResponse(
				megahal.ReplyWithOptions(
					cleanTweetText(tweet.Text()),
					replyOpts,
				),
			)

			// Store recieved tweet in brain
			megahal.Learn(cleanTweetText(tweet.Text()))

			// Count down responseCountdown if the response is short enough
			if len(response) <= 140 {
				responseCountdown--
			}

			// Print recieved tweet
			fmt.Printf(
				"@%s: %s\n",
				tweet.User().ScreenName(),
				cleanTweetText(tweet.Text()),
			)

			// Print generated response
			fmt.Printf(
				"Response(%v): %v\n",
				len(response),
				response,
			)
			fmt.Printf("Response Countdown: %v\n", responseCountdown)

			// If countdown is below 0, send response and reset countdown
			if responseCountdown < 0 {
				// Check current hour so he doesn't tweet between 00:00-07:00
				if time.Now().Hour() > 6 {
					sendTweet(client, response)

					fmt.Println("tweet sent")
				}

				responseCountdown = 25 + rand.Intn(10)
			}

			// Counter to only fetch new tweets
			if tweet.Id() > lastTweet {
				lastTweet = tweet.Id()
			}
		}

		time.Sleep(time.Second * 60)
	}
}
Beispiel #2
0
func main() {
	flag.Parse()

	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			log.Fatalf("Creating cpu profile: %s", err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	if *statsdserver != "" {
		s, err := statsd.New(*statsdserver, *statsdname)
		if err != nil {
			log.Fatalf("Initializing statsd: %s", err)
		}

		cobe.SetStatter(s)
	}

	args := flag.Args()
	if len(args) < 1 {
		fmt.Println("Usage: cobe console")
		os.Exit(1)
	}

	b, err := cobe.OpenCobe2Brain("cobe.brain")
	if err != nil {
		log.Fatalf("Opening brain file: %s", err)
	}

	var cmd = args[0]
	switch {
	case cmd == "console":
		console.RunForever(b)
	case cmd == "ircbot" || cmd == "irc-client":
		opts := &ircbot.Options{
			Server:   *ircserver,
			Nick:     *ircnick,
			Channels: []string{*ircchannel},
		}
		ircbot.RunForever(b, opts)
	case cmd == "learn":
		for _, f := range args[1:] {
			learnFileLines(b, f)
		}
	case cmd == "del-stemmer":
		err := b.DelStemmer()
		if err != nil {
			log.Fatalf("Deleting stemmer: %s", err)
		}
	case cmd == "set-stemmer":
		if len(args) < 2 {
			log.Fatal("Usage: set-stemmer <language>")
		}
		err := b.SetStemmer(args[1])
		if err != nil {
			log.Fatalf("Setting stemmer: %s", err)
		}
	default:
		log.Fatalf("Unknown command: %s", cmd)
	}
}