Update Echobot Tutorial
This commit is contained in:
parent
6f7a98c688
commit
ed98f059f1
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue