Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

line bot #20

Open
shungo0525 opened this issue Feb 26, 2023 · 3 comments
Open

line bot #20

shungo0525 opened this issue Feb 26, 2023 · 3 comments

Comments

@shungo0525
Copy link
Owner

shungo0525 commented Feb 26, 2023

 go get github.com/line/line-bot-sdk-go/v7/linebot
@shungo0525
Copy link
Owner Author

package main

import (
  "log"
  "net/http"
  "math/rand"
  "time"

  "github.com/line/line-bot-sdk-go/v7/linebot"
  "github.com/PuerkitoBio/goquery"
)

// NOTE: ハロプロ
const LINE_BOT_CHANNEL_SECRET = "b66d538b03a93231f0cf0a6da939b343"
const LINE_BOT_CHANNEL_TOKEN = "pKyiodiIqLMz77Ekiliq2jY3ZTkKOXjZ/HVyOttlThVNgfL9RnEmcO+6NY3OxkTKdGthHxp04X1BhKj4nHZ/5g4V31f/AddlVEt2dks+rS7FY/U4U8Uczb7qgsrtWNktiHtS9bg5B+AAhNYzx3bJ1gdB04t89/1O/w1cDnyilFU="
const URL = "http://hpmatome.antena.biz/"
const CLASS_NAME = "div.blog_a > a"

func main() {
  url := get_url()
  send_linebot(url)
}

func get_url() (url string) {
  res, err := http.Get(URL)
  if err != nil {
    log.Println("htmlの取得に失敗しました", err)
    return
  }
  defer res.Body.Close()

  doc, _ := goquery.NewDocumentFromReader(res.Body)
  urls := make([]string, 0)

  doc.Find(CLASS_NAME).Each(func(i int, s *goquery.Selection) {
    url, _ := s.Attr("href")
    urls = append(urls, url)
  })
  rand.Seed(time.Now().UnixNano())
  index := rand.Intn(len(urls)-1)

  return urls[index]
}

func send_linebot(url string) {
  bot, err := linebot.New(LINE_BOT_CHANNEL_SECRET,LINE_BOT_CHANNEL_TOKEN)
  if err != nil {
    log.Fatal(err)
  }

  message := linebot.NewTextMessage(url)
  
  // NOTE: テキストメッセージを友達登録しているユーザー全員に配信する
  if _, err := bot.BroadcastMessage(message).Do(); err != nil {
    log.Fatal(err)
  }
}

@shungo0525
Copy link
Owner Author

  • Lambda
    • ローカルでも実行できるようにする
  • API Gateway

@shungo0525
Copy link
Owner Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant