autogestion / pubgate-telegram

Extension for PubGate, Telegram <-> ActivityPub bridge
BSD 3-Clause "New" or "Revised" License
18 stars 2 forks source link

Allow posting to a Telegram channel from ActivityPub feeds #1

Closed mrvdb closed 5 years ago

mrvdb commented 5 years ago

Rather than post from Telegram to an ActivityPub server, I have got a reverse usecase where I would like to post to Telegram channels, typically just to my own, from ActivityPub sources, to provide access to my feeds.

The base usecase is where I want telegram friends to be able to follow my feeds, say pixelfed posts, without them having to create an account there. A first extension could be a number of feeds from me in the same channel, like an aggregator.

Another advantage I see is that the source feeds from me regularly change, which I could make transparent for the people not on those platforms. That is, if you are following me through a (Telegram) channel that aggregates my feeds, I can change those feeds without the followers needing to do anything.

So far i've tried to solve this with atom to telegram bots or rss feeds through ifttt.com but they all are rather primitive and don't give the user much control over how an when information comes into the channel.

autogestion commented 5 years ago

Let's define entities. They could be:

I see several workarounds how updates from sources could be delivered to telegram

ActivityPub side

  1. Subscribe ap_bot (by standard activitpub Follow flow) directly to sources. Sources will send to ap_bot notifications on updates, ap_bot will trigger tg_bot, and tg_bot will publish it to tg_channel
  2. Set ap_bot to track whom ap_user is following (check its following collection), if new source detected, subscribe to it, and process updates
  3. Subscribe (make follow) ap_bot to ap_user. ap_user could check updates from sources, and manually boost posts he want to share with telegram users. ap_bot will receive notifications from ap_user and forward them to tg_bot

Telegram side

  1. Bot could provide updates on request in its channel
  2. Bot could publish updates at once in his channel
  3. Same as 1 or 2, but in public channel or group

So, which one of this is closest to your usecase

mrvdb commented 5 years ago

My usecase with the defined entities:

autogestion commented 5 years ago

Got it. I think I can implement it this weekend. Could you also tell more about filtering

mrvdb commented 5 years ago

Re:filtering, things which I would like to be able to control are for example:

It would make sense to me to arrange for that in the ap_bot side of things, but perhaps it's easier to do this in tg_bot

mrvdb commented 5 years ago

Thanks, good stuff! As soon as I am back from traveling I will test and provide feedback

autogestion commented 5 years ago

it's not finished yet, I marked PR as wip

autogestion commented 5 years ago

@mrvdb so, I've made it)) for now bot broadcasts in both directions text and photos demo chain:

mrvdb commented 5 years ago

Thanks, I'm setting it up now

mrvdb commented 5 years ago

I think I'm missing something,

Using postman as client, I created a user 'ap_bot', with the given details set to channel 'mrb-aptest'

So, after this I'm unsure what to do. I'm assuming I want to let 'ap_bot' follow my accounts (say 'https://todon.nl/@mrb' ) through a call to <host>/ap_bot/outbox (follow) after which it replicates the output of https://todon.nl/@mrb to the channel configured. (the bot is joined in the channel as admin)

autogestion commented 5 years ago

@mrvdb yes, that's correct. Using mentioned call you can follow your accounts (you can check if it is successful by call <host>/ap_bot/following , 'https://todon.nl/@mrb' should be present in that list). Take into account that pubgate will not fetch existing posts (same as mastodon). New toots should go through the bridge to tg

mrvdb commented 5 years ago

The 'follow' doesn't seem to work. I get a 401 response and the following collection is not updated. I get this on both my accounts on todon.nl and pixelfed.social. What am I missing?

Redacted log:

POST http://activitypubbot.qua.name/ap_bot/outbox  201 18
<CIMultiDict('content-type': 'text/plain', 'user-agent': 'PostmanRuntime/7.11.0', 'accept': '*/*', 'cache-control': 'no-cache', 'postman-token': '3a2d0dfc-9960-4ed1-87bc-b20a889d2adb', 'host': 'activitypubbot.qua.name', 'accept-encoding': 'gzip, deflate', 'content-length': '64', 'connection': 'keep-alive', 'x-ssl-cipher': 'TLSv1.2/ECDHE-RSA-AES256-GCM-SHA384 TLSv1.2 Kx=ECDH     Au=RSA  Enc=AESGCM(256) Mac=AEAD', 'x-forwarded-for': '213.125.12.138')>
{'actor': 'http://activitypubbot.qua.name/ap_bot',
 'id': 'http://activitypubbot.qua.name/ap_bot/activity/b9746051e8388dc9',
 'object': 'https://todon.nl/@mrb',
 'type': 'Follow'}
