reordering
This commit is contained in:
117
services/connectors/discord.go
Normal file
117
services/connectors/discord.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package connectors
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/mudler/local-agent-framework/core/agent"
|
||||
"github.com/mudler/local-agent-framework/pkg/xlog"
|
||||
)
|
||||
|
||||
type Discord struct {
|
||||
token string
|
||||
defaultChannel string
|
||||
}
|
||||
|
||||
// NewDiscord creates a new Discord connector
|
||||
// with the given configuration
|
||||
// - token: Discord token
|
||||
// - defaultChannel: Discord channel to always answer even if not mentioned
|
||||
func NewDiscord(config map[string]string) *Discord {
|
||||
return &Discord{
|
||||
token: config["token"],
|
||||
defaultChannel: config["defaultChannel"],
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Discord) AgentResultCallback() func(state agent.ActionState) {
|
||||
return func(state agent.ActionState) {
|
||||
// Send the result to the bot
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Discord) AgentReasoningCallback() func(state agent.ActionCurrentState) bool {
|
||||
return func(state agent.ActionCurrentState) bool {
|
||||
// Send the reasoning to the bot
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Discord) Start(a *agent.Agent) {
|
||||
|
||||
Token := d.token
|
||||
// Create a new Discord session using the provided bot token.
|
||||
dg, err := discordgo.New(Token)
|
||||
if err != nil {
|
||||
xlog.Info("error creating Discord session,", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Register the messageCreate func as a callback for MessageCreate events.
|
||||
dg.AddHandler(d.messageCreate(a))
|
||||
|
||||
// In this example, we only care about receiving message events.
|
||||
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages | discordgo.IntentMessageContent
|
||||
|
||||
// Open a websocket connection to Discord and begin listening.
|
||||
err = dg.Open()
|
||||
if err != nil {
|
||||
xlog.Info("error opening connection,", err)
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
xlog.Info("Discord bot is now running. Press CTRL-C to exit.")
|
||||
<-a.Context().Done()
|
||||
dg.Close()
|
||||
xlog.Info("Discord bot is now stopped.")
|
||||
}()
|
||||
}
|
||||
|
||||
// This function will be called (due to AddHandler above) every time a new
|
||||
// message is created on any channel that the authenticated bot has access to.
|
||||
func (d *Discord) messageCreate(a *agent.Agent) func(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
return func(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
// Ignore all messages created by the bot itself
|
||||
// This isn't required in this specific example but it's a good practice.
|
||||
if m.Author.ID == s.State.User.ID {
|
||||
return
|
||||
}
|
||||
interact := func() {
|
||||
//m := m.ContentWithMentionsReplaced()
|
||||
content := m.Content
|
||||
|
||||
content = strings.ReplaceAll(content, "<@"+s.State.User.ID+"> ", "")
|
||||
xlog.Info("Received message", "content", content)
|
||||
job := a.Ask(
|
||||
agent.WithText(
|
||||
content,
|
||||
),
|
||||
)
|
||||
if job.Error != nil {
|
||||
xlog.Info("error asking agent,", job.Error)
|
||||
return
|
||||
}
|
||||
|
||||
xlog.Info("Response", "response", job.Response)
|
||||
_, err := s.ChannelMessageSend(m.ChannelID, job.Response)
|
||||
if err != nil {
|
||||
xlog.Info("error sending message,", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Interact if we are mentioned
|
||||
for _, mention := range m.Mentions {
|
||||
if mention.ID == s.State.User.ID {
|
||||
go interact()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Or we are in the default channel (if one is set!)
|
||||
if d.defaultChannel != "" && m.ChannelID == d.defaultChannel {
|
||||
go interact()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
196
services/connectors/githubissue.go
Normal file
196
services/connectors/githubissue.go
Normal file
@@ -0,0 +1,196 @@
|
||||
package connectors
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-github/v61/github"
|
||||
"github.com/mudler/local-agent-framework/core/agent"
|
||||
"github.com/mudler/local-agent-framework/pkg/xlog"
|
||||
|
||||
"github.com/sashabaranov/go-openai"
|
||||
)
|
||||
|
||||
type GithubIssues struct {
|
||||
token string
|
||||
repository string
|
||||
owner string
|
||||
replyIfNoReplies bool
|
||||
agent *agent.Agent
|
||||
pollInterval time.Duration
|
||||
client *github.Client
|
||||
}
|
||||
|
||||
// NewGithubIssueWatcher creates a new GithubIssues connector
|
||||
// with the given configuration
|
||||
// - token: Github token
|
||||
// - repository: Github repository name
|
||||
// - owner: Github repository owner
|
||||
// - replyIfNoReplies: If true, the bot will reply to issues with no comments
|
||||
func NewGithubIssueWatcher(config map[string]string) *GithubIssues {
|
||||
client := github.NewClient(nil).WithAuthToken(config["token"])
|
||||
replyIfNoReplies := false
|
||||
if config["replyIfNoReplies"] == "true" {
|
||||
replyIfNoReplies = true
|
||||
}
|
||||
|
||||
interval, err := time.ParseDuration(config["pollInterval"])
|
||||
if err != nil {
|
||||
interval = 10 * time.Minute
|
||||
}
|
||||
|
||||
return &GithubIssues{
|
||||
client: client,
|
||||
token: config["token"],
|
||||
repository: config["repository"],
|
||||
owner: config["owner"],
|
||||
replyIfNoReplies: replyIfNoReplies,
|
||||
pollInterval: interval,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GithubIssues) AgentResultCallback() func(state agent.ActionState) {
|
||||
return func(state agent.ActionState) {
|
||||
// Send the result to the bot
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GithubIssues) AgentReasoningCallback() func(state agent.ActionCurrentState) bool {
|
||||
return func(state agent.ActionCurrentState) bool {
|
||||
// Send the reasoning to the bot
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GithubIssues) Start(a *agent.Agent) {
|
||||
// Start the connector
|
||||
g.agent = a
|
||||
|
||||
go func() {
|
||||
ticker := time.NewTicker(g.pollInterval)
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
xlog.Info("Looking into github issues...")
|
||||
g.issuesService()
|
||||
case <-a.Context().Done():
|
||||
xlog.Info("GithubIssues connector is now stopping")
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (g *GithubIssues) issuesService() {
|
||||
user, _, err := g.client.Users.Get(g.agent.Context(), "")
|
||||
if err != nil {
|
||||
fmt.Printf("\nerror: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
issues, _, err := g.client.Issues.ListByRepo(
|
||||
g.agent.Context(),
|
||||
g.owner,
|
||||
g.repository,
|
||||
&github.IssueListByRepoOptions{})
|
||||
if err != nil {
|
||||
xlog.Info("Error listing issues", err)
|
||||
}
|
||||
for _, issue := range issues {
|
||||
// Do something with the issue
|
||||
if issue.IsPullRequest() {
|
||||
continue
|
||||
}
|
||||
labels := []string{}
|
||||
for _, l := range issue.Labels {
|
||||
labels = append(labels, l.GetName())
|
||||
}
|
||||
|
||||
// Get user that opened the issue
|
||||
userNameLogin := issue.GetUser().Login
|
||||
userName := ""
|
||||
if userNameLogin != nil {
|
||||
userName = *userNameLogin
|
||||
}
|
||||
|
||||
if userName == user.GetLogin() {
|
||||
xlog.Info("Ignoring issue opened by the bot")
|
||||
continue
|
||||
}
|
||||
messages := []openai.ChatCompletionMessage{
|
||||
{
|
||||
Role: "system",
|
||||
Content: fmt.Sprintf(
|
||||
`This is a conversation with an user ("%s") that opened a Github issue with title "%s" in the repository "%s" owned by "%s". The issue is the issue number %d. Current labels: %+v`, userName, issue.GetTitle(), g.repository, g.owner, issue.GetNumber(), labels),
|
||||
},
|
||||
{
|
||||
Role: "user",
|
||||
Content: issue.GetBody(),
|
||||
},
|
||||
}
|
||||
|
||||
comments, _, _ := g.client.Issues.ListComments(g.agent.Context(), g.owner, g.repository, issue.GetNumber(),
|
||||
&github.IssueListCommentsOptions{})
|
||||
|
||||
mustAnswer := false
|
||||
botAnsweredAlready := false
|
||||
for i, comment := range comments {
|
||||
role := "user"
|
||||
if comment.GetUser().GetLogin() == user.GetLogin() {
|
||||
botAnsweredAlready = true
|
||||
role = "assistant"
|
||||
}
|
||||
messages = append(messages, openai.ChatCompletionMessage{
|
||||
Role: role,
|
||||
Content: comment.GetBody(),
|
||||
})
|
||||
|
||||
// if last comment is from the user and mentions the bot username, we must answer
|
||||
if comment.User.GetName() != user.GetLogin() && len(comments)-1 == i {
|
||||
if strings.Contains(comment.GetBody(), fmt.Sprintf("@%s", user.GetLogin())) {
|
||||
xlog.Info("Bot was mentioned in the last comment")
|
||||
mustAnswer = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(comments) == 0 || !botAnsweredAlready {
|
||||
// if no comments, or bot didn't answer yet, we must answer
|
||||
xlog.Info("No comments, or bot didn't answer yet",
|
||||
"comments", len(comments),
|
||||
"botAnsweredAlready", botAnsweredAlready,
|
||||
"agent", g.agent.Character.Name,
|
||||
)
|
||||
mustAnswer = true
|
||||
}
|
||||
|
||||
if len(comments) != 0 && g.replyIfNoReplies {
|
||||
xlog.Info("Ignoring issue with comments", "issue", issue.GetNumber(), "agent", g.agent.Character.Name)
|
||||
mustAnswer = false
|
||||
}
|
||||
|
||||
if !mustAnswer {
|
||||
continue
|
||||
}
|
||||
|
||||
res := g.agent.Ask(
|
||||
agent.WithConversationHistory(messages),
|
||||
)
|
||||
if res.Error != nil {
|
||||
xlog.Error("Error asking", "error", res.Error, "agent", g.agent.Character.Name)
|
||||
return
|
||||
}
|
||||
|
||||
_, _, err := g.client.Issues.CreateComment(
|
||||
g.agent.Context(),
|
||||
g.owner, g.repository,
|
||||
issue.GetNumber(), &github.IssueComment{
|
||||
Body: github.String(res.Response),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
xlog.Error("Error creating comment", "error", err, "agent", g.agent.Character.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
196
services/connectors/githubpr.go
Normal file
196
services/connectors/githubpr.go
Normal file
@@ -0,0 +1,196 @@
|
||||
package connectors
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-github/v61/github"
|
||||
"github.com/mudler/local-agent-framework/core/agent"
|
||||
"github.com/mudler/local-agent-framework/pkg/xlog"
|
||||
|
||||
"github.com/sashabaranov/go-openai"
|
||||
)
|
||||
|
||||
type GithubPRs struct {
|
||||
token string
|
||||
repository string
|
||||
owner string
|
||||
replyIfNoReplies bool
|
||||
agent *agent.Agent
|
||||
pollInterval time.Duration
|
||||
client *github.Client
|
||||
}
|
||||
|
||||
// NewGithubIssueWatcher creates a new GithubPRs connector
|
||||
// with the given configuration
|
||||
// - token: Github token
|
||||
// - repository: Github repository name
|
||||
// - owner: Github repository owner
|
||||
// - replyIfNoReplies: If true, the bot will reply to issues with no comments
|
||||
func NewGithubPRWatcher(config map[string]string) *GithubPRs {
|
||||
client := github.NewClient(nil).WithAuthToken(config["token"])
|
||||
replyIfNoReplies := false
|
||||
if config["replyIfNoReplies"] == "true" {
|
||||
replyIfNoReplies = true
|
||||
}
|
||||
|
||||
interval, err := time.ParseDuration(config["pollInterval"])
|
||||
if err != nil {
|
||||
interval = 10 * time.Minute
|
||||
}
|
||||
|
||||
return &GithubPRs{
|
||||
client: client,
|
||||
token: config["token"],
|
||||
repository: config["repository"],
|
||||
owner: config["owner"],
|
||||
replyIfNoReplies: replyIfNoReplies,
|
||||
pollInterval: interval,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GithubPRs) AgentResultCallback() func(state agent.ActionState) {
|
||||
return func(state agent.ActionState) {
|
||||
// Send the result to the bot
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GithubPRs) AgentReasoningCallback() func(state agent.ActionCurrentState) bool {
|
||||
return func(state agent.ActionCurrentState) bool {
|
||||
// Send the reasoning to the bot
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GithubPRs) Start(a *agent.Agent) {
|
||||
// Start the connector
|
||||
g.agent = a
|
||||
|
||||
go func() {
|
||||
ticker := time.NewTicker(g.pollInterval)
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
xlog.Info("Looking into github Prs...")
|
||||
g.prService()
|
||||
case <-a.Context().Done():
|
||||
xlog.Info("GithubPRs connector is now stopping")
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (g *GithubPRs) prService() {
|
||||
user, _, err := g.client.Users.Get(g.agent.Context(), "")
|
||||
if err != nil {
|
||||
fmt.Printf("\nerror: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
issues, _, err := g.client.Issues.ListByRepo(
|
||||
g.agent.Context(),
|
||||
g.owner,
|
||||
g.repository,
|
||||
&github.IssueListByRepoOptions{})
|
||||
if err != nil {
|
||||
xlog.Info("Error listing issues", err)
|
||||
}
|
||||
for _, issue := range issues {
|
||||
// Do something if not an PR
|
||||
if !issue.IsPullRequest() {
|
||||
continue
|
||||
}
|
||||
labels := []string{}
|
||||
for _, l := range issue.Labels {
|
||||
labels = append(labels, l.GetName())
|
||||
}
|
||||
|
||||
// Get user that opened the issue
|
||||
userNameLogin := issue.GetUser().Login
|
||||
userName := ""
|
||||
if userNameLogin != nil {
|
||||
userName = *userNameLogin
|
||||
}
|
||||
|
||||
if userName == user.GetLogin() {
|
||||
xlog.Info("Ignoring issue opened by the bot")
|
||||
continue
|
||||
}
|
||||
messages := []openai.ChatCompletionMessage{
|
||||
{
|
||||
Role: "system",
|
||||
Content: fmt.Sprintf(
|
||||
`This is a conversation with an user ("%s") that opened a Github issue with title "%s" in the repository "%s" owned by "%s". The issue is the issue number %d. Current labels: %+v`, userName, issue.GetTitle(), g.repository, g.owner, issue.GetNumber(), labels),
|
||||
},
|
||||
{
|
||||
Role: "user",
|
||||
Content: issue.GetBody(),
|
||||
},
|
||||
}
|
||||
|
||||
comments, _, _ := g.client.Issues.ListComments(g.agent.Context(), g.owner, g.repository, issue.GetNumber(),
|
||||
&github.IssueListCommentsOptions{})
|
||||
|
||||
mustAnswer := false
|
||||
botAnsweredAlready := false
|
||||
for i, comment := range comments {
|
||||
role := "user"
|
||||
if comment.GetUser().GetLogin() == user.GetLogin() {
|
||||
botAnsweredAlready = true
|
||||
role = "assistant"
|
||||
}
|
||||
messages = append(messages, openai.ChatCompletionMessage{
|
||||
Role: role,
|
||||
Content: comment.GetBody(),
|
||||
})
|
||||
|
||||
// if last comment is from the user and mentions the bot username, we must answer
|
||||
if comment.User.GetName() != user.GetLogin() && len(comments)-1 == i {
|
||||
if strings.Contains(comment.GetBody(), fmt.Sprintf("@%s", user.GetLogin())) {
|
||||
xlog.Info("Bot was mentioned in the last comment")
|
||||
mustAnswer = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(comments) == 0 || !botAnsweredAlready {
|
||||
// if no comments, or bot didn't answer yet, we must answer
|
||||
xlog.Info("No comments, or bot didn't answer yet",
|
||||
"comments", len(comments),
|
||||
"botAnsweredAlready", botAnsweredAlready,
|
||||
"agent", g.agent.Character.Name,
|
||||
)
|
||||
mustAnswer = true
|
||||
}
|
||||
|
||||
if len(comments) != 0 && g.replyIfNoReplies {
|
||||
xlog.Info("Ignoring issue with comments", "issue", issue.GetNumber(), "agent", g.agent.Character.Name)
|
||||
mustAnswer = false
|
||||
}
|
||||
|
||||
if !mustAnswer {
|
||||
continue
|
||||
}
|
||||
|
||||
res := g.agent.Ask(
|
||||
agent.WithConversationHistory(messages),
|
||||
)
|
||||
if res.Error != nil {
|
||||
xlog.Error("Error asking", "error", res.Error, "agent", g.agent.Character.Name)
|
||||
return
|
||||
}
|
||||
|
||||
_, _, err := g.client.Issues.CreateComment(
|
||||
g.agent.Context(),
|
||||
g.owner, g.repository,
|
||||
issue.GetNumber(), &github.IssueComment{
|
||||
Body: github.String(res.Response),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
xlog.Error("Error creating comment", "error", err, "agent", g.agent.Character.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
155
services/connectors/slack.go
Normal file
155
services/connectors/slack.go
Normal file
@@ -0,0 +1,155 @@
|
||||
package connectors
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/mudler/local-agent-framework/pkg/xlog"
|
||||
|
||||
"github.com/mudler/local-agent-framework/core/agent"
|
||||
|
||||
"github.com/slack-go/slack/socketmode"
|
||||
|
||||
"github.com/slack-go/slack"
|
||||
"github.com/slack-go/slack/slackevents"
|
||||
)
|
||||
|
||||
type Slack struct {
|
||||
appToken string
|
||||
botToken string
|
||||
channelID string
|
||||
alwaysReply bool
|
||||
}
|
||||
|
||||
func NewSlack(config map[string]string) *Slack {
|
||||
return &Slack{
|
||||
appToken: config["appToken"],
|
||||
botToken: config["botToken"],
|
||||
channelID: config["channelID"],
|
||||
alwaysReply: config["alwaysReply"] == "true",
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Slack) AgentResultCallback() func(state agent.ActionState) {
|
||||
return func(state agent.ActionState) {
|
||||
// Send the result to the bot
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Slack) AgentReasoningCallback() func(state agent.ActionCurrentState) bool {
|
||||
return func(state agent.ActionCurrentState) bool {
|
||||
// Send the reasoning to the bot
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Slack) Start(a *agent.Agent) {
|
||||
api := slack.New(
|
||||
t.botToken,
|
||||
// slack.OptionDebug(true),
|
||||
slack.OptionLog(log.New(os.Stdout, "api: ", log.Lshortfile|log.LstdFlags)),
|
||||
slack.OptionAppLevelToken(t.appToken),
|
||||
)
|
||||
|
||||
client := socketmode.New(
|
||||
api,
|
||||
//socketmode.OptionDebug(true),
|
||||
//socketmode.OptionLog(log.New(os.Stdout, "socketmode: ", log.Lshortfile|log.LstdFlags)),
|
||||
)
|
||||
go func() {
|
||||
for evt := range client.Events {
|
||||
switch evt.Type {
|
||||
case socketmode.EventTypeConnecting:
|
||||
xlog.Info("Connecting to Slack with Socket Mode...")
|
||||
case socketmode.EventTypeConnectionError:
|
||||
xlog.Info("Connection failed. Retrying later...")
|
||||
case socketmode.EventTypeConnected:
|
||||
xlog.Info("Connected to Slack with Socket Mode.")
|
||||
case socketmode.EventTypeEventsAPI:
|
||||
eventsAPIEvent, ok := evt.Data.(slackevents.EventsAPIEvent)
|
||||
if !ok {
|
||||
fmt.Printf("Ignored %+v\n", evt)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("Event received: %+v\n", eventsAPIEvent)
|
||||
|
||||
client.Ack(*evt.Request)
|
||||
|
||||
switch eventsAPIEvent.Type {
|
||||
case slackevents.CallbackEvent:
|
||||
innerEvent := eventsAPIEvent.InnerEvent
|
||||
|
||||
b, err := api.AuthTest()
|
||||
if err != nil {
|
||||
fmt.Printf("Error getting auth test: %v", err)
|
||||
}
|
||||
|
||||
switch ev := innerEvent.Data.(type) {
|
||||
case *slackevents.MessageEvent:
|
||||
if t.channelID == "" && !t.alwaysReply || // If we have set alwaysReply and no channelID
|
||||
t.channelID != ev.Channel { // If we have a channelID and it's not the same as the event channel
|
||||
// Skip messages from other channels
|
||||
xlog.Info("Skipping reply to channel", ev.Channel, t.channelID)
|
||||
continue
|
||||
}
|
||||
|
||||
if b.UserID == ev.User {
|
||||
// Skip messages from ourselves
|
||||
continue
|
||||
}
|
||||
|
||||
message := ev.Text
|
||||
go func() {
|
||||
res := a.Ask(
|
||||
agent.WithText(message),
|
||||
)
|
||||
|
||||
_, _, err = api.PostMessage(ev.Channel,
|
||||
slack.MsgOptionText(res.Response, false),
|
||||
slack.MsgOptionPostMessageParameters(slack.PostMessageParameters{LinkNames: 1}))
|
||||
if err != nil {
|
||||
fmt.Printf("Error posting message: %v", err)
|
||||
}
|
||||
}()
|
||||
case *slackevents.AppMentionEvent:
|
||||
|
||||
if b.UserID == ev.User {
|
||||
// Skip messages from ourselves
|
||||
continue
|
||||
}
|
||||
message := ev.Text
|
||||
|
||||
// strip our id from the message
|
||||
message = strings.ReplaceAll(message, "<@"+b.UserID+"> ", "")
|
||||
xlog.Info("Message", message)
|
||||
|
||||
go func() {
|
||||
res := a.Ask(
|
||||
agent.WithText(message),
|
||||
)
|
||||
|
||||
_, _, err = api.PostMessage(ev.Channel,
|
||||
slack.MsgOptionText(res.Response, false),
|
||||
slack.MsgOptionPostMessageParameters(slack.PostMessageParameters{LinkNames: 1}))
|
||||
if err != nil {
|
||||
fmt.Printf("Error posting message: %v", err)
|
||||
}
|
||||
}()
|
||||
case *slackevents.MemberJoinedChannelEvent:
|
||||
fmt.Printf("user %q joined to channel %q", ev.User, ev.Channel)
|
||||
}
|
||||
default:
|
||||
client.Debugf("unsupported Events API event received")
|
||||
}
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "Unexpected event type received: %s\n", evt.Type)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
client.RunContext(a.Context())
|
||||
}
|
||||
93
services/connectors/telegram.go
Normal file
93
services/connectors/telegram.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package connectors
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"os/signal"
|
||||
|
||||
"github.com/go-telegram/bot"
|
||||
"github.com/go-telegram/bot/models"
|
||||
"github.com/mudler/local-agent-framework/core/agent"
|
||||
)
|
||||
|
||||
type Telegram struct {
|
||||
Token string
|
||||
lastChatID int64
|
||||
bot *bot.Bot
|
||||
agent *agent.Agent
|
||||
}
|
||||
|
||||
// Send any text message to the bot after the bot has been started
|
||||
|
||||
func (t *Telegram) AgentResultCallback() func(state agent.ActionState) {
|
||||
return func(state agent.ActionState) {
|
||||
t.bot.SetMyDescription(t.agent.Context(), &bot.SetMyDescriptionParams{
|
||||
Description: state.Reasoning,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Telegram) AgentReasoningCallback() func(state agent.ActionCurrentState) bool {
|
||||
return func(state agent.ActionCurrentState) bool {
|
||||
t.bot.SetMyDescription(t.agent.Context(), &bot.SetMyDescriptionParams{
|
||||
Description: state.Reasoning,
|
||||
})
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Telegram) Start(a *agent.Agent) {
|
||||
ctx, cancel := signal.NotifyContext(a.Context(), os.Interrupt)
|
||||
defer cancel()
|
||||
|
||||
opts := []bot.Option{
|
||||
bot.WithDefaultHandler(func(ctx context.Context, b *bot.Bot, update *models.Update) {
|
||||
go func() {
|
||||
res := a.Ask(
|
||||
agent.WithText(
|
||||
update.Message.Text,
|
||||
),
|
||||
)
|
||||
b.SendMessage(ctx, &bot.SendMessageParams{
|
||||
ChatID: update.Message.Chat.ID,
|
||||
Text: res.Response,
|
||||
})
|
||||
t.lastChatID = update.Message.Chat.ID
|
||||
}()
|
||||
}),
|
||||
}
|
||||
|
||||
b, err := bot.New(t.Token, opts...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
t.bot = b
|
||||
t.agent = a
|
||||
|
||||
go func() {
|
||||
for m := range a.ConversationChannel() {
|
||||
if t.lastChatID == 0 {
|
||||
continue
|
||||
}
|
||||
b.SendMessage(ctx, &bot.SendMessageParams{
|
||||
ChatID: t.lastChatID,
|
||||
Text: m.Content,
|
||||
})
|
||||
}
|
||||
}()
|
||||
|
||||
b.Start(ctx)
|
||||
}
|
||||
|
||||
func NewTelegramConnector(config map[string]string) (*Telegram, error) {
|
||||
token, ok := config["token"]
|
||||
if !ok {
|
||||
return nil, errors.New("token is required")
|
||||
}
|
||||
|
||||
return &Telegram{
|
||||
Token: token,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user