Compare commits

..

No commits in common. "master" and "master" have entirely different histories.

2 changed files with 49 additions and 73 deletions

View File

@ -16,7 +16,7 @@ Installation
Clone the repo: Clone the repo:
git clone https://git.bitmessage.org/Bitmessage/PyBitMessage_AutoResponder && cd PyBitMessage_AutoResponder git clone https://git.bitmessage.org/prachiyadav/PyBitMessage_AutoResponder && cd PyBitMessage_AutoResponder
And.. you're done. And.. you're done.
@ -26,7 +26,7 @@ Before running this script please run the bitmessages application, because if yo
Configuration Steps Configuration Steps
1.Put the autoresponder.conf file into the .config directory which contains the database of bitmessage application. 1.Put the credentials.ini file into the .config directory which contains the database of bitmessage application.
2.To establish a connection, copy and paste these lines into the bitmessagesettings section of the keys.dat file.Note that the values "username" and "password" below are merely examples, and should be replaced by values that cannot feasibly be guessed: 2.To establish a connection, copy and paste these lines into the bitmessagesettings section of the keys.dat file.Note that the values "username" and "password" below are merely examples, and should be replaced by values that cannot feasibly be guessed:
--> apienabled = true --> apienabled = true
@ -36,4 +36,9 @@ Configuration Steps
--> apipassword = abc@123 --> apipassword = abc@123
3.For running the application use 3.For running the application use
--> python3 autoresponder_mailing.py --> Python autoresponder_mailing.py

View File

@ -1,79 +1,61 @@
#!/usr/bin/python3
"""Bitmessage AutoResponder""" """Bitmessage AutoResponder"""
import base64
import configparser import configparser
import json import json
import os import os
import time import time
import xmlrpc.client import xmlrpclib
class BitmessageAutoresponder():
class BitmessageAutoresponder(): # pylint: disable=too-many-locals
"""Sending and receiving messages through autoresponder""" """Sending and receiving messages through autoresponder"""
config = configparser.ConfigParser() config = configparser.ConfigParser()
db_var = os.path.join(os.environ["HOME"], db_var = os.path.join(os.environ["HOME"], '.config/PyBitmessage/credentials.ini')
'.config/PyBitmessage/autoresponder.conf')
config.read(db_var) config.read(db_var)
apiport = config['sqlite3']['apiport'] apiport = config['sqlite3']['apiport']
apiinterface = config['sqlite3']['apiinterface'] apiinterface = config['sqlite3']['apiinterface']
apiusername = config['sqlite3']['apiusername'] apiusername = config['sqlite3']['apiusername']
apipassword = config['sqlite3']['apipassword'] apipassword = config['sqlite3']['apipassword']
api = xmlrpc.client.ServerProxy( api = xmlrpclib.ServerProxy("http://%s:%s@%s:%s/"%(apiusername, apipassword, apiinterface, apiport))
"http://%s:%s@%s:%s/"
% (apiusername, apipassword, apiinterface, apiport))
expire = 300 expire = 300
label = "Autoresponder" myaddress_path = os.path.join(os.environ["HOME"],'.config/PyBitmessage/keys.dat')
def __init__(self):
self.addresses = {}
self.track = {}
def init_address(self):
"""generate random address if there isn't one"""
try:
addresses = json.loads(self.api.listAddresses())['addresses']
if addresses:
return
except KeyError:
pass
self.api.createRandomAddress(base64.b64encode(
self.label.encode()).decode('ascii'))
def loop(self):
"""Main loop"""
for i in json.loads(self.api.listAddresses())['addresses']:
self.addresses[i['address']] = True
def send_autorepond_inbox_message(self):
"""Sending Auto Message To The Recipient"""
track = {}
open_file = open(self.myaddress_path)
get_file = open_file.read()
split_file = get_file.split("\n")
for lines in split_file:
if len(lines) == 39:
if "[" in lines:
set_address=lines.strip("[]")
while True: while True:
self.process_inbox_messages() inboxmessages = json.loads(self.api.getAllInboxMessages())
self.cleanup_tracking() inbox_var = inboxmessages.get('inboxMessages')
self.delete_outbox_messages() currenttime = time.time()
for values in inbox_var:
msgid = values.get('msgid')
toaddress = values.get('fromAddress')
fromaddress = values.get('toAddress')
subject = values.get('subject')
message = values.get('message')
try:
if time.time() < track[toaddress]:
continue
except KeyError:
pass
if set_address == toaddress:
continue
self.api.sendMessage(toaddress, fromaddress, subject, message)
self.api.trashMessage(msgid)
track[toaddress] = currenttime + self.expire
list_of_time_track = list(key for (key, value) in track.items() if value < currenttime)
for address in list_of_time_track:
track.pop(address)
self.delete_outbox_message()
time.sleep(10) time.sleep(10)
def process_inbox_messages(self): def delete_outbox_message(self):
"""Loop inbox and process messages""" """Deleting Messages from Outbox Having Status ackreceived or msgsentnoackexpected"""
inboxmessages = json.loads(self.api.getAllInboxMessages())
inbox_var = inboxmessages.get('inboxMessages')
for values in inbox_var:
msgid = values.get('msgid')
toaddress = values.get('fromAddress')
fromaddress = values.get('toAddress')
subject = values.get('subject')
message = values.get('message')
try:
if time.time() < self.track[toaddress]:
continue
except KeyError:
pass
if toaddress in self.addresses:
continue
self.api.sendMessage(toaddress, fromaddress, subject, message)
self.api.trashMessage(msgid)
self.track[toaddress] = time.time() + self.expire
def delete_outbox_messages(self):
"""Deleting Messages from outbox having status
ackreceived or msgsentnoackexpected"""
sentmessage = json.loads(self.api.getAllSentMessages()) sentmessage = json.loads(self.api.getAllSentMessages())
sent_var = sentmessage.get('sentMessages') sent_var = sentmessage.get('sentMessages')
for values in sent_var: for values in sent_var:
@ -82,16 +64,5 @@ class BitmessageAutoresponder(): # pylint: disable=too-many-locals
if status_ack_rcvd in ('ackreceived', 'msgsentnoackexpected'): if status_ack_rcvd in ('ackreceived', 'msgsentnoackexpected'):
self.api.trashSentMessageByAckData(ackdata) self.api.trashSentMessageByAckData(ackdata)
def cleanup_tracking(self):
"""Expire old tracking data"""
currenttime = time.time()
list_of_time_track = list(
key for (key, value)
in self.track.items() if value < currenttime)
for address in list_of_time_track:
self.track.pop(address)
if __name__ == '__main__': if __name__ == '__main__':
BitmessageAutoresponder().init_address() BitmessageAutoresponder().send_autorepond_inbox_message()
BitmessageAutoresponder().loop()