aboutsummaryrefslogtreecommitdiff
path: root/telegram_bot.go
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 /telegram_bot.go
parent1fb4ed2592fa4f077184a9ca52a0af738f4fbaf9 (diff)
downloadkamusku-d044fe5904d4a5301ad32b85cb5a61cf6326ea57.tar.xz
all: implementasi bot untuk Telegram
Diffstat (limited to 'telegram_bot.go')
-rw-r--r--telegram_bot.go142
1 files changed, 142 insertions, 0 deletions
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()
+}