Skip to content

Commit

Permalink
add matix chat publisher, freeze pip requirements
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias <[email protected]>
  • Loading branch information
call-me-matt committed Jan 26, 2025
1 parent e5408f7 commit 8d78a0e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 19 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ mail_pw: "somegmailssecretpasswort"
forum_KEY: "abcdef123456"
mastondon_INSTANCE: "yourinstance.social"
mastodon_TOKEN: "yourMastodonDeveloperApplicationAccessToken"
matrix_USER: "@youruser:matrix.org"
matrix_BASE: "https://matrix.org"
matrix_TOKEN: "syt_tokentoken_tokentoken_token"
tw_CONSUMER_KEY: "yourTwitterConsumerKey"
tw_CONSUMER_SECRET: "yourTwitterConsumerSecretFrdsfor09512kljda98324iu21as"
tw_ACCESS_KEY: "123456-YourTWAccessKeyNelsonMandela4Evaaaa"
Expand Down Expand Up @@ -80,5 +83,5 @@ test call - for twitter

how to call - mail, forum, josm, telegram, mastodon and twitter
```
./runenvweekly2all.sh --mail --forum --josm --telegram --mastodon --bluesky --pic auto --showpic "WEEKLY" "int,de,en,es,pt,tr,ru,ja,fr,it,ko,br,zh,pl,uk"
./runenvweekly2all.sh --bluesky --forum --josm --mail --mastodon --matrix --telegram --pic auto --showpic "WEEKLY" "int,de,en,es,pt,tr,ru,ja,fr,it,ko,br,zh,pl,uk"
```
2 changes: 1 addition & 1 deletion configs/private
20 changes: 11 additions & 9 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
requests
tweepy
Pillow
mechanicalsoup
pyyaml
Mastodon.py
telepot
feedparser
atproto
asyncio==3.4.3
atproto==0.0.56
feedparser==6.0.11
Mastodon.py==1.8.1
mautrix==0.20.7
MechanicalSoup==1.3.0
pillow==10.4.0
PyYAML==6.0.2
requests==2.32.3
telepot==12.7
tweepy==4.14.0
51 changes: 43 additions & 8 deletions weekly2all.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import feedparser
import requests
import telepot
from mautrix.client import ClientAPI
import asyncio
import tweepy
import yaml
from mastodon import Mastodon
Expand Down Expand Up @@ -102,6 +104,7 @@ def __init__(self):
self.do_mastodon = False
self.do_pin_mastodon = False
self.do_unpin_mastodon = False
self.do_matrix = False
self.do_twitter = False
self.do_bluesky = False
self.do_mail = False
Expand All @@ -111,6 +114,9 @@ def __init__(self):
self.url = "" # url that we are sending or tweeting
self.mastodon_INSTANCE = ""
self.mastodon_TOKEN = ""
self.matrix_USER = ""
self.matrix_BASE = ""
self.matrix_TOKEN = ""
self.bluesky_USER = ""
self.bluesky_TOKEN = ""
self.bluesky_text = ""
Expand All @@ -134,6 +140,7 @@ def __init__(self):
self.telegram_TOKEN = ""
self.telegram_to = []
self.mastodon_to = []
self.matrix_to = []
self.josm_user = ""
self.josm_pw = ""
self.josm_body = ""
Expand All @@ -145,6 +152,7 @@ def load_params(self, args):
self.do_show_pic = vars(args)["showpic"]
self.do_mail = vars(args)["mail"]
self.do_mastodon = vars(args)["mastodon"]
self.do_matrix = vars(args)["matrix"]
self.do_bluesky = vars(args)["bluesky"]
self.do_twitter = vars(args)["twitter"]
self.do_forum = vars(args)["forum"]
Expand Down Expand Up @@ -212,13 +220,17 @@ def load_from_config(self, conf):
"do_telegram",
"do_josm",
"do_mastodon",
"do_matrix",
"do_bluesky",
"do_twitter",
"do_mail",
"do_show_pic",
"url",
"mastodon_INSTANCE",
"mastodon_TOKEN",
"matrix_USER",
"matrix_BASE",
"matrix_TOKEN",
"bluesky_USER",
"bluesky_TOKEN",
"bluesky_text",
Expand Down Expand Up @@ -248,6 +260,7 @@ def load_from_config(self, conf):
"forum_to",
"telegram_to",
"mastodon_to",
"matrix_to",
]:
self.assign_safe(field, conf)

Expand Down Expand Up @@ -505,6 +518,23 @@ def telegram(self, bot, recipient):
# bot.unpinChatMessage(recipient) # unpins most recent chat message
# bot.pinChatMessage(recipient,resp['message_id'],True)

async def post_matrix(self, recipients):
logger.info("...posting to matrix...")
try:
client = ClientAPI(
self.matrix_USER, base_url=self.matrix_BASE, token=self.matrix_TOKEN
)
except Exception as e:
logger.error(f"could not connect to matrix chat - {e}")
return
for recipient in recipients:
logger.info(recipient)
try:
await client.send_text(recipient, self.tw_text)
except Exception as e:
logger.error(f"could not send matrix chat message- {e}")
await client.api.session.close()

def post_josm(self):
logger.info(f"...posting to josm...")
with xmlrpc.client.ServerProxy(
Expand Down Expand Up @@ -584,6 +614,8 @@ def send_stuff(self):
bot = telepot.Bot(self.telegram_TOKEN)
for to in self.telegram_to:
self.telegram(bot, to)
if self.do_matrix:
asyncio.run(self.post_matrix(self.matrix_to))
if self.do_mail:
for to in self.mail_to:
self.send_email(to)
Expand All @@ -608,19 +640,12 @@ def send_stuff(self):
argparser = argparse.ArgumentParser(
prog="weekly2all",
description="Python 3 script to notify about a new issue of Wochennotiz/weeklyOSM.",
epilog='example: python weekly2all.py --mail --forum --twitter --mastodon --pic ~/downloads/1.jpg "WEEKLY" "en,de"',
epilog='example: python weekly2all.py --mail --forum --mastodon --pic ~/downloads/1.jpg "WEEKLY" "en,de"',
)

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(
"--bluesky", action="store_true", help="send bluesky.social notification"
)
argparser.add_argument("--mail", action="store_true", help="send mail")
argparser.add_argument(
"--forum", action="store_true", help="send post to forum threads"
)
Expand All @@ -629,11 +654,21 @@ def send_stuff(self):
action="store_true",
help="send announcements to telegram channels and groups where the bot is admin",
)
argparser.add_argument(
"--twitter", action="store_true", help="send twitter notification"
)
argparser.add_argument(
"--josm",
action="store_true",
help="send announcements to josm wiki (shown at josm program start)",
)
argparser.add_argument("--mail", action="store_true", help="send e-mail notification")
argparser.add_argument(
"--mastodon", action="store_true", help="send mastodon notification"
)
argparser.add_argument(
"--matrix", action="store_true", help="send matrix chat notification"
)
argparser.add_argument(
"--pic",
help="picture for mastodon, bluesky and twitter, use auto to retrieve from latest weekly",
Expand Down

0 comments on commit 8d78a0e

Please sign in to comment.