From 5df5dde5064d3b448d280d26fe29bbbbca29474b Mon Sep 17 00:00:00 2001 From: bdnugget Date: Wed, 28 May 2025 13:15:23 +0200 Subject: [PATCH] Mol improvements --- commands/mol.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/commands/mol.go b/commands/mol.go index 3d71d69..bf48fb9 100644 --- a/commands/mol.go +++ b/commands/mol.go @@ -4,7 +4,7 @@ import ( "fmt" "io" "net/http" - "regexp" + "net/url" "strings" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" @@ -34,13 +34,14 @@ func (m MolCommand) Execute(update tgbotapi.Update, bot *tgbotapi.BotAPI) { return } imgURL := fmt.Sprintf("https://pubchem.ncbi.nlm.nih.gov/image/imgsrv.fcgi?t=l&cid=%s", cid) - msg := tgbotapi.NewMessage(update.Message.Chat.ID, imgURL) - bot.Send(msg) + photo := tgbotapi.NewPhoto(update.Message.Chat.ID, tgbotapi.FileURL(imgURL)) + photo.Caption = args + bot.Send(photo) } func fetchPubchemCID(compound string) (string, error) { - url := "https://pubchem.ncbi.nlm.nih.gov/compound/" + compound - resp, err := http.Get(url) + apiURL := "https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/" + url.QueryEscape(compound) + "/cids/TXT" + resp, err := http.Get(apiURL) if err != nil { return "", err } @@ -49,12 +50,11 @@ func fetchPubchemCID(compound string) (string, error) { if err != nil { return "", err } - re := regexp.MustCompile(``) - matches := re.FindStringSubmatch(string(body)) - if len(matches) < 2 { + cid := strings.TrimSpace(string(body)) + if cid == "" || strings.Contains(cid, "Status:") { return "", fmt.Errorf("CID not found") } - return matches[1], nil + return cid, nil } func init() {