[2019-04-30 19:23:36 +0200] [29209] [INFO] ------
[2019-04-30 19:23:36 +0200] [29209] [INFO] Delivering Follow ===>> https://todon.nl/@mrb
[2019-04-30 19:23:36 +0200] [29209] [INFO] Fetch https://todon.nl/@mrb, status: 200, OK
[2019-04-30 19:23:36 +0200] [29209] [INFO] Post to inbox https://todon.nl/users/mrb/inbox, status: 401, Unauthorized
autogestion commented 5 years ago

Tested with my instance. Looks like there is some issue with todon.nl. I'll look into it

[2019-04-30 17:40:45 +0000] - (sanic.access)[INFO][1:2]: 46.96.32.178 --->> POST http://pubgate.autogestion.org/testtgbot/outbox  201 18
[2019-04-30 17:40:45 +0000] [26214] [INFO] Delivering Follow ===>> https://todon.nl/@mrb
[2019-04-30 17:40:45 +0000] [26214] [INFO] Fetch https://todon.nl/@mrb, status: 200, OK
[2019-04-30 17:40:46 +0000] - (sanic.access)[INFO][1:2]: 173.249.16.10 --->> GET http://pubgate.autogestion.org/testtgbot  200 1868
[2019-04-30 17:40:47 +0000] - (sanic.access)[INFO][1:2]: 173.249.16.10 --->> GET http://pubgate.autogestion.org/.well-known/host-meta  404 53
[2019-04-30 17:40:47 +0000] [26214] [INFO] Post to inbox https://todon.nl/users/mrb/inbox, status: 401, Unauthorized

[2019-04-30 17:42:22 +0000] - (sanic.access)[INFO][1:2]: 46.96.32.178 --->> POST http://pubgate.autogestion.org/testtgbot/outbox  201 18
[2019-04-30 17:42:22 +0000] [26214] [INFO] Delivering Follow ===>> https://mastodon.social/@siderus
[2019-04-30 17:42:22 +0000] [26214] [INFO] Fetch https://mastodon.social/@siderus, status: 200, OK
[2019-04-30 17:42:22 +0000] [26214] [INFO] Post to inbox https://mastodon.social/users/siderus/inbox, status: 202, Accepted
[2019-04-30 17:42:23 +0000] [26214] [INFO] Fetch https://mastodon.social/users/siderus#main-key, status: 200, OK
[2019-04-30 17:42:23 +0000] - (sanic.access)[INFO][1:2]: 136.243.88.169 --->> POST http://pubgate.autogestion.org/testtgbot/inbox  202 18
autogestion commented 5 years ago

@mrvdb I fixed following pixelfed. successfully delivered image from SneekyPeet@pixelfed.social to @galiabarda . there was problem with http sig. todon.nl still an issue to play with pixelfed you should update pubgate to last (0.2.11) version

autogestion commented 5 years ago

todon.nl is tricky) it has same version as mastodon.social (2.8.0), and accepts follow requests from other mastodon instances. mastodon.social accepts requests from pubgate and todon.nl refuses to

autogestion commented 5 years ago

@mrvdb todon issue is solved as well))

[2019-04-30 20:38:23 +0000] [9782] [INFO] Delivering Follow ===>> https://todon.nl/@mrb
[2019-04-30 20:38:23 +0000] [9782] [INFO] Fetch https://todon.nl/@mrb, status: 200, OK
[2019-04-30 20:38:23 +0000] - (sanic.access)[INFO][1:2]: 173.249.16.10 --->> GET http://pubgate.autogestion.org/utest4  200 1491
[2019-04-30 20:38:24 +0000] - (sanic.access)[INFO][1:2]: 173.249.16.10 --->> GET http://pubgate.autogestion.org/.well-known/webfinger?resource=acct:utest4@pubgate.
autogestion.org  200 262
[2019-04-30 20:38:24 +0000] - (sanic.access)[INFO][1:2]: 173.249.16.10 --->> GET http://pubgate.autogestion.org/utest4/outbox  200 6413
[2019-04-30 20:38:24 +0000] - (sanic.access)[INFO][1:2]: 173.249.16.10 --->> GET http://pubgate.autogestion.org/utest4/following  200 519
[2019-04-30 20:38:24 +0000] - (sanic.access)[INFO][1:2]: 173.249.16.10 --->> GET http://pubgate.autogestion.org/utest4/followers  200 640
[2019-04-30 20:38:25 +0000] [9782] [INFO] Post to inbox https://todon.nl/users/mrb/inbox, status: 202, Accepted

