@@ -233,71 +233,134 @@ async def send_to_nerimity(cls, updated_json, endpoint, attachments: list = None
233233 response_json = updated_json
234234 sender_channel = response_json ["meta" ]["sender-channel" ]
235235 discord_channels = response_json ["config" ]["channels" ]["discord" ]
236- try :
237- if sender_channel in discord_channels :
238- channel_id = response_json ["config" ]["channels" ]["nerimity" ][discord_channels .index (sender_channel )]
239- elif sender_channel in response_json ["config" ]["channels" ]["guilded" ]:
240- channel_id = response_json ["config" ]["channels" ]["nerimity" ][response_json ["config" ]["channels" ]["guilded" ].index (sender_channel )]
241- elif sender_channel in response_json ["config" ]["channels" ]["revolt" ]:
242- channel_id = response_json ["config" ]["channels" ]["nerimity" ][response_json ["config" ]["channels" ]["revolt" ].index (sender_channel )]
243- else :
244- raise errors .SendingError .ChannelNotFound (f'The channel { sender_channel } ({ updated_json ["meta" ]["sender" ]} ) does not seem to be a registered channel on other platforms.' )
245- except IndexError :
246- await surrealdb_handler .QueueHandler .remove_from_queue (endpoint , updated_json )
247- return True
248- message_author_name = response_json ["meta" ]["message" ]["author" ]["name" ]
249- message_content = response_json ["meta" ]["message" ].get ("content" )
250- if message_content is not None and message_content != "" :
251- message_content = await emoji_handler .convert_message (message_content , updated_json ["meta" ]["sender" ], "nerimity" , endpoint )
252- if updated_json ["config" ]["isbeta" ] is True :
253- headers = {
236+ nerimity_webhooks = response_json ["config" ]["webhooks" ]["nerimity" ]
237+ if updated_json .get ("config" , {}).get ("isbeta" , {}) is True and len (nerimity_webhooks ) > 0 :
238+ print ("Sending to Nerimity via webhook..." )
239+ try :
240+ if sender_channel in discord_channels :
241+ webhook_url = response_json ["config" ]["webhooks" ]["nerimity" ][discord_channels .index (sender_channel )]
242+ elif sender_channel in response_json ["config" ]["channels" ]["guilded" ]:
243+ webhook_url = response_json ["config" ]["webhooks" ]["nerimity" ][response_json ["config" ]["channels" ]["guilded" ].index (sender_channel )]
244+ elif sender_channel in response_json ["config" ]["channels" ]["revolt" ]:
245+ webhook_url = response_json ["config" ]["webhooks" ]["nerimity" ][response_json ["config" ]["channels" ]["revolt" ].index (sender_channel )]
246+ else :
247+ raise errors .SendingError .ChannelNotFound (f'The channel { sender_channel } ({ updated_json ["meta" ]["sender" ]} ) does not seem to be a registered channel on other platforms.' )
248+ except IndexError :
249+ await surrealdb_handler .QueueHandler .remove_from_queue (endpoint , updated_json )
250+ return True
251+ message_author_name = response_json ["meta" ]["message" ]["author" ]["name" ]
252+ message_content = response_json ["meta" ]["message" ].get ("content" )
253+ if message_content is not None and message_content != "" :
254+ message_content = await emoji_handler .convert_message (message_content , updated_json ["meta" ]["sender" ], "nerimity" , endpoint )
255+ print (webhook_url )
254256
255- "Authorization" : f"{ config .BETA_NERIMITY_TOKEN } " ,
257+ payload = {
258+ "username" : message_author_name ,
259+ "content" : message_content ,
260+ "avatar_url" : response_json ["meta" ]["message" ]["author" ]["avatar" ]
256261 }
262+ if updated_json ["meta" ]["message" ]["isReply" ] is True :
263+ reply_emoji_filtered = await emoji_handler .convert_message (updated_json ["meta" ]["message" ]["reply" ]["message" ], updated_json ["meta" ]["sender" ], "nerimity" , endpoint )
264+ payload = {
265+ "content" : f"> **{ updated_json ['meta' ]['message' ]['reply' ]['author' ]} **: { reply_emoji_filtered } \n \n { message_content } " ,
266+ }
267+ nerimityCdnFileId = None
268+ if attachments is not None :
269+ formdata = aiohttp .FormData ()
270+ try :
271+ if '../' in attachments [0 ].name or '..\\ ' in attachments [0 ].name :
272+ raise Exception ("Invalid file path" )
273+ else :
274+ formdata .add_field ("f" , open (os .path .abspath (attachments [0 ].name ), "rb" ), filename = attachments [0 ].name .split ("/" )[- 1 ], content_type = f"image/{ attachments [0 ].name .split ('.' )[- 1 ]} " )
275+ async with session .post ("https://cdn.nerimity.com/upload" , headers = headers , data = formdata ) as r :
276+ if r .ok :
277+ nerimityCdnFileId = (await r .json ())["fileId" ]
278+ async with session .post (f"https://cdn.nerimity.com/attachments/{ int (channel_id )} /{ nerimityCdnFileId } " , headers = headers ) as r :
279+ if r .ok :
280+ nerimityCdnFileId = (await r .json ())["fileId" ]
281+ payload ["nerimityCdnFileId" ] = nerimityCdnFileId
282+ else :
283+ raise errors .SendingError .SendFromNerimiryError (f"Failed to upload attachment to nerimity. Response: { r .status } , { r .reason } " )
284+ else :
285+ raise errors .SendingError .SendFromNerimiryError (f"Failed to upload attachment to nerimity. Response: { r .status } , { r .reason } " )
286+ except Exception as e :
287+ print ("Error uploading attachment to nerimity: " , e )
288+ pass
289+ r = await session .post (webhook_url , data = payload )
290+ print (f"Sent to nerimity. Response: { r .status } , { r .reason } { await r .text ()} " )
291+ await session .close ()
292+ if attachments is not None :
293+ await surrealdb_handler .AttachmentProcessor .update_attachment (attachments [0 ].name .split ("/" )[- 1 ].split ("." )[0 ], sentby = "nerimity" )
294+ asyncio .create_task (read_handler .ReadHandler .mark_read (endpoint , "nerimity" ))
295+ print ("Sent to nerimity" )
296+ return True
257297 else :
258- headers = {
259- "Authorization" : f"{ config .NERIMITY_TOKEN } " ,
260- }
261- print (channel_id )
298+ print ("Sending to Nerimity via API..." )
299+ try :
300+ if sender_channel in discord_channels :
301+ channel_id = response_json ["config" ]["channels" ]["nerimity" ][discord_channels .index (sender_channel )]
302+ elif sender_channel in response_json ["config" ]["channels" ]["guilded" ]:
303+ channel_id = response_json ["config" ]["channels" ]["nerimity" ][response_json ["config" ]["channels" ]["guilded" ].index (sender_channel )]
304+ elif sender_channel in response_json ["config" ]["channels" ]["revolt" ]:
305+ channel_id = response_json ["config" ]["channels" ]["nerimity" ][response_json ["config" ]["channels" ]["revolt" ].index (sender_channel )]
306+ else :
307+ raise errors .SendingError .ChannelNotFound (f'The channel { sender_channel } ({ updated_json ["meta" ]["sender" ]} ) does not seem to be a registered channel on other platforms.' )
308+ except IndexError :
309+ await surrealdb_handler .QueueHandler .remove_from_queue (endpoint , updated_json )
310+ return True
311+ message_author_name = response_json ["meta" ]["message" ]["author" ]["name" ]
312+ message_content = response_json ["meta" ]["message" ].get ("content" )
313+ if message_content is not None and message_content != "" :
314+ message_content = await emoji_handler .convert_message (message_content , updated_json ["meta" ]["sender" ], "nerimity" , endpoint )
315+ if updated_json ["config" ]["isbeta" ] is True :
316+ headers = {
317+
318+ "Authorization" : f"{ config .BETA_NERIMITY_TOKEN } " ,
319+ }
320+ else :
321+ headers = {
322+ "Authorization" : f"{ config .NERIMITY_TOKEN } " ,
323+ }
324+ print (channel_id )
262325
263- payload = {
264- "content" : f"**{ message_author_name } **: { message_content } " ,
265- }
266- if updated_json ["meta" ]["message" ]["isReply" ] is True :
267- reply_emoji_filtered = await emoji_handler .convert_message (updated_json ["meta" ]["message" ]["reply" ]["message" ], updated_json ["meta" ]["sender" ], "nerimity" , endpoint )
268326 payload = {
269- "content" : f"> ** { updated_json [ 'meta' ][ 'message' ][ 'reply' ][ 'author' ] } **: { reply_emoji_filtered } \n \n **{ message_author_name } **: { message_content } " ,
327+ "content" : f"**{ message_author_name } **: { message_content } " ,
270328 }
271- nerimityCdnFileId = None
272- if attachments is not None :
273- formdata = aiohttp .FormData ()
274- try :
275- if '../' in attachments [0 ].name or '..\\ ' in attachments [0 ].name :
276- raise Exception ("Invalid file path" )
277- else :
278- formdata .add_field ("f" , open (os .path .abspath (attachments [0 ].name ), "rb" ), filename = attachments [0 ].name .split ("/" )[- 1 ], content_type = f"image/{ attachments [0 ].name .split ('.' )[- 1 ]} " )
279- async with session .post ("https://cdn.nerimity.com/upload" , headers = headers , data = formdata ) as r :
280- if r .ok :
281- nerimityCdnFileId = (await r .json ())["fileId" ]
282- async with session .post (f"https://cdn.nerimity.com/attachments/{ int (channel_id )} /{ nerimityCdnFileId } " , headers = headers ) as r :
283- if r .ok :
284- nerimityCdnFileId = (await r .json ())["fileId" ]
285- payload ["nerimityCdnFileId" ] = nerimityCdnFileId
286- else :
287- raise errors .SendingError .SendFromNerimiryError (f"Failed to upload attachment to nerimity. Response: { r .status } , { r .reason } " )
288- else :
289- raise errors .SendingError .SendFromNerimiryError (f"Failed to upload attachment to nerimity. Response: { r .status } , { r .reason } " )
290- except Exception as e :
291- print ("Error uploading attachment to nerimity: " , e )
292- pass
293- r = await session .post (f"https://nerimity.com/api/channels/{ int (channel_id )} /messages" , headers = headers , data = payload )
294- print (f"Sent to nerimity. Response: { r .status } , { r .reason } { await r .text ()} " )
295- await session .close ()
296- if attachments is not None :
297- await surrealdb_handler .AttachmentProcessor .update_attachment (attachments [0 ].name .split ("/" )[- 1 ].split ("." )[0 ], sentby = "nerimity" )
298- asyncio .create_task (read_handler .ReadHandler .mark_read (endpoint , "nerimity" ))
299- print ("Sent to nerimity" )
300- return True
329+ if updated_json ["meta" ]["message" ]["isReply" ] is True :
330+ reply_emoji_filtered = await emoji_handler .convert_message (updated_json ["meta" ]["message" ]["reply" ]["message" ], updated_json ["meta" ]["sender" ], "nerimity" , endpoint )
331+ payload = {
332+ "content" : f"> **{ updated_json ['meta' ]['message' ]['reply' ]['author' ]} **: { reply_emoji_filtered } \n \n **{ message_author_name } **: { message_content } " ,
333+ }
334+ nerimityCdnFileId = None
335+ if attachments is not None :
336+ formdata = aiohttp .FormData ()
337+ try :
338+ if '../' in attachments [0 ].name or '..\\ ' in attachments [0 ].name :
339+ raise Exception ("Invalid file path" )
340+ else :
341+ formdata .add_field ("f" , open (os .path .abspath (attachments [0 ].name ), "rb" ), filename = attachments [0 ].name .split ("/" )[- 1 ], content_type = f"image/{ attachments [0 ].name .split ('.' )[- 1 ]} " )
342+ async with session .post ("https://cdn.nerimity.com/upload" , headers = headers , data = formdata ) as r :
343+ if r .ok :
344+ nerimityCdnFileId = (await r .json ())["fileId" ]
345+ async with session .post (f"https://cdn.nerimity.com/attachments/{ int (channel_id )} /{ nerimityCdnFileId } " , headers = headers ) as r :
346+ if r .ok :
347+ nerimityCdnFileId = (await r .json ())["fileId" ]
348+ payload ["nerimityCdnFileId" ] = nerimityCdnFileId
349+ else :
350+ raise errors .SendingError .SendFromNerimiryError (f"Failed to upload attachment to nerimity. Response: { r .status } , { r .reason } " )
351+ else :
352+ raise errors .SendingError .SendFromNerimiryError (f"Failed to upload attachment to nerimity. Response: { r .status } , { r .reason } " )
353+ except Exception as e :
354+ print ("Error uploading attachment to nerimity: " , e )
355+ pass
356+ r = await session .post (f"https://nerimity.com/api/channels/{ int (channel_id )} /messages" , headers = headers , data = payload )
357+ print (f"Sent to nerimity. Response: { r .status } , { r .reason } { await r .text ()} " )
358+ await session .close ()
359+ if attachments is not None :
360+ await surrealdb_handler .AttachmentProcessor .update_attachment (attachments [0 ].name .split ("/" )[- 1 ].split ("." )[0 ], sentby = "nerimity" )
361+ asyncio .create_task (read_handler .ReadHandler .mark_read (endpoint , "nerimity" ))
362+ print ("Sent to nerimity" )
363+ return True
301364 else :
302365 return False
303366 except Exception as e :
0 commit comments