From ed98f059f1714ecbe7b0fa7b2704e26f9c5cb0d5 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Tue, 25 Apr 2023 14:05:30 -0700 Subject: [PATCH] Update Echobot Tutorial --- blog/2023-04-28-developer-docs.md | 2 +- .../building-an-echobot.md | 42 ++++++++----------- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/blog/2023-04-28-developer-docs.md b/blog/2023-04-28-developer-docs.md index fd8f8f9a..5037b505 100644 --- a/blog/2023-04-28-developer-docs.md +++ b/blog/2023-04-28-developer-docs.md @@ -2,7 +2,7 @@ title: Cwtch Developer Documentation description: "In this development log we take a look at the new Cwtch developer docs!" slug: cwtch-developer-documentation -tags: [cwtch, cwtch-stable, developer-documentation, security-handbook] +tags: [cwtch, cwtch-stable, developer-documentation] image: /img/devlog9_small.png hide_table_of_contents: false toc_max_heading_level: 4 diff --git a/developing/building-a-cwtch-app/building-an-echobot.md b/developing/building-a-cwtch-app/building-an-echobot.md index 4284c0a1..c16b5fbd 100644 --- a/developing/building-a-cwtch-app/building-an-echobot.md +++ b/developing/building-a-cwtch-app/building-an-echobot.md @@ -24,7 +24,9 @@ package main import ( "cwtch.im/cwtch/event" "cwtch.im/cwtch/model" - "cwtch.im/cwtch/protocol/connections" + "cwtch.im/cwtch/model/attr" + "cwtch.im/cwtch/model/constants" + "fmt" "git.openprivacy.ca/sarah/cwtchbot" _ "github.com/mutecomm/go-sqlcipher/v4" "os/user" @@ -32,41 +34,33 @@ import ( ) func main() { + user, _ := user.Current() + cwtchbot := bot.NewCwtchBot(path.Join(user.HomeDir, "/.echobot/"), "echobot") + cwtchbot.Launch() - // Set up a new Cwtch Profile in $HOME/.echobot - user, _ := user.Current() - cwtchbot := bot.NewCwtchBot(path.Join(user.HomeDir, "/.echobot/"), "echobot") - - // Launch the Cwtch Bot - cwtchbot.Launch() + // Set Some Profile Information + cwtchbot.Peer.SetScopedZonedAttribute(attr.PublicScope, attr.ProfileZone, constants.Name, "echobot2") + cwtchbot.Peer.SetScopedZonedAttribute(attr.PublicScope, attr.ProfileZone, constants.ProfileAttribute1, "A Cwtchbot Echobot") - // Echobot Cwtch Address will be printed to stdout - fmt.Printf("echobot address: %v", cwtchbot.Peer.GetOnion()) + fmt.Printf("echobot address: %v\n", cwtchbot.Peer.GetOnion()) - // Start of the eventbus loop for { message := cwtchbot.Queue.Next() cid, _ := cwtchbot.Peer.FetchConversationInfo(message.Data[event.RemotePeer]) switch message.EventType { case event.NewMessageFromPeer: - // Send an Acknowledgement of the Message cwtchbot.Queue.Publish(event.NewEvent(event.PeerAcknowledgement, map[event.Field]string{event.EventID: message.EventID, event.RemotePeer: message.Data[event.RemotePeer]})) - // Unpack the message msg := cwtchbot.UnpackMessage(message.Data[event.Data]) - // Print the message to stdout... - fmt.Printf("Message: %v", msg) - // Repackage the message, and reply... + fmt.Printf("Message: %v\n", msg) reply := string(cwtchbot.PackMessage(msg.Overlay, msg.Data)) cwtchbot.Peer.SendMessage(cid.ID, reply) - case event.PeerStateChange: - state := message.Data[event.ConnectionState] - // If we have a new peer that is authenticated.... - if state == connections.ConnectionStateName[connections.AUTHENTICATED] { - // accept the stranger as a new contact - cwtchbot.Peer.NewContactConversation("stranger", model.DefaultP2PAccessControl(), true) - } - default: - // Other events will be handled here... + case event.ContactCreated: + fmt.Printf("Auto approving stranger %v %v\n", cid, message.Data[event.RemotePeer]) + // accept the stranger as a new contact + cwtchbot.Peer.AcceptConversation(cid.ID) + // Send Hello... + reply := string(cwtchbot.PackMessage(model.OverlayChat, "Hello!")) + cwtchbot.Peer.SendMessage(cid.ID, reply) } } }