Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
cloud.google.com/go/datastore v1.20.0
cloud.google.com/go/secretmanager v1.16.0
github.com/joho/godotenv v1.5.1
github.com/julwrites/BotPlatform v0.0.0-20220206144002-60e1b8060734
github.com/julwrites/BotPlatform v0.0.0-20251128175347-656700b2e4d4
golang.org/x/net v0.43.0
google.golang.org/api v0.247.0
gopkg.in/yaml.v2 v2.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ github.com/googleapis/gax-go/v2 v2.15.0 h1:SyjDc1mGgZU5LncH8gimWo9lW1DtIfPibOG81
github.com/googleapis/gax-go/v2 v2.15.0/go.mod h1:zVVkkxAQHa1RQpg9z2AUCMnKhi0Qld9rcmyfL1OZhoc=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/julwrites/BotPlatform v0.0.0-20220206144002-60e1b8060734 h1:U/z8aO/8zMpOzdR7kK9hnHfXber1fHa7FWlXGeuG3Yc=
github.com/julwrites/BotPlatform v0.0.0-20220206144002-60e1b8060734/go.mod h1:RAVF1PibRuRYv1Z7VxNapzrikBrjtF48aFPCoCVnLpM=
github.com/julwrites/BotPlatform v0.0.0-20251128175347-656700b2e4d4 h1:NeEPkJt4VXvyb/zxggO9zFs9tAJ07iiht1lAkUL/ZNs=
github.com/julwrites/BotPlatform v0.0.0-20251128175347-656700b2e4d4/go.mod h1:PZT+yPLr4MrricOGOhXwiJCurNcGj36fD1jZOwMiuIk=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down
15 changes: 10 additions & 5 deletions pkg/app/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import (
"strings"

"github.com/julwrites/BotPlatform/pkg/def"
"github.com/julwrites/ScriptureBot/pkg/secrets"
"github.com/julwrites/ScriptureBot/pkg/utils"
)

