-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
216 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |