aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <m.shulhan@gmail.com>2020-04-11 04:00:59 +0700
committerShulhan <m.shulhan@gmail.com>2020-04-11 04:00:59 +0700
commitd044fe5904d4a5301ad32b85cb5a61cf6326ea57 (patch)
treeab8fd059533ec93ce304007fcc3b5565c9d95d5c
parent1fb4ed2592fa4f077184a9ca52a0af738f4fbaf9 (diff)
downloadkamusku-d044fe5904d4a5301ad32b85cb5a61cf6326ea57.tar.xz
all: implementasi bot untuk Telegram
-rw-r--r--.gitignore4
-rw-r--r--Makefile5
-rw-r--r--cmd/bot-kbbi/main.go35
-rw-r--r--go.mod2
-rw-r--r--go.sum4
-rw-r--r--telegram_bot.go142
6 files changed, 188 insertions, 4 deletions
diff --git a/.gitignore b/.gitignore
index e8d0b52..e1376d0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,7 @@
-/cmd/www-kbbi/static.go
/_www-kbbi/bin/*
+/bot-kbbi
+/bot-kbbi-linux-amd64
+/cmd/www-kbbi/static.go
/internal/cmd/mergedic/id_ID.dic
/internal/cmd/mergedic/id_ID.dic.new
/kamus.gob
diff --git a/Makefile b/Makefile
index 15a9875..e1a8775 100644
--- a/Makefile
+++ b/Makefile
@@ -49,6 +49,11 @@ deploy:
rsync --progress ./www-kbbi-linux-amd64 www-kbbi:~/bin/www-kbbi
rsync --progress --recursive ./_www-kbbi/ www-kbbi:~/bin/_www-kbbi/
+deploy-bot:
+ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
+ go build -o bot-kbbi-linux-amd64 ./cmd/bot-kbbi/
+ rsync --progress ./bot-kbbi-linux-amd64 www-kbbi:~/bin/bot-kbbi
+
##
## Development task
##
diff --git a/cmd/bot-kbbi/main.go b/cmd/bot-kbbi/main.go
new file mode 100644
index 0000000..66b2edf
--- /dev/null
+++ b/cmd/bot-kbbi/main.go
@@ -0,0 +1,35 @@
+// Copyright 2020, Shulhan <m.shulhan@gmail.com>. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+ "log"
+ "os"
+ "os/signal"
+
+ "github.com/shuLhan/kbbi"
+)
+
+func main() {
+ log.SetFlags(0)
+
+ // Use the token and Webhook URL from environment variables.
+ tgbot, err := kbbi.NewTelegramBot("", "")
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ go func() {
+ err := tgbot.Start()
+ if err != nil {
+ log.Println(err)
+ }
+ }()
+ defer tgbot.Stop()
+
+ c := make(chan os.Signal, 1)
+ signal.Notify(c, os.Interrupt)
+ <-c
+}
diff --git a/go.mod b/go.mod
index 9e964b4..dd04ad8 100644
--- a/go.mod
+++ b/go.mod
@@ -3,7 +3,7 @@ module github.com/shuLhan/kbbi
go 1.13
require (
- github.com/shuLhan/share v0.14.1-0.20200405143508-d34b820adf64
+ github.com/shuLhan/share v0.14.1-0.20200410205026-5378abd1e1f4
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e
)
diff --git a/go.sum b/go.sum
index 862d195..75c6bb7 100644
--- a/go.sum
+++ b/go.sum
@@ -1,5 +1,5 @@
-github.com/shuLhan/share v0.14.1-0.20200405143508-d34b820adf64 h1:1UTOAeD7Jlon7/hJBLXDDxKjVNFOVrCAY1gKx7NyILg=
-github.com/shuLhan/share v0.14.1-0.20200405143508-d34b820adf64/go.mod h1:mpa0ub5qmuko/muUlOROOqLCSHKU76GzuAR/sUaSwRo=
+github.com/shuLhan/share v0.14.1-0.20200410205026-5378abd1e1f4 h1:NgTmO/2wMlcENPxK3EOIYFfdNEmz5QzcVOkDplxSluI=
+github.com/shuLhan/share v0.14.1-0.20200410205026-5378abd1e1f4/go.mod h1:mpa0ub5qmuko/muUlOROOqLCSHKU76GzuAR/sUaSwRo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
diff --git a/telegram_bot.go b/telegram_bot.go
new file mode 100644
index 0000000..f096823
--- /dev/null
+++ b/telegram_bot.go
@@ -0,0 +1,142 @@
+// Copyright 2020, Shulhan <m.shulhan@gmail.com>. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package kbbi
+
+import (
+ "bytes"
+ "fmt"
+ "log"
+ "strings"
+
+ "github.com/shuLhan/share/api/telegram/bot"
+)
+
+const (
+ defAddress = ":1928"
+
+ commandBantuan = "bantuan"
+ commandDefinisi = "definisi"
+)
+
+//
+// TelegramBot implement Telegram Bot for KBBI.
+//
+type TelegramBot struct {
+ *bot.Bot
+ apiClient *apiClient
+}
+
+//
+// NewTelegramBot create and initialize new Telegram Bot.
+//
+func NewTelegramBot(token, webhookURL string) (tgbot *TelegramBot, err error) {
+ tgbot = &TelegramBot{
+ apiClient: newAPIClient(""),
+ }
+
+ var commands = []bot.Command{{
+ Command: commandDefinisi,
+ Description: "Cari definisi dari satu atau lebih kata." +
+ " Pisahkan antara kata dengan koma.",
+ Handler: tgbot.handleCommandDefinisi,
+ }}
+
+ opts := bot.Options{
+ Token: token,
+ HandleUpdate: tgbot.handleUpdate,
+ Webhook: &bot.Webhook{
+ URL: webhookURL,
+ ListenAddress: defAddress,
+ },
+ }
+
+ tgbot.Bot, err = bot.New(opts)
+ if err != nil {
+ return nil, fmt.Errorf("NewTelegramBot: %w", err)
+ }
+
+ err = tgbot.SetMyCommands(commands)
+ if err != nil {
+ return nil, fmt.Errorf("NewTelegramBot: %w", err)
+ }
+
+ return tgbot, nil
+}
+
+//
+// handleUpdate process the Update information send by Telegram server.
+//
+func (tgbot *TelegramBot) handleUpdate(update bot.Update) {
+ if update.Message == nil {
+ return
+ }
+ fmt.Printf("update.Message: %+v\n", update.Message)
+}
+
+//
+// handleCommandDefinisi lookup the words definition and reply back to client.
+//
+func (tgbot *TelegramBot) handleCommandDefinisi(update bot.Update) {
+ msgReq := update.Message
+ fmt.Printf("handleCommandDefinisi: %q %q\n", msgReq.Command,
+ msgReq.CommandArgs)
+
+ daftarKata := strings.Split(msgReq.CommandArgs, ",")
+
+ def, err := tgbot.apiClient.CariDefinisi(daftarKata)
+ if err != nil {
+ tgbot.sendError(msgReq, err)
+ return
+ }
+
+ text := formatText(def)
+
+ _, err = tgbot.SendMessage(msgReq, bot.ParseModeHTML, text)
+ if err != nil {
+ log.Printf("handleCommandDefinisi: %s", err)
+ return
+ }
+}
+
+func (tgbot *TelegramBot) sendError(msg *bot.Message, err error) {
+ _, err = tgbot.SendMessage(msg, "", err.Error())
+ if err != nil {
+ log.Printf("sendError: %s", err)
+ return
+ }
+}
+
+func formatText(definisiKata DefinisiResponse) string {
+ buf := &bytes.Buffer{}
+ for k, kata := range definisiKata {
+ fmt.Fprintf(buf, "<b>%s</b>\n", k)
+
+ if len(kata.Dasar) > 0 {
+ fmt.Fprintf(buf, " Kata dasar: <i>%s</i>\n\n",
+ kata.Dasar)
+ }
+
+ for x, def := range kata.Definisi {
+ fmt.Fprintf(buf, " Definisi #%d: %s\n", x+1, def.Isi)
+
+ if len(def.Kelas) > 0 {
+ fmt.Fprintln(buf, " Kelas kata,")
+ for _, kelas := range def.Kelas {
+ fmt.Fprintln(buf, " - "+kelas)
+ }
+ }
+
+ if len(def.Contoh) > 0 {
+ fmt.Fprintln(buf, " Contoh,")
+ for _, contoh := range def.Contoh {
+ fmt.Fprintln(buf, " - "+contoh)
+ }
+ }
+ fmt.Fprintln(buf, "")
+ }
+ }
+
+ return buf.String()
+}