Skip to content

Commit

Permalink
connection to telegram bot
Browse files Browse the repository at this point in the history
  • Loading branch information
toannd96 committed Feb 28, 2022
1 parent 3b5a6f9 commit 3c56ffb
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 57 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TELEGRAM_TOKEN=<telegram_token>
141 changes: 141 additions & 0 deletions broadcasts/telegram.go
Original file line number Diff line number Diff line change
@@ -1 +1,142 @@
package broadcasts

import (
"fmt"
"strings"
"time"
"vieclamit/repository"

tb "gopkg.in/tucnak/telebot.v2"
)

type Telegram struct {
Token string
Repo repository.Repository
}

func (t *Telegram) NewTelegram() {
pref := tb.Settings{
Token: t.Token,
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
}

bot, err := tb.NewBot(pref)
if err != nil {
fmt.Println(err)
}

bot.Handle("/start", func(m *tb.Message) {
bot.Send(m.Sender, `👋 Tôi là bot việc làm IT
✅ Tôi có thể tìm kiếm tin tuyển dụng việc làm IT theo từ khóa địa điểm, kỹ năng, công ty
🔎 Để tôi giúp bạn hiểu cách hoạt động /help`)
})

bot.Handle("/help", func(m *tb.Message) {
bot.Send(m.Sender, `✅ Từ khóa bạn nhập không phân biệt chữ hoa chữ thường, phải có dấu
✅ Từ khóa tên địa điểm
👉 /location <tên địa điểm>
Ví dụ:
👍 /location hà nội
👎 /location ha noi
✅ Từ khóa tên công ty
👉 /company <tên công ty>
Ví dụ:
👍 /company smartosc
👍 /company giao hàng tiết kiệm
👎 /company giao hang tiet kiem
✅ Từ khóa tên kỹ năng
👉 /skill <tên kỹ năng>
Ví dụ:
👍 /skill golang`)
})

bot.Handle("/location", func(m *tb.Message) {
location := strings.TrimSpace(m.Text[9:])
if location == "" {
bot.Send(m.Sender, "💡 Nhập tên thành phố có công việc bạn muốn tìm. Ví dụ: /location Hà nội")
return
}
recruitments, err := t.Repo.FindByLocation(location, "vieclamit")
if err != nil {
fmt.Println(err)
}
for _, recruitment := range *recruitments {
output := getTemplate(
recruitment.Title,
recruitment.Company,
recruitment.Location,
recruitment.Salary,
recruitment.JobDeadline.Format("02/01/2006"),
recruitment.UrlJob,
recruitment.UrlCompany,
)
bot.Send(m.Sender, output, &tb.SendOptions{
ParseMode: "Markdown",
DisableWebPagePreview: true,
})
}
})

bot.Handle("/company", func(m *tb.Message) {
company := strings.TrimSpace(m.Text[8:])
if company == "" {
bot.Send(m.Sender, "💡 Nhập tên công ty có công việc bạn muốn tìm. Ví dụ: /company vng")
return
}
recruitments, err := t.Repo.FindByCompany(company, "vieclamit")
if err != nil {
fmt.Println(err)
}
for _, recruitment := range *recruitments {
output := getTemplate(
recruitment.Title,
recruitment.Company,
recruitment.Location,
recruitment.Salary,
recruitment.JobDeadline.Format("02/01/2006"),
recruitment.UrlJob,
recruitment.UrlCompany,
)
bot.Send(m.Sender, output, &tb.SendOptions{
ParseMode: "Markdown",
DisableWebPagePreview: true,
})
}
})

bot.Handle("/skill", func(m *tb.Message) {
skill := strings.TrimSpace(m.Text[6:])
if skill == "" {
bot.Send(m.Sender, "💡 Nhập tên kỹ năng bạn muốn tìm. Ví dụ: /skill php")
return
}
recruitments, err := t.Repo.FindBySkill(skill, "vieclamit")
if err != nil {
fmt.Println(err)
}
for _, recruitment := range *recruitments {
output := getTemplate(
recruitment.Title,
recruitment.Company,
recruitment.Location,
recruitment.Salary,
recruitment.JobDeadline.Format("02/01/2006"),
recruitment.UrlJob,
recruitment.UrlCompany,
)
bot.Send(m.Sender, output, &tb.SendOptions{
ParseMode: "Markdown",
DisableWebPagePreview: true,
})
}
})

bot.Start()
}

