Skip to content

Commit

Permalink
post to community forum via api instead of mail
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias <matthias@peanut>
  • Loading branch information
Matthias committed Feb 18, 2023
1 parent 64cafbe commit 177b1ff
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 42 deletions.
2 changes: 0 additions & 2 deletions configs/mailto/weekly_int.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion setup.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
python3 -m venv spamenv
source spamenv/bin/activate
pip install -r requirements.txt
pip install --upgrade -r requirements.txt
deactivate
77 changes: 38 additions & 39 deletions weekly2all.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ def __init__(self):
""" values from config """
self.runnable = False # set this variable if the config can actually be called
self.url = '' # url that we are sending or tweeting
self.forum_usr = ''
self.forum_pw = ''
self.forum_login_url = ''
self.forum_urls = []
self.mastodon_INSTANCE = ''
self.mastodon_TOKEN = ''
self.tw_CONSUMER_KEY = ''
Expand All @@ -121,7 +117,7 @@ def __init__(self):
self.mail_from = ''
self.mail_to = []
self.mail_body = ''
self.forum_from = ''
self.forum_KEY = ''
self.forum_to = []
self.telegram_TOKEN = ''
self.telegram_to = []
Expand Down Expand Up @@ -156,10 +152,6 @@ def load_from_config(self,conf):
'do_mail',
'do_show_pic',
'url',
'forum_usr',
'forum_pw',
'forum_login_url',
'forum_urls',
'mastodon_INSTANCE',
'mastodon_TOKEN',
'telegram_TOKEN',
Expand All @@ -178,7 +170,7 @@ def load_from_config(self,conf):
'mail_from',
'mail_to',
'mail_body',
'forum_from',
'forum_KEY',
'forum_to',
'telegram_to']:
self.assign_safe(field,conf)
Expand All @@ -193,7 +185,6 @@ def create_texts(self):
self.mail_subject = self.mail_subject.format(c=self)
self.tw_text = self.tw_text.format(c=self)
self.mail_from = self.mail_from.format(c=self)
self.forum_from = self.forum_from.format(c=self)

def check_url_exists(self):
r = requests.get(self.url)
Expand Down Expand Up @@ -225,31 +216,39 @@ def send_email(self, recipient): # dunno why the loop at this call and the list

return

def post_forum(self, recipient):
logger.info('...community forum post...')

TO = recipient if type(recipient) is list else [recipient]

# Prepare actual message
msg = MIMEMultipart()

msg['From'] = self.forum_from
msg['To'] = ", ".join(TO)
msg['Subject'] = self.mail_subject
msg.attach(MIMEText(self.mail_body, 'plain'))
def post_forum(self, topicId):
logger.info(f'...community forum post to https://community.openstreetmap.org/t/-/{topicId}.')

try:
server = smtplib.SMTP(self.mail_smtp_host, self.mail_smtp_port)
server.ehlo()
server.starttls()
server.login(self.mail_user, self.mail_pw)
server.sendmail(self.forum_from, TO, msg.as_string())
server.close()
logger.info('published to '+", ".join(TO))
FORUM_API = "https://community.openstreetmap.org/posts.json"

data = {
"title": self.mail_subject,
"raw": self.mail_body,
"topic_id": topicId,
"category": 0,
"target_recipients": "",
"target_usernames": "",
"archetype": "",
"created_at": "",
"embed_url": "",
"external_id": ""
}

headers = {
"Content-Type": "application/json",
"User-Api-Key": self.forum_KEY,
"Accept": "application/json"
}

response = requests.post(FORUM_API, json=data, headers=headers)

logger.info(f"Status Code {response.status_code}")
logger.info(f"JSON Response: {response.json()}")

except:
logger.error("failed to publish")
traceback.print_exc()
pprint.pprint((self.mail_user, self.mail_pw,self.forum_from))

def tweet(self):
logger.info('...tweet...')
Expand Down Expand Up @@ -285,6 +284,7 @@ def toot(self):
access_token = self.mastodon_TOKEN,
api_base_url = self.mastodon_INSTANCE
)
media = None

if self.pic:
if os.path.isfile(self.pic):
Expand All @@ -297,14 +297,13 @@ def toot(self):
self.do_show_pic = False
logger.debug('sending toot with image')
pic = mastodon.media_post(self.pic)
mastodon.status_post(self.tw_text, language=self.lang, media_ids=[pic.id])
media = [pic.id]
else:
logger.error('image not found!')
exit(1)
else:
mastodon.toot(self.tw_text)

logger.debug(self.tw_text)
toot = mastodon.status_post(self.tw_text, language=self.lang, media_ids=media)
logger.debug(f"{toot}")

def telegram(self, bot, recipient):
logger.info(f'...telegramming {recipient}...')
Expand Down Expand Up @@ -337,9 +336,9 @@ def send_stuff(self):
if self.do_mail:
for to in self.mail_to:
self.send_email(to)
if self.do_forum:
for to in self.forum_to:
self.post_forum(to)
if self.do_forum and self.forum_to:
# only one message per language, as multiple posts with same text are rejected from forum
self.post_forum(self.forum_to)

return

Expand All @@ -357,7 +356,7 @@ def send_stuff(self):
argparser.add_argument('--twitter', action='store_true', help='send twitter notification')
argparser.add_argument('--mastodon', action='store_true', help='send mastodon notification')
argparser.add_argument('--mail', action='store_true', help='send mail')
argparser.add_argument('--forum', action='store_true', help='send mail to forum threads')
argparser.add_argument('--forum', action='store_true', help='send post to forum threads')
argparser.add_argument('--telegram', action='store_true', help='send announcements to telegram channels and groups where the bot is admin')
argparser.add_argument('--pic', help='picture for mastodon and twitter')
argparser.add_argument('--showpic', action='store_true', help='show picture before sending tweet/toot')
Expand Down

0 comments on commit 177b1ff

Please sign in to comment.