Skip to content

Commit

Permalink
Merge branch 'summarizer'
Browse files Browse the repository at this point in the history
  • Loading branch information
100nandoo committed Feb 11, 2024
2 parents c0410df + 83f72eb commit 9244e48
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 1 deletion.
5 changes: 4 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package config

const SupabaseUrl, SupabaseKey string = "SUPABASE_URL", "SUPABASE_KEY"
const TelegramBot string = "TELEGRAM_BOT"
const CaptainKiddBot string = "CAPTAIN_KIDD_BOT"
const TelegramDebug string = "TELEGRAM_CHANNEL_DEBUG"
const TelegramFreeGames string = "TELEGRAM_CHANNEL_FREE_GAMES"
const TelegramRemoteOk string = "TELEGRAM_CHANNEL_REMOTE_OK"
const TelegramRemoteOk string = "TELEGRAM_CHANNEL_REMOTE_OK"

const SmmryKey string = "SMMRY_KEY"
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"gobot/freegames/app"
"gobot/pkg"
"gobot/remoteok"
"gobot/summarizer"
)

func main() {
Expand All @@ -15,5 +16,6 @@ func main() {
remoteok.Scouting()
remoteok.Cleaning()

go summarizer.Run()
pkg.StartBlocking()
}
76 changes: 76 additions & 0 deletions summarizer/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package summarizer

import (
"bytes"
"encoding/json"
"fmt"
"gobot/config"
"net/http"
"net/url"
"os"
)

const (
baseURL = "https://api.smmry.com/"
paramAPIKey = "SM_API_KEY"
paramURL = "SM_URL"
paramInput = "sm_api_input"
)

func SummarizeURL(link string) (*SmmryResponse, *ErrorResponse) {
params := url.Values{}
params.Set(paramAPIKey, os.Getenv(config.SmmryKey))
params.Set(paramURL, link)

reqURL := baseURL + "?" + params.Encode()

resp, err := http.Get(reqURL)
if err != nil {
return nil, &ErrorResponse{SmAPIMessage: err.Error()}
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, &ErrorResponse{SmAPIMessage: fmt.Sprintf("HTTP Status: %s", resp.Status)}
}

var smmryResponse SmmryResponse
if err := json.NewDecoder(resp.Body).Decode(&smmryResponse); err != nil {
fmt.Println("Error decoding JSON:", err)
return nil, &ErrorResponse{SmAPIMessage: "Error decoding JSON response"}
}

return &smmryResponse, nil
}

func SummarizeText(text string) (*SmmryResponse, *ErrorResponse) {
params := url.Values{}
params.Set(paramAPIKey, os.Getenv(config.SmmryKey))
reqURL := baseURL + "?" + params.Encode()

textBody := paramInput + "=" + text
body := []byte(textBody)
r, err := http.NewRequest("POST", reqURL, bytes.NewBuffer(body))
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r.Header.Add("Expect", "")
if err != nil {
panic(err)
}

client := &http.Client{}
res, err := client.Do(r)
if err != nil {
panic(err)
}

defer res.Body.Close()

smmryResponse := &SmmryResponse{}

derr := json.NewDecoder(res.Body).Decode(smmryResponse)
if derr != nil {
panic(derr)
}

return smmryResponse, nil
}
26 changes: 26 additions & 0 deletions summarizer/formatter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package summarizer

import (
"fmt"
"strings"
)

func (s SmmryResponse) ToMarkdownString() string {
var builder strings.Builder

if len(s.SmAPITitle) > 0 {
builder.WriteString("*")
builder.WriteString(s.SmAPITitle)
builder.WriteString("*\n\n")
}

sentences := strings.Split(s.SmAPIContent, ". ")
for _, sentence := range sentences {
builder.WriteString(sentence)
builder.WriteString(".\n\n")
}

builder.WriteString(fmt.Sprintf("\nContent reduced by *%s*", s.SmAPIContentReduced))

return builder.String()
}
14 changes: 14 additions & 0 deletions summarizer/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package summarizer

type SmmryResponse struct {
SmAPICharacterCount string `json:"sm_api_character_count"`
SmAPIContentReduced string `json:"sm_api_content_reduced"`
SmAPITitle string `json:"sm_api_title"`
SmAPIContent string `json:"sm_api_content"`
SmAPILimitation string `json:"sm_api_limitation"`
}

type ErrorResponse struct {
SmAPIError int `json:"sm_api_error"`
SmAPIMessage string `json:"sm_api_message"`
}
94 changes: 94 additions & 0 deletions summarizer/telegram.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package summarizer

import (
"fmt"
"gobot/config"
"log"
"os"
"regexp"
"strings"
"time"

"gopkg.in/telebot.v3"
tele "gopkg.in/telebot.v3"
)

const (
regexUrl = `(?i)\b(?:https?://|www\.)\S+\b`

helpMessage = `Hello this is Captain Kidd Bot
*Features:*
- Summarise web article
- Summarise paragraph text
*How to:*
Simply send an url or paragraph text to this bot
Captain Kidd bot made with ❤️ by @crossix`
)

func Run() {
println("Run Summarizer")
pref := tele.Settings{
Token: os.Getenv(config.CaptainKiddBot),
Poller: &tele.LongPoller{Timeout: 10 * time.Second},
}

b, err := tele.NewBot(pref)
if err != nil {
log.Fatal(err)
return
}

b.Handle("/start", func(c tele.Context) error {
return c.Send(helpMessage, &telebot.SendOptions{
ParseMode: telebot.ModeMarkdown,
})
})

b.Handle("/help", func(c tele.Context) error {
return c.Send(helpMessage, &telebot.SendOptions{
ParseMode: telebot.ModeMarkdown,
})
})

b.Handle("/s", func(c tele.Context) error {
return c.Send(helpMessage, &telebot.SendOptions{
ParseMode: telebot.ModeMarkdown,
})
})

b.Handle(telebot.OnText, func(c tele.Context) error {
pattern := regexp.MustCompile(regexUrl)
if pattern.MatchString(c.Text()) {
fmt.Println("url pattern")
smmryResponse, errResponse := SummarizeURL(c.Text())
if errResponse != nil {
fmt.Println("Error:", errResponse.SmAPIMessage)
return c.Send("Something went wrong...")
}
return c.Send(smmryResponse.ToMarkdownString(), &telebot.SendOptions{
ParseMode: telebot.ModeMarkdown,
})
} else {
words := strings.Fields(c.Text())
wordCount := len(words)

if wordCount > 100 {
fmt.Println("text pattern")
smmryResponse, errResponse := SummarizeText(c.Text())
if errResponse != nil {
fmt.Println("Error:", errResponse.SmAPIMessage)
return c.Send("Something went wrong...")
}
return c.Send(smmryResponse.ToMarkdownString(), &telebot.SendOptions{
ParseMode: telebot.ModeMarkdown,
})
}
return c.Send("Other regex")
}
})

b.Start()
}

0 comments on commit 9244e48

Please sign in to comment.