Attach Community Report | Steem Discord Bot Python #9

in Steem Devlast year

Welcome back, developers.

Greetings to all Steem enthusiasts! Today, we will learn how to format our generated report and write it into a .txt file and how to attach this file via Discord bot.

lectur9.png

Summary

We established the necessary environment for developing a Python-based Discord bot during our first lecture. In our second lecture we create a discord bot token, configure the bot and generate the invite link. We used the invite link and added the bot to our server. In our third lecture, we will bring the Community-Bot online in our local environment.

In the 4th lecture, we successfully developed our initial bot command, !info. We also learned how to create functions and retrieve data using the requests library in 5th lecture. We implemented the !info command in 6th lecture which is used fetch the information of any steem user.

In the 7th lecture, we learned how to send an embedded message with columns, images, and a profile image. In lecture 8th we learn how to generate a report for the community. In today's lecture, we will process that report and write the response intro in a .txt file.

Procedure
  • First, we need to format our report according to our requirements. In this report, I want to display usernames, posts, comments, and unique comments in separate columns. Let's utilize markdown styling to generate the report.
 async def get_community_report(self, community):
        api = self.utils.sds_base + f"/feeds_api/getActiveCommunityReport/{community}"
        response = requests.get(api).json()
        community_report = map_sds_response(response)
        formatted_report = ''
        for item in community_report:
            formatted_report = (
                                   f'| {item.get("author")} | {item.get("total_post_count")} | {item.get("total_comment_count")} | '
                                   f'{item.get("unique_comment_count")} |\n') + formatted_report

        return '|Username|Posts|Comments|Unique Comments|\n|-|-|-|-|\n' + formatted_report

image.png

  • We have obtained the formatted community report, which can also be customized. Now, our task is to write the report into a text file. To do this, we must create an empty directory within our project. We need to specify the path and use Python code to create the directory.

  • Add a path variable in utils.py file.

self.folder_path = 'community-bot/files/'
  • Now add and use the create directory code in main.py.
def create_directory(path):
    # Check whether the specified path exists or not
    is_exist = os.path.exists(path)

    if not is_exist:
        # Create a new directory because it does not exist
        os.makedirs(path)


# Commit
create_directory(utils.folder_path)
  • To write to a text file, we need to first create the file using this code. The function requires two parameters: the file name and the content we wish to write to the file.
def create_file(file_name, content):
    f = open(utils.folder_path + file_name, "w")
    f.write(content)
    f.close()
  • Now we can get community reports in the defined format and create and write the file. Here how can we do this?
 if command == utils.commands[1]:
            if len(props) >= 2:
                await message.channel.typing()
                community = str(props[1]).lower().replace('@', '')
                community_report = await steemfun.get_community_report(community)
                create_file(f"{community}-report.txt", community_report)
                await message.reply(f'**{community} Report (7-Days):**',
                                file=discord.File(utils.folder_path + f"{community}-report.txt"))

image.png

  • Here is the final result

image.png

Github
Steem Discord Bot Series
SteemPro Official

Cc: @blacks
Cc: @rme
Cc: @hungry-griffin
Cc: @steemchiller
Cc: @steemcurator01
Cc: @pennsif
Cc: @future.witness
Cc: @stephenkendal
Cc: @justyy


Best Regards @faisalamin

Coin Marketplace

STEEM 0.18
TRX 0.16
JST 0.030
BTC 66445.86
ETH 2588.21
USDT 1.00
SBD 2.68