summaryrefslogtreecommitdiff
path: root/tgbot.py
diff options
context:
space:
mode:
Diffstat (limited to 'tgbot.py')
-rwxr-xr-xtgbot.py67
1 files changed, 56 insertions, 11 deletions
diff --git a/tgbot.py b/tgbot.py
index ad72f00..c8561cd 100755
--- a/tgbot.py
+++ b/tgbot.py
@@ -3,6 +3,8 @@
import os
import time, threading, schedule
import telebot
+import asyncio
+import telebot.async_telebot
from FortniteStatusWrapper import *
from FortniteStatusFormatter import *
from FortniteClient import *
@@ -13,30 +15,67 @@ from datetime import datetime
if "TELEBOT_BOT_TOKEN" not in os.environ:
raise AssertionError("Please configure TELEBOT_BOT_TOKEN as environment variables")
-bot = telebot.TeleBot(os.environ["TELEBOT_BOT_TOKEN"])
+#bot = telebot.TeleBot(os.environ["TELEBOT_BOT_TOKEN"])
+bot = telebot.async_telebot.AsyncTeleBot(os.environ["TELEBOT_BOT_TOKEN"])
userRepository = UserRepository('db.sqlite')
fortniteStatusWrapper = FortniteStatusWrapper()
fortniteClient = FortniteClient()
@bot.message_handler(commands = ['start'])
-def startCommand(message):
+async def startCommand(message):
userRepository.putUser(message.chat.id)
- bot.reply_to(message, "This chat successfully registered to receive Fortnite updates!")
+ await bot.reply_to(message, "This chat successfully registered to receive Fortnite updates!")
@bot.message_handler(commands = ['status'])
-def getStatus(message):
- bot.reply_to(
+async def getStatus(message):
+ await bot.reply_to(
message,
formatFortniteStatus(fortniteStatus.getStatus()),
parse_mode='MarkdownV2')
@bot.message_handler(commands = ['friends'])
-def getFriends(message):
- bot.reply_to(
+async def getFriends(message):
+ await bot.reply_to(
message,
formatFriends(fortniteClient.get_friends()),
parse_mode='MarkdownV2')
+@bot.message_handler(commands = ['find'])
+async def findUser(message):
+ arg = message.text.split()
+ if len(arg) > 1:
+ search_user_display_name = arg[1]
+ print('Searching users by name {}'.format(search_user_display_name))
+ users: typing.List[fortnitepy.User] = await fortniteClient.fetch_users_by_display_name(search_user_display_name)
+ for user in users:
+ stats = await user.fetch_br_stats()
+ await bot.reply_to(
+ message,
+ formatUser(user, stats),
+ parse_mode='MarkdownV2')
+ else:
+ await bot.reply_to(
+ message,
+ 'Usage: /find username',
+ parse_mode='MarkdownV2')
+
+@bot.message_handler(commands = ['add'])
+async def addUser(message):
+ arg = message.text.split()
+ if len(arg) > 1:
+ user_id = arg[1]
+ print('Adding user with ID as friend {}'.format(user_id))
+ await fortniteClient.add_friend(user_id)
+ await bot.reply_to(
+ message,
+ 'Send friend request successfully',
+ parse_mode='MarkdownV2')
+ else:
+ await bot.reply_to(
+ message,
+ 'Usage: /add username',
+ parse_mode='MarkdownV2')
+
class FortniteStatusObserver(Observer):
def update(self, fortniteStatus) -> None:
for user in userRepository.getAllUsers():
@@ -72,11 +111,17 @@ class FortnitePresenceObserver(PresenceObserver):
parse_mode='MarkdownV2'
)
+async def run_bot():
+ await bot.polling()
+
+async def run_client():
+ await fortniteClient.run()
+
if __name__ == '__main__':
fortniteStatusWrapper.attach(FortniteStatusObserver())
fortniteClient.attach(FortnitePresenceObserver())
- main_thread = threading.Thread(target=bot.infinity_polling, name='bot_infinity_polling', daemon=True)
- main_thread.start()
- #main_thread.join()
- fortniteClient.run() \ No newline at end of file
+ loop = asyncio.get_event_loop()
+ loop.create_task(bot.polling())
+ loop.create_task(fortniteClient.run())
+ loop.run_forever() \ No newline at end of file