├── .gitignore ├── README.md ├── configure.sh.sample ├── dao ├── entities.go ├── gorm.go └── utils.go ├── glide.lock ├── glide.yaml ├── main.go ├── tasks └── reddit.go └── utils └── telegram.go /.gitignore: -------------------------------------------------------------------------------- 1 | bot 2 | telegram-bot 3 | telegram.db 4 | vendor 5 | configure.sh -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Telegram Bot 2 | My personal Telegram bot 3 | 4 | #### Goal 5 | The key objective is to create some sort of automated notification system to stay up to date with various 6 | information sources (reddit, hacker news, rss feeds, tweets etc.) from one place. 7 | 8 | The bot will gather stuff from various sources and push them to me on Telegram. All sorts of contents mashed 9 | up in one place. 10 | 11 | 12 | #### Setup 13 | 14 | The different credentials and configuration values would be stored as environment variables. I have a 15 | `configure.sh` file which exports the variables. The project includes a `configure.sh.sample` which you 16 | can use as a template. I am using Glide to manage the project dependencies. I recommend you use it too. 17 | You can install it from here: https://glide.sh/ 18 | 19 | The following steps should set the project up - 20 | 21 | 22 | * `mv configure.sh.sample configure.sh` 23 | * Edit `configure.sh` to update your credentials / details 24 | * `. ./configure.sh` 25 | * `glide install` 26 | * `go build -o bot` 27 | * `./bot` -------------------------------------------------------------------------------- /configure.sh.sample: -------------------------------------------------------------------------------- 1 | export REDDIT_USERNAME="" 2 | export REDDIT_PASSWORD="" 3 | export TELEGRAM_KEY="" 4 | export TELEGRAM_OWNER_CHATID="" -------------------------------------------------------------------------------- /dao/entities.go: -------------------------------------------------------------------------------- 1 | package dao 2 | 3 | import ( 4 | "github.com/jinzhu/gorm" 5 | ) 6 | 7 | // RedditPost - Struct for storing Reddit Posts 8 | // Used by `gorm` 9 | type RedditPost struct { 10 | gorm.Model 11 | PermaLink string 12 | } 13 | -------------------------------------------------------------------------------- /dao/gorm.go: -------------------------------------------------------------------------------- 1 | package dao 2 | 3 | import ( 4 | "github.com/jinzhu/gorm" 5 | _ "github.com/jinzhu/gorm/dialects/sqlite" // for db 6 | ) 7 | 8 | // Init - Initialize database and return a handler 9 | func Init() *gorm.DB { 10 | db, err := gorm.Open("sqlite3", "telegram.db") 11 | if err != nil { 12 | panic("failed to connect database") 13 | } 14 | 15 | db.AutoMigrate(&RedditPost{}) 16 | 17 | return db 18 | } 19 | -------------------------------------------------------------------------------- /dao/utils.go: -------------------------------------------------------------------------------- 1 | package dao 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | // Exists - check if an item exists in db 8 | func Exists(PermaLink string) bool { 9 | db := Init() 10 | defer db.Close() 11 | var post RedditPost 12 | result := db.First(&post, "perma_link = ?", PermaLink) 13 | if result.Error != nil { 14 | errorMsg := result.Error.Error() 15 | 16 | if errorMsg == "record not found" { 17 | return false 18 | } 19 | 20 | fmt.Println(errorMsg) 21 | 22 | } 23 | 24 | return true 25 | 26 | } 27 | 28 | // Create post 29 | func Create(PermaLink string) { 30 | db := Init() 31 | defer db.Close() 32 | var post = RedditPost{PermaLink: PermaLink} 33 | result := db.Create(&post) 34 | if result.Error != nil { 35 | errorMsg := result.Error.Error() 36 | fmt.Println(errorMsg) 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /glide.lock: -------------------------------------------------------------------------------- 1 | hash: 6b77967d04a7776f3ed5b6fd90970ebbe38c550123e14841dda7a83f0e5948e3 2 | updated: 2016-11-05T10:26:08.134564481+06:00 3 | imports: 4 | - name: github.com/google/go-querystring 5 | version: 9235644dd9e52eeae6fa48efd539fdc351a0af53 6 | subpackages: 7 | - query 8 | - name: github.com/jinzhu/gorm 9 | version: 5174cc5c242a728b435ea2be8a2f7f998e15429b 10 | subpackages: 11 | - dialects/sqlite 12 | - name: github.com/jinzhu/inflection 13 | version: 74387dc39a75e970e7a3ae6a3386b5bd2e5c5cff 14 | - name: github.com/jzelinskie/geddit 15 | version: 4c36fbd6d900519bb9633b55d1a4e0b9c74cb16c 16 | - name: github.com/mattn/go-sqlite3 17 | version: cde7293c72308f3cc5b57bfa65e162fb868470d8 18 | - name: github.com/technoweenie/multipartstreamer 19 | version: a90a01d73ae432e2611d178c18367fbaa13e0154 20 | - name: golang.org/x/net 21 | version: 55a3084c9119aeb9ba2437d595b0a7e9cb635da9 22 | subpackages: 23 | - context 24 | - name: gopkg.in/telegram-bot-api.v4 25 | version: a7f48eb2dd301356942677e65bebe0c9aef07013 26 | testImports: [] 27 | -------------------------------------------------------------------------------- /glide.yaml: -------------------------------------------------------------------------------- 1 | package: github.com/masnun/masnun-bot 2 | import: 3 | - package: github.com/jinzhu/gorm 4 | version: ^1.0.0 5 | subpackages: 6 | - dialects/sqlite 7 | - package: github.com/jzelinskie/geddit 8 | - package: gopkg.in/telegram-bot-api.v4 9 | version: ^4.5.1 10 | - package: github.com/technoweenie/multipartstreamer 11 | version: ^1.0.1 12 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "github.com/masnun/telegram-bot/tasks" 4 | 5 | func main() { 6 | 7 | // Run the tasks 8 | tasks.PushReddit() 9 | 10 | } 11 | -------------------------------------------------------------------------------- /tasks/reddit.go: -------------------------------------------------------------------------------- 1 | package tasks 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | 7 | "github.com/jzelinskie/geddit" 8 | "github.com/masnun/telegram-bot/dao" 9 | "github.com/masnun/telegram-bot/utils" 10 | ) 11 | 12 | func PushReddit() { 13 | session, err := geddit.NewLoginSession( 14 | os.Getenv("REDDIT_USERNAME"), 15 | os.Getenv("REDDIT_PASSWORD"), 16 | "gedditAgent v1", 17 | ) 18 | 19 | if err != nil { 20 | fmt.Println("Reddit Login Error: ", err) 21 | return 22 | } 23 | 24 | subOpts := geddit.ListingOptions{ 25 | Limit: 15, 26 | } 27 | 28 | submissions, _ := session.Frontpage(geddit.DefaultPopularity, subOpts) 29 | 30 | for _, s := range submissions { 31 | if exists := dao.Exists(s.Permalink); !exists { 32 | fmt.Printf("Title: %s\nAuthor: %s\n\n", s.Title, s.Permalink) 33 | dao.Create(s.Permalink) 34 | utils.SendTelegramMessage(fmt.Sprintf("%s : https://www.reddit.com/%s", s.Title, s.Permalink)) 35 | } else { 36 | fmt.Println("Exists: ", s.Permalink) 37 | } 38 | 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /utils/telegram.go: -------------------------------------------------------------------------------- 1 | package utils 2 | 3 | import ( 4 | "os" 5 | "strconv" 6 | 7 | "fmt" 8 | 9 | "gopkg.in/telegram-bot-api.v4" 10 | ) 11 | 12 | func SendTelegramMessage(message string) { 13 | 14 | bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_KEY")) 15 | if err != nil { 16 | fmt.Println(err.Error()) 17 | } 18 | 19 | if bot.Self.UserName == "" { 20 | fmt.Println("Error connecting to Telegram!") 21 | return 22 | } 23 | 24 | chatID, _ := strconv.ParseInt(os.Getenv("TELEGRAM_OWNER_CHATID"), 10, 64) 25 | msg := tgbotapi.NewMessage(chatID, message) 26 | bot.Send(msg) 27 | 28 | } 29 | --------------------------------------------------------------------------------