func DumpUserList(env def.SessionData) def.SessionData {
if env.User.Id == env.Secrets.ADMIN_ID {
adminID, _ := secrets.Get("TELEGRAM_ADMIN_ID")
if env.User.Id == adminID {
var message = ""

users := utils.GetAllUsers(env.Secrets.PROJECT_ID)
projectID, _ := secrets.Get("GCLOUD_PROJECT_ID")
users := utils.GetAllUsers(projectID)
message += fmt.Sprintf("%d Users: \n", len(users))
for _, user := range users {
message += user.Firstname + " " + user.Lastname + " - @" + user.Username + "\n"
Expand All @@ -27,8 +30,10 @@ func DumpUserList(env def.SessionData) def.SessionData {
}

func Migrate(env def.SessionData) def.SessionData {
if env.User.Id == env.Secrets.ADMIN_ID {
users := utils.GetAllUsers(env.Secrets.PROJECT_ID)
adminID, _ := secrets.Get("TELEGRAM_ADMIN_ID")
if env.User.Id == adminID {
projectID, _ := secrets.Get("GCLOUD_PROJECT_ID")
users := utils.GetAllUsers(projectID)
for _, user := range users {
config := utils.DeserializeUserConfig(user.Config)
if len(config.Subscriptions) > 0 {
Expand All @@ -50,7 +55,7 @@ func Migrate(env def.SessionData) def.SessionData {

user.Config = utils.SerializeUserConfig(config)

utils.PushUser(user, env.Secrets.PROJECT_ID)
utils.PushUser(user, projectID)
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/app/ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/julwrites/BotPlatform/pkg/def"
"github.com/julwrites/ScriptureBot/pkg/secrets"
"github.com/julwrites/ScriptureBot/pkg/utils"
)

Expand All @@ -25,7 +26,8 @@ func GetBibleAsk(env def.SessionData) def.SessionData {
}

var resp OQueryResponse
err := SubmitQuery(req, &resp, env.Secrets.PROJECT_ID)
projectID, _ := secrets.Get("GCLOUD_PROJECT_ID")
err := SubmitQuery(req, &resp, projectID)
if err != nil {
log.Printf("Error asking bible: %v", err)
env.Res.Message = "Sorry, I encountered an error processing your question."
Expand Down
9 changes: 6 additions & 3 deletions pkg/app/command.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package app

import "github.com/julwrites/BotPlatform/pkg/def"
import (
"github.com/julwrites/BotPlatform/pkg/def"
"github.com/julwrites/BotPlatform/pkg/platform"
)

func ProcessCommand(env def.SessionData) def.SessionData {
func ProcessCommand(env def.SessionData, bot platform.Platform) def.SessionData {
switch env.Msg.Command {
case ADM_CMD_DUMP:
env = DumpUserList(env)
Expand All @@ -17,7 +20,7 @@ func ProcessCommand(env def.SessionData) def.SessionData {
env = GetTMSVerse(env)
break
case CMD_DEVO:
env = GetDevo(env)
env = GetDevo(env, bot)
break
case CMD_SUBSCRIBE:
env = UpdateSubscription(env)
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/devo.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func GetDevotionalData(env def.SessionData, devo string) def.ResponseData {
return response
}

func GetDevo(env def.SessionData) def.SessionData {
func GetDevo(env def.SessionData, bot platform.Platform) def.SessionData {
switch env.User.Action {
case CMD_DEVO:
log.Printf("Detected existing action /devo")
Expand All @@ -180,7 +180,7 @@ func GetDevo(env def.SessionData) def.SessionData {
env.Res.Message = "Just a moment..."

log.Printf("Affordances before posting: %+v", env.Res.Affordances)
platform.PostFromProps(env)
bot.Post(env)

// Retrieve devotional
env.Res = GetDevotionalData(env, devo)
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/devo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestGetDevo(t *testing.T) {
env.User.Action = ""
env.Msg.Message = CMD_DEVO

env = GetDevo(env)
env = GetDevo(env, &MockBot{})
if len(env.Res.Message) == 0 {
t.Error("Failed TestGetDevo initial, no message")
}
Expand All @@ -123,7 +123,7 @@ func TestGetDevo(t *testing.T) {
env.Msg.Message = devoName
env.ResourcePath = "../../resource"

env = GetDevo(env)
env = GetDevo(env, &MockBot{})

if len(env.Res.Message) == 0 && len(env.Res.Affordances.Options) == 0 {
t.Fatalf("Failed TestGetDevo for %s: no message or affordances", devoName)
Expand Down
11 changes: 6 additions & 5 deletions pkg/app/passage.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/julwrites/BotPlatform/pkg/def"
"github.com/julwrites/BotPlatform/pkg/platform"
"github.com/julwrites/ScriptureBot/pkg/secrets"
"github.com/julwrites/ScriptureBot/pkg/utils"
)

Expand Down Expand Up @@ -54,10 +55,9 @@ func ParseNodesForPassage(node *html.Node) string {
switch tag := child.Data; tag {
case "span":
childText := ParseNodesForPassage(child)
if len(childText) > 0 {
parts = append(parts, childText)
} else {
parts = append(parts, child.Data)
parts = append(parts, childText)
if len(strings.TrimSpace(childText)) > 0 {
parts = append(parts, "\n")
}
case "sup":
isFootnote := func(node *html.Node) bool {
Expand Down Expand Up @@ -200,7 +200,8 @@ func GetBiblePassage(env def.SessionData) def.SessionData {
}

var resp VerseResponse
err := SubmitQuery(req, &resp, env.Secrets.PROJECT_ID)
projectID, _ := secrets.Get("GCLOUD_PROJECT_ID")
err := SubmitQuery(req, &resp, projectID)

// Fallback to direct passage retrieval logic
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/passage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestParsePassageFromHtml(t *testing.T) {

t.Run("HTML with spans", func(t *testing.T) {
html := `<p><span>Line 1.</span><br><span> </span><span>Line 2.</span></p>`
expected := "Line 1.\n Line 2."
expected := "Line 1.\n\n Line 2."
if got := ParsePassageFromHtml("", html, ""); got != expected {
t.Errorf("ParsePassageFromHtml() = %v, want %v", got, expected)
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/app/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/julwrites/BotPlatform/pkg/def"
"github.com/julwrites/ScriptureBot/pkg/secrets"
"github.com/julwrites/ScriptureBot/pkg/utils"
)

Expand Down Expand Up @@ -38,7 +39,8 @@ func GetBibleSearch(env def.SessionData) def.SessionData {
}

var resp WordSearchResponse
err := SubmitQuery(req, &resp, env.Secrets.PROJECT_ID)
projectID, _ := secrets.Get("GCLOUD_PROJECT_ID")
err := SubmitQuery(req, &resp, projectID)
if err != nil {
log.Printf("Error searching bible: %v", err)
env.Res.Message = "Sorry, I encountered an error while searching."
Expand Down
12 changes: 12 additions & 0 deletions pkg/app/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@ package app

import (
"os"

"github.com/julwrites/BotPlatform/pkg/def"
)

type MockBot struct{}

func (b *MockBot) Translate(body []byte) (def.SessionData, error) {
return def.SessionData{}, nil
}

func (b *MockBot) Post(env def.SessionData) bool {
return true
}

// SetEnv is a helper function to temporarily set an environment variable and return a function to restore it.
func SetEnv(key, value string) func() {
originalValue, isSet := os.LookupEnv(key)
Expand Down
9 changes: 5 additions & 4 deletions pkg/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"

"github.com/julwrites/BotPlatform/pkg/def"
"github.com/julwrites/BotPlatform/pkg/platform"

"github.com/julwrites/ScriptureBot/pkg/app"
)
Expand All @@ -17,7 +18,7 @@ func HelpMessage(env *def.SessionData) string {
//\n/subscribe - Subscribe to / Unsubscribe from daily reading material
}

func RunCommands(env def.SessionData) def.SessionData {
func RunCommands(env def.SessionData, bot platform.Platform) def.SessionData {
if len(env.User.Action) > 0 {
log.Printf("Detected user has active action %s", env.User.Action)
env.Msg.Command = env.User.Action
Expand All @@ -27,13 +28,13 @@ func RunCommands(env def.SessionData) def.SessionData {
env.Msg.Command = app.CMD_CLOSE
}

env = app.ProcessCommand(env)
env = app.ProcessCommand(env, bot)

return env
}

func HandleBotLogic(env def.SessionData) def.SessionData {
env = RunCommands(env)
func HandleBotLogic(env def.SessionData, bot platform.Platform) def.SessionData {
env = RunCommands(env, bot)

// log.Printf("Commands run, resulting message: %s", env.Res.Message)
if len(env.Res.Message) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/bot/bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestRunCommands(t *testing.T) {
env.User.Config = utils.SerializeUserConfig(conf)
env.Msg.Message = "psalm 1"

env = RunCommands(env)
env = RunCommands(env, &app.MockBot{})

if !strings.Contains(env.Res.Message, "wicked") && !strings.Contains(env.Res.Message, "Blessed") {
t.Errorf("Failed TestRunCommands Passage command. Got: %s", env.Res.Message)
Expand Down
25 changes: 10 additions & 15 deletions pkg/bot/sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (

"github.com/julwrites/BotPlatform/pkg/def"
"github.com/julwrites/BotPlatform/pkg/platform"
bpsecrets "github.com/julwrites/BotPlatform/pkg/secrets"
"github.com/julwrites/ScriptureBot/pkg/app"
"github.com/julwrites/ScriptureBot/pkg/secrets"
"github.com/julwrites/ScriptureBot/pkg/utils"
)

func HandleSubscriptionLogic(env def.SessionData) def.SessionData {
func HandleSubscriptionLogic(env def.SessionData, bot platform.Platform) def.SessionData {
user := env.User
config := utils.DeserializeUserConfig(user.Config)

Expand All @@ -25,30 +24,30 @@ func HandleSubscriptionLogic(env def.SessionData) def.SessionData {
env.Res.Message = "Here are today's devotions!"
env.Res.Affordances.Remove = true

platform.PostFromProps(env)
bot.Post(env)

// Send the devotional
for _, devo := range subscriptions {
// Retrieve devotional
log.Printf("Getting data for <%s>", devo)
env.Res = app.GetDevotionalData(env, devo)

platform.PostFromProps(env)
bot.Post(env)
}
}

return env
}

func HandleSubscriptionPublish(env def.SessionData) def.SessionData {
func HandleSubscriptionPublish(env def.SessionData, bot platform.Platform, projectID string) def.SessionData {
// Check all users
users := utils.GetAllUsers(env.Secrets.PROJECT_ID)
users := utils.GetAllUsers(projectID)
log.Printf("Retrieved %d users", len(users))
for _, user := range users {
env.User = user
env.User.Action = app.CMD_DEVO

env = HandleSubscriptionLogic(env)
env = HandleSubscriptionLogic(env, bot)
}

return env
Expand All @@ -57,23 +56,19 @@ func HandleSubscriptionPublish(env def.SessionData) def.SessionData {
func SubscriptionHandler(localSecrets *secrets.SecretsData) {
env := def.SessionData{}

platformSecrets := bpsecrets.SecretsData{
TELEGRAM_ID: localSecrets.TELEGRAM_ID,
PROJECT_ID: localSecrets.PROJECT_ID,
}
bot := platform.NewTelegram(localSecrets.TELEGRAM_ID, localSecrets.TELEGRAM_ADMIN_ID)

env.Secrets = platformSecrets
log.Printf("Loaded secrets...")
// log.Printf("Loaded secrets...")

env.ResourcePath = "/go/bin/"

// TODO: Iterate through types
env.Type = def.TYPE_TELEGRAM

env = HandleSubscriptionPublish(env)
env = HandleSubscriptionPublish(env, bot, localSecrets.PROJECT_ID)
log.Printf("Handled bot publish...")

if !platform.PostFromProps(env) {
if !bot.Post(env) {
log.Printf("This message was not translatable from bot language")
return
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/bot/sub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/julwrites/BotPlatform/pkg/def"
"github.com/julwrites/ScriptureBot/pkg/app"
"github.com/julwrites/ScriptureBot/pkg/utils"
)

Expand All @@ -16,7 +17,7 @@ func TestHandleSubscriptionLogic(t *testing.T) {
conf.Subscriptions = "DTMSV"
env.User.Config = utils.SerializeUserConfig(conf)

env = HandleSubscriptionLogic(env)
env = HandleSubscriptionLogic(env, &app.MockBot{})

if len(env.Res.Message) == 0 {
t.Errorf("Failed TestHandleSubscriptionLogic")
Expand Down
Loading
Loading