func getTemplate(title, company, location, salary, jobDeadline, urlJob, urlCompany string) string {
return fmt.Sprintf("*%s - %s*\n"+"🏢 %s\n"+"💰 %s\n"+"⏳ %s\n"+"👉 [%s](%s)\n"+"👉 [%s](%s)\n", title, company, location, salary, jobDeadline, "Xem tin tuyển dụng", urlJob, "Xem công ty", urlCompany)
}
4 changes: 2 additions & 2 deletions feeds/topcv.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func dataOnePage(url string, repo repository.Repository) error {

// TopCV crawl all page it jobs
func TopCV(repo repository.Repository) {
sem := semaphore.NewWeighted(int64(runtime.NumCPU()))
sem := semaphore.NewWeighted(int64(2 * runtime.NumCPU()))
group, ctx := errgroup.WithContext(context.Background())

totalPage, _ := totalPageTopCV()
Expand All @@ -177,7 +177,7 @@ func TopCV(repo repository.Repository) {
fmt.Println(err)
}

fmt.Println("Completed")
fmt.Println("Crawl completed")
}

// screenshotJDTopCV takes a screenshot of job descript topcv
Expand Down
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,28 @@ go 1.17

require (
github.com/PuerkitoBio/goquery v1.8.0
github.com/chromedp/cdproto v0.0.0-20211126220118-81fa0469ad77
github.com/chromedp/chromedp v0.7.6
github.com/joho/godotenv v1.4.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
gopkg.in/tucnak/telebot.v2 v2.5.0
)

require (
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/chromedp/cdproto v0.0.0-20211126220118-81fa0469ad77 // indirect
github.com/chromedp/sysutil v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/gobwas/ws v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/stretchr/testify v1.7.0 // indirect
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8 // indirect
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 // indirect
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
22 changes: 21 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ github.com/chromedp/chromedp v0.7.6 h1:2juGaktzjwULlsn+DnvIZXFUckEp5xs+GOBroaea+
github.com/chromedp/chromedp v0.7.6/go.mod h1:ayT4YU/MGAALNfOg9gNrpGSAdnU51PMx+FCeuT1iXzo=
github.com/chromedp/sysutil v1.0.0 h1:+ZxhTpfpZlmchB58ih/LBHX52ky7w2VhQVKQMucy3Ic=
github.com/chromedp/sysutil v1.0.0/go.mod h1:kgWmDdq8fTzXYcKIBqIYvRRTnYb9aNS9moAV0xufSww=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=
github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
github.com/gobwas/ws v1.1.0 h1:7RFti/xnNkMJnrK7D1yQ/iCIB5OrrY/54/H930kIbHA=
github.com/gobwas/ws v1.1.0/go.mod h1:nzvNcVha5eUziGrbxFCo6qFIojQHjJV5cLYIbezhfL0=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
Expand All @@ -25,15 +30,24 @@ github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/orisano/pixelmatch v0.0.0-20210112091706-4fa4c7ba91d5 h1:1SoBaSPudixRecmlHXb/GxmaD3fLMtHIDN13QujwQuc=
github.com/orisano/pixelmatch v0.0.0-20210112091706-4fa4c7ba91d5/go.mod h1:nZgzbfBr3hhjoZnS66nKrHmduYNpc34ny7RK4z5/HM0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8 h1:/6y1LfuqNuQdHAm0jjtPtgRcxIxjVZgm5OTu8/QhZvk=
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 h1:TyHqChC80pFkXWraUUf6RuB5IqFdQieMLwwCJokV2pc=
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 h1:XfKQ4OlFl8okEOr5UvAqFRVj8pY/4yfcXrddB8qAbU0=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand All @@ -42,5 +56,11 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 h1:VpOs+IwYnYBaFnrNAeB8UUWtL3vEUnzSCL1nVjPhqrw=
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
gopkg.in/tucnak/telebot.v2 v2.5.0 h1:i+NynLo443Vp+Zn3Gv9JBjh3Z/PaiKAQwcnhNI7y6Po=
gopkg.in/tucnak/telebot.v2 v2.5.0/go.mod h1:BgaIIx50PSRS9pG59JH+geT82cfvoJU/IaI5TJdN3v8=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
41 changes: 0 additions & 41 deletions handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@ package handle
import (
"fmt"

"vieclamit/models"
"vieclamit/repository"
)

const (
collection = "vieclamit"
)

// Handle struct
type Handle struct {
Repo repository.Repository
Expand All @@ -25,39 +20,3 @@ func (h *Handle) CheckJobDeadlineExpired() error {
fmt.Printf("check job deadline time expired, removed %d\n", count)
return nil
}

// SearchJobByLocation serach all job by location
func (h *Handle) SearchJobByLocation(location string) (*models.Recruitments, error) {
recruitments, err := h.Repo.FindByLocation(location, collection)
if err != nil {
return nil, err
}
// for _, recruitment := range *recruitments {
// fmt.Println(recruitment.Location)
// }
return recruitments, nil
}

// SearchJobBySkill serach all job by skill
func (h *Handle) SearchJobBySkill(skill string) (*models.Recruitments, error) {
recruitments, err := h.Repo.FindBySkill(skill, collection)
if err != nil {
return nil, err
}
// for _, recruitment := range *recruitments {
// fmt.Println(recruitment.Title, recruitment.Company)
// }
return recruitments, nil
}

// SearchJobByCompany serach all job by company
func (h *Handle) SearchJobByCompany(company string) (*models.Recruitments, error) {
recruitments, err := h.Repo.FindByCompany(company, collection)
if err != nil {
return nil, err
}
// for _, recruitment := range *recruitments {
// fmt.Println(recruitment.Company)
// }
return recruitments, nil
}
30 changes: 19 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
package main

import (
"os"
"sync"
"time"

"vieclamit/broadcasts"
"vieclamit/database"
"vieclamit/feeds"
"vieclamit/handle"
repoimpl "vieclamit/repository/repo_impl"

"github.com/joho/godotenv"
)

func init() {
if err := godotenv.Load(".env"); err != nil {
return
}
}

func main() {

// conn db
mg := &database.Mongo{}
mg.CreateConn()

Expand All @@ -21,26 +33,22 @@ func main() {
// run crawl
var wg sync.WaitGroup
wg.Add(1)

go func() {
defer wg.Done()
feeds.TopCV(handle.Repo)
}()

wg.Wait()

// search job by keyword
// handle.SearchJobByLocation("Hà nội")
// handle.SearchJobBySkill("golang")
// handle.SearchJobByCompany("fpt")

// url screenshot JD TopCV
// feeds.ScreenshotJDTopCV("https://www.topcv.vn/viec-lam/blockchain-developers-luong-1-000-4-000-hcm/590697.html")
// feeds.ScreenshotJDTopCV("https://www.topcv.vn/brand/smartosc/tuyen-dung/it-comtor-j592057.html")
// conn telegram
telegramConfig := &broadcasts.Telegram{
Token: os.Getenv("TELEGRAM_TOKEN"),
Repo: repoimpl.NewRepo(mg),
}
telegramConfig.NewTelegram()

// schedule crawl
go schedule(6*time.Hour, handle, 1)
schedule(24*time.Hour, handle, 2)
schedule(10*time.Hour, handle, 2)
}

func schedule(timeSchedule time.Duration, handle handle.Handle, index int) {
Expand Down
Loading

0 comments on commit 3c56ffb

Please sign in to comment.