Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import fbchat
- import random as rd
- import requests
- import feedparser
- from bs4 import BeautifulSoup
- import json
- import asyncio
- class Bot(fbchat.Client):
- async def on_inbox(self, unseen=None, unread=None, recent_unread=None, msg=None):
- thread_ids = []
- thread_ids += [thread.uid for thread in await self.fetch_thread_list(thread_location=fbchat.ThreadLocation.PENDING, limit=20)]
- thread_ids += [thread.uid for thread in await self.fetch_thread_list(thread_location=fbchat.ThreadLocation.OTHER, limit=20)]
- await self.move_threads(fbchat.ThreadLocation.INBOX, thread_ids)
- async def on_people_added(self,mid=None,added_ids=None,author_id=None,thread_id=None,at=None,msg=None,):
- if thread_id in mutelist:
- pass
- else:
- await self.send(fbchat.Message("Witaj w grupie! Jeśli chcesz zobaczyć moje funckje napisz help"), thread_id=thread_id, thread_type=fbchat.ThreadType.GROUP)
- async def on_person_removed(self,mid=None,removed_id=None,author_id=None,thread_id=None,at=None,msg=None,):
- if thread_id in mutelist:
- pass
- else:
- await self.send(fbchat.Message("ktos upuscil grupe"), thread_id=thread_id, thread_type=fbchat.ThreadType.GROUP)
- async def on_message(self,mid=None,author_id=None,message_object=None,thread_id=None,thread_type=fbchat.ThreadType.USER,at=None,metadata=None,msg=None,):
- if author_id != self.uid:
- message_text = str(message_object.text).lower()
- if message_object.text == "!mem":
- drawing_meme = str(rd.randint(1, 199))
- await self.send_local_files(["mem"+drawing_meme+".png"], thread_id=thread_id, thread_type=thread_type)
- elif message_object.text == "!koronawirus":
- data = requests.get("https://coronavirus-19-api.herokuapp.com/all").json()
- message = f"Total confirmed: {data['cases']}\nTotal deaths: {data['deaths']}\nTotal recovered: {data['recovered']}\nExisting: {data['cases'] - data['deaths'] - data['recovered']}"
- await self.send(fbchat.Message(text=message), thread_id=thread_id, thread_type=thread_type)
- elif message_object.text == "!koronawiruspl":
- data = requests.get("https://coronavirus-19-api.herokuapp.com/countries").json()
- for i in data:
- if i["country"] == "Poland":
- message = f"Total confirmed: {i['cases']}\nToday confirmed: {i['todayCases']}\nTotal deaths: {i['deaths']}\nTotal recovered: {i['recovered']}\nExisting: {i['active']}"
- await self.send(fbchat.Message(text=message), thread_id=thread_id, thread_type=thread_type)
- pass
- elif message_object.text == "!luckymember":
- if thread_type == fbchat.ThreadType.GROUP:
- group_members = list((await self.fetch_thread_info(thread_id))[thread_id].participants)
- lucky_member = rd.randint(0,len(group_members)-1)
- lucky_member = group_members[lucky_member]
- await self.send(fbchat.Message(text="Zwycięzca", mentions=[fbchat.Mention(lucky_member)]),thread_id=thread_id, thread_type=thread_type)
- else:
- await self.send(fbchat.Message("To komenda dla grup"),thread_id=thread_id,thread_type=thread_type)
- if message_object.text == "!para":
- if thread_type == fbchat.ThreadType.GROUP:
- group_members = list((await self.fetch_thread_info(thread_id))[thread_id].participants)
- first_person = rd.randint(0,len(group_members)-1)
- first_person = group_members[first_person]
- secend_person = rd.randint(0, len(group_members)-1)
- secend_person = group_members[secend_person]
- await self.send(fbchat.Message(text="Mąz", mentions=[fbchat.Mention(first_person)]),thread_id=thread_id, thread_type=thread_type)
- await self.send(fbchat.Message(text="Żona", mentions=[fbchat.Mention(secend_person)]),thread_id=thread_id, thread_type=thread_type)
- else:
- await self.send(fbchat.Message("To komenda dla grup"),thread_id=thread_id,thread_type=thread_type)
- elif message_object.text == "!disco":
- for i in range(0,20):
- change_color = rd.choice([fbchat.ThreadColor.BRIGHT_TURQUOISE, fbchat.ThreadColor.BILOBA_FLOWER, fbchat.ThreadColor.BRILLIANT_ROSE, fbchat.ThreadColor.CAMEO, fbchat.ThreadColor.DARK_TANGERINE, fbchat.ThreadColor.DEEP_SKY_BLUE, fbchat.ThreadColor.FERN, fbchat.ThreadColor.FREE_SPEECH_GREEN, fbchat.ThreadColor.GOLDEN_POPPY, fbchat.ThreadColor.LIGHT_CORAL, fbchat.ThreadColor.MALACHITE, fbchat.ThreadColor.MEDIUM_SLATE_BLUE, fbchat.ThreadColor.MESSENGER_BLUE, fbchat.ThreadColor.PICTON_BLUE, fbchat.ThreadColor.PUMPKIN, fbchat.ThreadColor.RADICAL_RED, fbchat.ThreadColor.RUBY, fbchat.ThreadColor.SHOCKING, fbchat.ThreadColor.TICKLE_ME_PINK, fbchat.ThreadColor.VIKING])
- await self.change_thread_color(color=change_color ,thread_id=thread_id)
- elif message_object.text == "!utrudnieniawawa":
- feed = feedparser.parse('https://www.wtp.waw.pl/feed/?post_type=impediment')
- wiadomosc = ""
- for entry in feed['entries']:
- wiadomosc += str(entry.title).upper() + "\n"
- wiadomosc += entry.published[:-9] + "\n"
- wiadomosc += entry.summary + "\n"
- wiadomosc += "\n"
- if wiadomosc == "":
- await self.send(fbchat.Message("Brak utrudnień w Warszawie :)"), thread_id=thread_id, thread_type=thread_type)
- else:
- await self.send(fbchat.Message(wiadomosc), thread_id=thread_id, thread_type=thread_type)
- elif message_object.text == "!utrudnieniawroclaw":
- data = requests.get("https://www.facebook.com/mpkwroc/")
- soup = BeautifulSoup(data.text, "html.parser")
- sformatowane_dane = ""
- for i in soup.find_all("p"):
- sformatowane_dane += str(i) + "\n"
- sformatowane_dane = BeautifulSoup(sformatowane_dane, "html.parser").text
- await self.send(fbchat.Message(sformatowane_dane), thread_id=thread_id, thread_type=thread_type)
- elif message_object.text == "!utrudnienialodz":
- data = requests.get("http://www.mpk.lodz.pl/rozklady/utrudnienia.jsp")
- soup = BeautifulSoup(data.text, "html.parser")
- sformatowane_dane = ""
- for i in soup.find_all("p"):
- if "MPK na facebook".lower() in str(i).lower():
- sformatowane_dane += "\n"
- elif "Zamknij" in str(i):
- break
- else:
- sformatowane_dane += str(i) + "\n"
- sformatowane_dane = BeautifulSoup(sformatowane_dane, "html.parser").text
- await self.send(fbchat.Message(text="Właścicielami danych jest http://www.mpk.lodz.pl/rozklady/utrudnienia.jsp\n{0} ".format(sformatowane_dane)), thread_id=thread_id, thread_type=thread_type)
- elif "!pogoda" in message_object.text:
- try:
- api_address = 'http://api.openweathermap.org/data/2.5/weather?appid=myid&q='
- city = message_object.text[8:]
- url = api_address + city
- json_data = requests.get(url).json()
- temperature = json_data["main"]["temp"]
- temperature -= 273
- feel_temperature = json_data["main"]["feels_like"]
- feel_temperature -= 273
- weather = json_data["weather"][0]["description"]
- preassure = json_data["main"]["pressure"]
- humidity = json_data["main"]["humidity"]
- wind_speed = json_data["wind"]["speed"]
- full_weather = """Pogoda w {0}
- Temperatura: {1} C
- Odczuwalna: {2} C
- Atmosfera: {3}
- Ciśnienie: {4} hPa
- Wilgotność: {5} %
- Prędkość wiatru: {6} m/s""".format(str(city).title(),int(temperature),int(feel_temperature),weather,preassure,humidity,wind_speed)
- await self.send(fbchat.Message(full_weather),thread_id=thread_id, thread_type=thread_type)
- except:
- await self.send(fbchat.Message("Nie znalezione takiej miejscowości, lub linux nie umie odczytać polskiego znaku. Spróbuj wpisać nazwe miejscowości bez polskich znakow"),thread_id=thread_id, thread_type=thread_type)
- elif message_object.text == "!ruletka":
- if thread_type == fbchat.ThreadType.GROUP:
- group = (await self.fetch_group_info(thread_id))[thread_id]
- group_admins = group.admins
- group_members = list((await self.fetch_thread_info(thread_id))[thread_id].participants)
- member_to_kick = rd.choice(group_members)
- if self.uid not in group_admins:
- await self.send(fbchat.Message("Nie mam admina (jeśli chcesz by działała ta komenda, musisz dać mi admina na tej grupie)"), thread_id=thread_id, thread_type=thread_type)
- elif author_id not in group_admins:
- await self.send(fbchat.Message("To komenda tylko dla adminów (tylko admini danej grupy mogą używac tej komendy)"),thread_id=thread_id, thread_type=thread_type)
- else:
- if member_to_kick in group_admins:
- await self.send(fbchat.Message("Wylosowalo admina. Nie moge go usunąć"),thread_id=thread_id ,thread_type=thread_type)
- else:
- await self.remove_user_from_group(member_to_kick,thread_id=thread_id)
- else:
- await self.send(fbchat.Message("To komenda dla grup"), thread_id=thread_id, thread_type=thread_type)
- elif "!nick" in message_object.text:
- try:
- await self.change_nickname(message_object.text[5:], user_id=author_id, thread_id=thread_id, thread_type=thread_type)
- except:
- await self.send(fbchat.Message("Linux nie moze odczytać polskiej litery, albo wpisałes za długi nick"), thread_id=thread_id, thread_type=thread_type)
- elif message_object.text == "!mute":
- x = ""
- if thread_type != fbchat.ThreadType.GROUP:
- await self.send(fbchat.Message("To komenda tylko dla grup"), thread_id=thread_id, thread_type=thread_type)
- x = "stop"
- if x == "stop":
- pass
- else:
- group = (await self.fetch_group_info(thread_id))[thread_id]
- group_admins = group.admins
- if author_id not in group_admins:
- await self.send(fbchat.Message("Tylko administartor grupy może używać tej funkcji"), thread_id=thread_id, thread_type=thread_type)
- else:
- if thread_id in mutelist:
- await self.send(fbchat.Message("Jestem zmutowany. Aby mnie odmutować napisz !unmute"), thread_id=thread_id, thread_type=thread_type)
- else:
- await self.send(fbchat.Message("Zostalem zmutowany. Aby mnie odmutowac napisz !unmute"), thread_id=thread_id, thread_type=thread_type)
- mutelist.append(thread_id)
- with open("mutelist.json", "w") as write_file:
- json.dump(mutelist, write_file)
- elif message_object.text == "!unmute":
- x = ""
- if thread_type != fbchat.ThreadType.GROUP:
- await self.send(fbchat.Message("To komenda tylko dla grup"), thread_id=thread_id, thread_type=thread_type)
- x = "stop"
- if x == "stop":
- pass
- else:
- group = (await self.fetch_group_info(thread_id))[thread_id]
- group_admins = group.admins
- if author_id not in group_admins:
- await self.send(fbchat.Message("Tylko administrator grupy może używać tej funkcji"), thread_id=thread_id, thread_type=thread_type)
- else:
- if thread_id in mutelist:
- await self.send(fbchat.Message("Nie jestem już zmutowany :)"), thread_id=thread_id, thread_type=thread_type)
- mutelist.remove(str(thread_id))
- with open("mutelist.json", "w") as write_file:
- json.dump(mutelist, write_file)
- else:
- await self.send(fbchat.Message("Nie jestem zmutowany"), thread_id=thread_id, thread_type=thread_type)
- elif "!losuj" in message_object.text:
- try:
- numbers = str(message_object.text).split()
- drawed_number = rd.randint(int(numbers[1]), int(numbers[2]))
- await self.send(fbchat.Message("Wylosowano: " + str(drawed_number)), thread_id=thread_id, thread_type=thread_type)
- except:
- await self.send(fbchat.Message("Losuje tylko pełne liczby"))
- elif message_object.text == "!everyone":
- if thread_id in mutelist:
- await self.send(fbchat.Message("Zły admin wyłączył te funkcję mutująć mnie :("),thread_id=thread_id, thread_type=thread_type)
- else:
- group_members = list((await self.fetch_thread_info(thread_id))[thread_id].participants)
- mentions = []
- for member in range(len(group_members)):
- mentions.append(fbchat.Mention(group_members[member], offset=0))
- await self.send(fbchat.Message("ELUWA ALL", mentions=mentions),thread_id=thread_id, thread_type=thread_type)
- mail = ""
- password = ""
- loop = asyncio.get_event_loop()
- async def start():
- bot = Bot(loop=loop)
- print("loggin in...")
- await bot.start(email=mail, password=password)
- print("listening...")
- await bot._listen_mqtt()
- print("listening stopped")
- loop.create_task(start())
- loop.run_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement