Last active
August 23, 2016 20:48
-
-
Save turnage/468f981f3b1e85bb19f2 to your computer and use it in GitHub Desktop.
Example graw 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 ( | |
"fmt" | |
"github.com/turnage/graw" | |
"github.com/turnage/redditproto" | |
) | |
type announcer struct{} | |
func (a *announcer) Post(post *redditproto.Link) { | |
fmt.Printf("[%s] %s\n", post.GetSubreddit(), post.GetTitle()) | |
} | |
func main() { | |
if err := graw.Run("useragent.protobuf", &announcer{}, "all"); err != nil { | |
fmt.Printf("An error occurred: %v\n", err) | |
} | |
} |
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 is the main package of autoreply, a simple Reddit bot that | |
// automatically replies to messages, which demonstrates how to use graw to | |
// write Reddit bots. | |
package main | |
import ( | |
"fmt" | |
"github.com/turnage/graw" | |
"github.com/turnage/redditproto" | |
) | |
// autoReplier is a grawbot, which is just a struct that implements methods graw | |
// looks for, which handle the events from Reddit graw feeds it. | |
type autoReplier struct { | |
cannedMessage string | |
eng graw.Engine | |
} | |
// SetUp is a method graw looks for. If it is implemented, it will be called | |
// before the engine starts looking for events on Reddit. If SetUp returns an | |
// error, the bot will stop. | |
func (a *autoReplier) SetUp() error { | |
a.eng = graw.GetEngine(a) | |
return nil | |
} | |
// Message is a method graw looks for. If implemented, graw knows the bot can | |
// handle private messages it receives, so this method will be called whenever | |
// the bot receives a private message. The message, like all data graw provides, | |
// is a protobuffer. | |
func (a *autoReplier) Message(message *redditproto.Message) { | |
if err := a.eng.Reply(message.GetName(), a.cannedMessage); err != nil { | |
fmt.Printf( | |
"Failed to reply to %s's message; err: %v\n", | |
message.GetAuthor(), | |
err, | |
) | |
} | |
} | |
func main() { | |
bot := &autoReplier{ | |
cannedMessage: "I'm not available right now.", | |
} | |
if err := graw.Run("agent.protobuf", bot); err != nil { | |
fmt.Printf("An error occurred: %v\n", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment