0

Okay so I know that there is a way to open let's say a wiki. By typing a command(archive) it will then search the wiki and return with a message containing certain information about what you searched for.

I know that you can use webhooks for this, but I don't know how. Can anyone of you help?

  • I think "archive" is a wrong word for this, you're looking for a way to *search* the wiki. Since this is a Discord bot, and the action is performed in response to an interaction, you don't need a webhook for this. Start by writing a bot that responds to the command you want, and then take a look at your wiki's API to see how to find the information you want -- for example, Wikipedia is using the [MediaWiki API](https://www.mediawiki.org/wiki/API:Main_page), so you should take a look there. – Danya02 Aug 13 '21 at 05:35
  • Thank you. I will try and check that out. The reason why I call the command 'Archive' is because it search the archive for information. Btw it's meant to search the Star wars fandom page called Wookieepedia. – Commander Purple Aug 14 '21 at 15:51
  • It's still not a particularly good idea to call it that, it'll cause confusion. Most commands express a verb -- `save`, `play`, `delete` -- so if you name your command `archive` it sounds like you want [to archive](https://en.wiktionary.org/wiki/archive#Verb) something. As for the wiki, it does indeed use the MediaWiki API, the endpoint is at https://starwars.fandom.com/api.php. – Danya02 Aug 14 '21 at 21:43
  • I know that, but the problem is that the API is made for discord.js, and I make my bot in discord.py. If you have a solution for this, feel free to add me on discord(Commander Purple#7470) then we can discuss it in there instead – Commander Purple Aug 19 '21 at 18:20
  • It's best to keep all discussion related to the question on the site. I've created [a chat room so you can ask further questions](https://chat.stackoverflow.com/rooms/236211/chat-for-question-68640956), but if they're too different you should consider asking a separate question on the main site. – Danya02 Aug 19 '21 at 19:52
  • Sorry i can't chat in there... i don't have enough "reputation" or something... – Commander Purple Aug 20 '21 at 15:16

1 Answers1

0

There are multiple ways to get a summary of a page from a wiki. The easiest of them is probably the TextExtracts extension that allows you to get the plain text of some article. However, this only works if the wiki in question has that extension installed -- the Star Wars Fandom wiki that you wanted to work with does not. Nevertheless, this should give you an idea of how to use the MediaWiki API to get information about articles from there.

import discord
from discord.ext import commands
import aiohttp

bot = commands.Bot(command_prefix='-')

WIKI_ENDPOINT = 'https://en.wikipedia.org/w/api.php'

NUMBER_OF_SENTENCES = 3  # (must be between 1 and 10)

@bot.command('lookup')
async def lookup_wiki(ctx, *query_elements):
    query = ' '.join(query_elements)
    params = {
        "action": "query",
        "prop": "extracts",
        "titles": query,
        "exsentences": NUMBER_OF_SENTENCES,
        "explaintext": "true",
        "format": "json",
        "redirects": "true"
    }
    headers = {"User-Agent": "BOT-NAME_CHANGE-ME/1.0"}

    async with aiohttp.ClientSession() as session:
        async with session.get(WIKI_ENDPOINT, params=params, headers=headers) as r:
            data = await r.json()
            pages = data['query']['pages']
            page = pages[ list(pages)[0] ]
            try:
                extract = page['extract']
                if extract is None: raise ValueError
            except:
                extract = f"We could not fetch an extract for this page. Perhaps it does not exist, or the wiki queried does not support the **TextExtracts** MediaWiki extension: https://www.mediawiki.org/wiki/Extension:TextExtracts\nThe page data received is: `{page}`"
            await ctx.send(extract)

bot.run('your bot token here')

Test run of bot. -lookup StackOverflow and -lookup MediaWiki are sent by the user, and the bot responds with the first 3 sentences of corresponding Wikipedia articles.

Danya02
  • 1,006
  • 1
  • 9
  • 24
  • Thank you! When I get time I'll check if it works. But thank you. – Commander Purple Aug 29 '21 at 17:28
  • I'm so sorry to ask, but is there even a way to make the thing I'm asking about? If you look up the bot C-3PO, it has the command called archive. That's what I'm trying to make – Commander Purple Aug 29 '21 at 17:29
  • @CommanderPurple It's absolutely possible, both because somebody else has already done it, and because you can see the page in your browser, and whatever your browser can do, you can do in code. In particular, when you're looking at a web page, it downloads it in a format called HTML. The act of downloading the HTML, running it through a parser, and getting data from it, is called "web scraping", and the most popular library to do it on static websites is "BeautifulSoup". [Here's a tutorial on using it.](https://www.dataquest.io/blog/web-scraping-python-using-beautiful-soup/) – Danya02 Aug 31 '21 at 04:45
  • Cool I'll try and look that up when I get time. Thank you for trying to help me @Danya02 – Commander Purple Sep 01 '21 at 18:09