This succesful follow is for pubgate user which was created as "profile": {"type": "Person",.... You created a bot user, which has "profile": {"type": "Service", There is some #nobot issue in fediverse, to restrict bots from following users. So, maybe in your account preferences, or in settings of todon.nl instance there is restriction for bots

So, to broadcast from todon, there are 2 ways

mrvdb commented 5 years ago

Still not working for me, behaviour slightly different for pixelfed, todon.nl the same

Pixelfed:

[2019-05-01 10:56:27 +0200] [5494] [INFO] Delivering Follow ===>> https://pixelfed.social/mrb
[2019-05-01 10:56:27 +0200] [5494] [INFO] Fetch https://pixelfed.social/mrb, status: 200, OK
[2019-05-01 10:56:27 +0200] [5494] [INFO] Post to inbox https://pixelfed.social/users/mrb/inbox, status: 400, Bad Request
{'@context': 'https://www.w3.org/ns/activitystreams',
 'actor': 'http://activitypubbot.qua.name/ap_bot',
 'id': 'http://activitypubbot.qua.name/ap_bot/activity/619d05623647716b',
 'object': 'https://pixelfed.social/mrb',
 'type': 'Follow'}

Also noticed unfollow is always delivered to pubgate user on mastodon.social (at least according to log)


[2019-05-01 10:56:14 +0200] [5494] [INFO] Delivering Unfollow ===>> https://mastodon.social/users/pubgate
[2019-05-01 10:56:14 +0200] [5494] [INFO] Fetch https://mastodon.social/users/pubgate, status: 200, OK
[2019-05-01 10:56:14 +0200] [5494] [INFO] Post to inbox https://mastodon.social/users/pubgate/inbox, status: 401, Unauthorized
{'@context': 'https://www.w3.org/ns/activitystreams',
 'actor': 'http://activitypubbot.qua.name/ap_bot',
 'id': 'http://activitypubbot.qua.name/ap_bot/activity/7fbe15942150507b',
 'object': 'https://pixelfed.social/mrb',
 'type': 'Unfollow'}
autogestion commented 5 years ago

could you also change "METHOD" in config/conf.cfg to "https"

mrvdb commented 5 years ago

That was already the case.

mrvdb commented 5 years ago

I got todon.nl to work by:

autogestion commented 5 years ago

user id starts with http Screenshot_16 same was the value of "METHOD" when user was created. I think this cause bad request in pixelfed it could be updated in mongodb, in user.uri or you can try create new user

as for todon, I don't think that 2 actions really affected. preferredUsername is what mastodon shows at first place when you see user's toot (then goes username with @). maybe todon has some timeout, after which it refetch profile (and discovered that bot is not bot anymore)

autogestion commented 5 years ago

Screenshot_17

mrvdb commented 5 years ago
autogestion commented 5 years ago

yes, my mistake. that value on the screen, which I marked as "preferredUsername" is a value of "name" attribute. I guess some time before it was opposite in mastodon)

mrvdb commented 5 years ago

Ok, follow works, but unfollow is not unregistering:

image

Subsequent calls to unfollow issue internal server error

autogestion commented 5 years ago

@mrvdb Actually, it's unregistering server error happens after db was updated and Unfollow was sent to remote server, when rendering API response. you can drop that duplicates in following list by calling outbox with Unfollow object with corresponding number of requests. Earlier, mastodon refuses to accept Follow requests, if it is already accepted one. Looks like its behavior changed, now mastodon just sends accept on every follow request

I'll provide a patch later today, with API response fix and with restriction to send multiple follow requests to same user, if it already accepted follow. Both this issues do not break operability of the bot, simply create confusion

mrvdb commented 5 years ago

ok, thanks.

Two suggestions: perhaps use parse_mode=html (default is markdown) and link_preview=False for the default send_message call?

autogestion commented 5 years ago

following issues fixed

good point with parse_mode. As for link preview, as it was first approach, I implemented delivering images in such way, that bot extract image url from AP object, and sends it to Telegram as a text. So if preview enabled, it allows to see picture at tg client at once, I guess. It is possible to download image by url, and to send it to Telegram as file. But I'm not sure it's better way, than sending url

mrvdb commented 5 years ago

There are, if I recall correctly two sorts of preview;

  1. link preview: which is what happens when inserting a link (not only image links). The effect is often for toots or tweets coming in, that the account link gets previewed. See screenshot. This distracts a lot from the content as you can see.

image

  1. The previewing of image content (which may or may not need link_preview, I don't recall by head)
autogestion commented 5 years ago

got it. could you add this arguments to send_message and create PR?

mrvdb commented 5 years ago

Sure, will do tomorrow

mrvdb commented 5 years ago

This can be closed, right?

autogestion commented 5 years ago

implemented