This repository has been archived by the owner on Nov 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
EchoBot.py
141 lines (110 loc) · 3.78 KB
/
EchoBot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#! /usr/bin/python3
# EchoBot Demontration Code
# Based on various Reticulum examples
# -- PLEASE NOTE --
# EchoBot is a noisy and hacky solution to get new users
# talking on a network quickly.
# Please don't leave a copy running on the public testnet.
# One should already be running and I'm already embarrased
# at the announce spam.
import RNS
import os
import time
import LXMF
namestring = "EchoBot"
# Name in bytes for transmission purposes
namebytes = bytes(namestring,"utf-8")
# Initialize Reticulum
reticulum = RNS.Reticulum()
# Time between announces
delaytime = 3600 #seconds
# Message Response Handler
def Respond(destination, message):
global lxm_router
# Convert string to bytes below if you pass as a string
destination_bytes = destination
# Check to see if RNS knows the identity
destination_identity = RNS.Identity.recall(destination_bytes)
# If it doesn't know the identity:
if destination_identity == None:
basetime = time.time()
# Request it
RNS.Transport.request_path(destination_bytes)
# And wait until it arrives; timeout in 30s
while destination_identity == None and (time.time() - basetime) < 30:
destination_identity = RNS.Identity.recall(destination_bytes)
time.sleep(1)
if destination_identity == None:
print("Error: Cannot recall identity")
# When we have the path:
else:
message = "Received your message: "+message
# Create the destination
lxmf_destination = RNS.Destination(
destination_identity,
RNS.Destination.OUT,
RNS.Destination.SINGLE,
"lxmf",
"delivery"
)
# NOTE: "lxmf" is the app name and "delivery" is the aspect.
# This is what allows it to communicate with NomadNet and Sideband.
# Otherwise these systems will filter out/not handle this message
# Create the lxm object
lxm = LXMF.LXMessage(
lxmf_destination,
local_lxmf_destination,
message,
title="ACK",
desired_method=LXMF.LXMessage.DIRECT
)
# Send the message through the router
lxm_router.handle_outbound(lxm)
def LXMDelivery(lxm):
# The lxmessage object contains the source hash (Base Reticulum packets do not)
# and a content which is decoded from bytes to string
Respond(lxm.source_hash,lxm.content.decode('utf-8'))
### End routines; Initilization code
# Set user directory
userdir = os.path.expanduser("~")
# Set config directory
configdir = userdir+"/.EchoBot"
# Error handling - Make missing config directory
if not os.path.isdir(configdir):
os.makedirs(configdir)
# Set identity path (filename)
identitypath = configdir+"/identity"
# If the file exists, load the identity
if os.path.exists(identitypath):
ID = RNS.Identity.from_file(identitypath)
print("Loading identity")
# If not, create and save a new identity
else:
ID = RNS.Identity()
ID.to_file(identitypath)
print("Saving new identity")
# Create and configure LXM router
lxm_router = LXMF.LXMRouter(identity = ID, storagepath = configdir)
# Register callback
lxm_router.register_delivery_callback(LXMDelivery)
# The delivery identity takes the display name as a string
local_lxmf_destination = lxm_router.register_delivery_identity(ID,display_name=namestring)
# Print hash to terminal
print(RNS.prettyhexrep(ID.hash))
# Main Loop Definition
def MainLoop():
global namebytes, delaytime
oldtime = 0
while True:
#Every delaytime seconds (and on startup)
newtime = time.time()
if newtime > (oldtime + delaytime):
oldtime = newtime
# Announce the destination
local_lxmf_destination.announce()
# And print status on the terminal
print("Tick")
# Remember to sleep your loops to save on processor time
time.sleep(1)
# Execute progam
MainLoop()