Update Echobot Tutorial

This commit is contained in:
Sarah Jamie Lewis 2023-04-25 14:05:30 -07:00
parent 6f7a98c688
commit ed98f059f1
2 changed files with 19 additions and 25 deletions

View File

@ -2,7 +2,7 @@
title: Cwtch Developer Documentation title: Cwtch Developer Documentation
description: "In this development log we take a look at the new Cwtch developer docs!" description: "In this development log we take a look at the new Cwtch developer docs!"
slug: cwtch-developer-documentation slug: cwtch-developer-documentation
tags: [cwtch, cwtch-stable, developer-documentation, security-handbook] tags: [cwtch, cwtch-stable, developer-documentation]
image: /img/devlog9_small.png image: /img/devlog9_small.png
hide_table_of_contents: false hide_table_of_contents: false
toc_max_heading_level: 4 toc_max_heading_level: 4

View File

@ -24,7 +24,9 @@ package main
import ( import (
"cwtch.im/cwtch/event" "cwtch.im/cwtch/event"
"cwtch.im/cwtch/model" "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" "git.openprivacy.ca/sarah/cwtchbot"
_ "github.com/mutecomm/go-sqlcipher/v4" _ "github.com/mutecomm/go-sqlcipher/v4"
"os/user" "os/user"
@ -32,41 +34,33 @@ import (
) )
func main() { 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 // Set Some Profile Information
user, _ := user.Current() cwtchbot.Peer.SetScopedZonedAttribute(attr.PublicScope, attr.ProfileZone, constants.Name, "echobot2")
cwtchbot := bot.NewCwtchBot(path.Join(user.HomeDir, "/.echobot/"), "echobot") cwtchbot.Peer.SetScopedZonedAttribute(attr.PublicScope, attr.ProfileZone, constants.ProfileAttribute1, "A Cwtchbot Echobot")
// Launch the Cwtch Bot
cwtchbot.Launch()
// Echobot Cwtch Address will be printed to stdout fmt.Printf("echobot address: %v\n", cwtchbot.Peer.GetOnion())
fmt.Printf("echobot address: %v", cwtchbot.Peer.GetOnion())
// Start of the eventbus loop
for { for {
message := cwtchbot.Queue.Next() message := cwtchbot.Queue.Next()
cid, _ := cwtchbot.Peer.FetchConversationInfo(message.Data[event.RemotePeer]) cid, _ := cwtchbot.Peer.FetchConversationInfo(message.Data[event.RemotePeer])
switch message.EventType { switch message.EventType {
case event.NewMessageFromPeer: 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]})) 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]) msg := cwtchbot.UnpackMessage(message.Data[event.Data])
// Print the message to stdout... fmt.Printf("Message: %v\n", msg)
fmt.Printf("Message: %v", msg)
// Repackage the message, and reply...
reply := string(cwtchbot.PackMessage(msg.Overlay, msg.Data)) reply := string(cwtchbot.PackMessage(msg.Overlay, msg.Data))
cwtchbot.Peer.SendMessage(cid.ID, reply) cwtchbot.Peer.SendMessage(cid.ID, reply)
case event.PeerStateChange: case event.ContactCreated:
state := message.Data[event.ConnectionState] fmt.Printf("Auto approving stranger %v %v\n", cid, message.Data[event.RemotePeer])
// If we have a new peer that is authenticated.... // accept the stranger as a new contact
if state == connections.ConnectionStateName[connections.AUTHENTICATED] { cwtchbot.Peer.AcceptConversation(cid.ID)
// accept the stranger as a new contact // Send Hello...
cwtchbot.Peer.NewContactConversation("stranger", model.DefaultP2PAccessControl(), true) reply := string(cwtchbot.PackMessage(model.OverlayChat, "Hello!"))
} cwtchbot.Peer.SendMessage(cid.ID, reply)
default:
// Other events will be handled here...
} }
} }
} }