async def play_duel( accepting_person_fb_id: str ) -> Tuple[str, Union[List[fbchat.Mention]], None]: mention = None accepting_person_fb_id_money = await handling_casino_sql.fetch_user_money( accepting_person_fb_id) try: accepting_person_fb_id_money = float(accepting_person_fb_id_money) except ValueError: return accepting_person_fb_id_money, mention duel_data = await handling_casino_sql.fetch_duel_info( accepting_person_fb_id) if len(duel_data) == 0: message = "🚫 Nie masz żadnych zaproszeń do gry" else: wage, duel_creator, opponent = duel_data[0] if accepting_person_fb_id_money < wage: message = f"🚫 Nie masz wystarczająco pieniędzy (Stawka: {wage}, ty posiadasz {'%.2f' % accepting_person_fb_id_money} dogecoinów)" else: await handling_casino_sql.insert_into_user_money( accepting_person_fb_id, accepting_person_fb_id_money - wage) winner = rd.choice([duel_creator, opponent]) winner_money = await handling_casino_sql.fetch_user_money(winner) winner_money += Decimal(wage * 2) await handling_casino_sql.insert_into_user_money( winner, winner_money) message = f"✨ Osoba która wygrała {'%.2f' % float(wage*2)} dogecoinów" mention = [fbchat.Mention(thread_id=winner, offset=0, length=45)] await handling_casino_sql.delete_duels(duel_creator) return message, mention
async def mention_everyone(self, event: fbchat.MessageEvent, group_info: fbchat.GroupData): mentions = [ fbchat.Mention(thread_id=participant.id, offset=0, length=12) for participant in group_info.participants ] await self.send_text_message(event, "💬 ELUWA ALL", mentions=mentions)
async def send_message_with_random_mention(self, event: fbchat.MessageEvent, group_info: fbchat.GroupData): lucky_member = rd.choice(group_info.participants).id mention = [fbchat.Mention(thread_id=lucky_member, offset=0, length=12)] await self.send_text_message(event, "🎆 Zwycięzca", mentions=mention, reply_to_id=event.message.id)
async def everyone(event: fbchat.MessageEvent): if '@everyone' in event.message.text: if perm.check('everyone', event.thread.id, event.author.id): if isinstance(event.thread, fbchat.Group): group = await WiertarBot.client.fetch_thread_info( [event.thread.id]).__anext__() mentions = [ fbchat.Mention(thread_id=participant.id, offset=0, length=9) for participant in group.participants ] await event.thread.send_text('@everyone', mentions=mentions)
async def szkaluj(event: fbchat.MessageEvent) -> Response: """ Użycie: {command} (oznaczenie/random) Zwraca: tekst szkalujący osobę """ text = event.message.text.lower() is_group_and_random = (event.thread.id != event.author.id and text.count(' ') == 1 and text.endswith(' random')) if is_group_and_random: thread = await WiertarBot.client.fetch_thread_info([event.thread.id] ).__anext__() uid = random.choice(thread.participants).id elif event.message.mentions: uid = event.message.mentions[0].thread_id else: uid = event.author.id user = await WiertarBot.client.fetch_thread_info([uid]).__anext__() name = user.name path = cmd_media_path / 'random/szkaluj.txt' with path.open('r', encoding='utf-8') as f: lines = f.readlines() msg = random.choice(lines) del lines msg = msg.replace('%n%', '\n') mentions = [] while '%on%' in msg: mention = fbchat.Mention(thread_id=uid, offset=msg.find('%on%'), length=len(name)) mentions.append(mention) msg = msg.replace('%on%', name, 1) return Response(event, text=msg, mentions=mentions)
def test_send_text_with_mention(any_thread): mention = fbchat.Mention(thread_id=any_thread.id, offset=5, length=8) assert any_thread.send_text("Test @mention", mentions=[mention])
# Will send a message to the thread thread.send_text("<message>") # Will send the default `like` emoji thread.send_sticker(fbchat.EmojiSize.LARGE.value) # Will send the emoji `👍` thread.send_emoji("👍", size=fbchat.EmojiSize.LARGE) # Will send the sticker with ID `767334476626295` thread.send_sticker("767334476626295") # Will send a message with a mention thread.send_text( text="This is a @mention", mentions=[fbchat.Mention(thread.id, offset=10, length=8)], ) # Will send the image located at `<image path>` with open("<image path>", "rb") as f: files = session._upload([("image_name.png", f, "image/png")]) thread.send_text(text="This is a local image", files=files) # Will download the image at the URL `<image url>`, and then send it r = requests.get("<image url>") files = session._upload([("image_name.png", r.content, "image/png")]) thread.send_files(files) # Alternative to .send_text # Only do these actions if the thread is a group if isinstance(thread, fbchat.Group): # Will remove the user with ID `<user id>` from the group
async def main(): session = await fbchat.Session.login("<email>", "<password>") client = fbchat.Client(session) thread = session.user # thread = fbchat.User(session=session, id="0987654321") # thread = fbchat.Group(session=session, id="1234567890") # Will send a message to the thread await thread.send_text("<message>") # Will send the default `like` emoji await thread.send_sticker(fbchat.EmojiSize.LARGE.value) # Will send the emoji `👍` await thread.send_emoji("👍", size=fbchat.EmojiSize.LARGE) # Will send the sticker with ID `767334476626295` await thread.send_sticker("767334476626295") # Will send a message with a mention await thread.send_text( text="This is a @mention", mentions=[fbchat.Mention(thread.id, offset=10, length=8)], ) # Will send the image located at `<image path>` with open("<image path>", "rb") as f: files = await client.upload([("image_name.png", f, "image/png")]) await thread.send_text(text="This is a local image", files=files) # Will download the image at the URL `<image url>`, and then send it async with ClientSession() as sess, sess.get("<image url>") as resp: image_data = await resp.read() files = await client.upload([("image_name.png", image_data, "image/png")]) await thread.send_files(files) # Alternative to .send_text # Only do these actions if the thread is a group if isinstance(thread, fbchat.Group): # Will remove the user with ID `<user id>` from the group await thread.remove_participant("<user id>") # Will add the users with IDs `<1st user id>`, `<2nd user id>` and `<3th user id>` to the group await thread.add_participants( ["<1st user id>", "<2nd user id>", "<3rd user id>"]) # Will change the title of the group to `<title>` await thread.set_title("<title>") # Will change the nickname of the user `<user id>` to `<new nickname>` await thread.set_nickname("<user id>", "<new nickname>") # Will set the typing status of the thread await thread.start_typing() # Will change the thread color to #0084ff await thread.set_color("#0084ff") # Will change the thread emoji to `👍` await thread.set_emoji("👍") message = fbchat.Message(thread=thread, id="<message id>") # Will react to a message with a 😍 emoji await message.react("😍")
async def see(event: fbchat.MessageEvent) -> Response: """ Użycie: {command} (ilosc<=10) Zwraca: jedną lub więcej ostatnio usuniętych wiadomości w wątku """ try: n = int(event.message.text.split(' ', 1)[1]) if n > 10: n = 10 elif n < 1: n = 1 except (IndexError, ValueError): n = 1 messages: Iterable[FBMessage] = FBMessage\ .select(FBMessage.message)\ .where( FBMessage.deleted_at != None, FBMessage.thread_id == event.thread.id )\ .order_by(FBMessage.time.desc())\ .limit(n) send_responses: List[Awaitable] = [] for message in messages: message = json.loads(message.message) mentions = [ fbchat.Mention(**mention) for mention in message['mentions'] ] voice_clip = False files = [] for att in message['attachments']: if att['type'] == 'ImageAttachment': p = attachment_save_path / f'{ att["id"] }.{ att["original_extension"] }' files.append(str(p)) elif att['type'] == 'AudioAttachment': p = attachment_save_path / att['filename'] files.append(str(p)) voice_clip = True elif att['type'] == 'VideoAttachment': p = attachment_save_path / f'{ att["id"] }.mp4' files.append(str(p)) response = Response( event, text=message['text'], mentions=mentions, files=files, voice_clip=voice_clip ) send_responses.append(response.send()) if send_responses: await asyncio.gather(*send_responses) else: return Response(event, text='Nie ma żadnych zapisanych usuniętych wiadomości w tym wątku')