Help:Creating a bot: Difference between revisions

Content deleted Content added
m Reverted edit by 12.138.73.146 (talk) to last version by --WikiUser1234945--
Logging in: de-emphasize re-inventing the wheel
 
(22 intermediate revisions by 16 users not shown)
Line 42:
* [[Special:Export]] can be used to obtain bulk export of page content in XML form. See [[mw:Manual:Parameters to Special:Export|Manual:Parameters to Special:Export]] for arguments;
*: '''Status:''' Built-in feature of MediaWiki, available on all Wikimedia servers.
* Raw (Wikitext) page processing: sending aan <code>action=raw</code> or aan <code>action=raw&templates=expand</code> GET request to index.php will give the unprocessed wikitext source code of a page. For example: <code>https://en.wikipedia.org/w/index.php?title=Help:Creating_a_bot&action=raw</code>. An API query with <code>action=query&prop=revisions&rvprop=content</code> or <code>action=query&prop=revisions&rvprop=content&rvexpandtemplates=1</code> is roughly equivalent, and allows for retrieving additional information.
*:'''Status:''' Built-in feature of MediaWiki, available on all Wikimedia servers.
 
Line 48:
 
===Logging in===
Approved bots need to be logged in to make edits. Although a bot can make read requests without logging in, bots that have completed testing should log in for all activities. Bots logged in from an account with the bot flag can obtain more results per query from the MediaWiki API (api.php). Most bot frameworks should handle login and cookies automatically, but if you are not using an existing framework, you will need to follow these steps.
 
Use of a bot framework is recommended as they handle login and cookies. Common frameworks include [[mw:Manual:Pywikibot|pywikibot]] for Python and [[mw:Manual:Mwn|mwn]] for Node.js. The manual steps below can be followed if you are implementing your own framework.
For security, login data must be passed using the [[HTTP POST]] method. Because parameters of [[HTTP GET]] requests are easily visible in URL, logins via GET are disabled.
 
To log a bot in using the [[mw:API|MediaWiki API]], two requests are needed:
Line 93:
Edit conflicts occur when multiple, overlapping edit attempts are made on the same page. Almost every bot ''will'' eventually get caught in an edit conflict of one sort or another, and should include some mechanism to test for and accommodate these issues.
 
Bots that use the Mediawiki API (api.php) should retrieve the edit token, along with the <code>starttimestamp</code> and the last revision "base" timestamp, ''before'' loading the page text in preparation for the edit; <code>prop=info|revisions</code> can be used to retrieve both the token and page contents in one query (<span class="plainlinks">[https://en.wikipedia.org/w/api.php?action=query&titles=Wikipedia:Sandbox&prop=info|revisions&inprop=&intoken=edit&rvprop=timestamp|content example]</span>). When submitting the edit, set the <code>starttimestamp</code> and <code>basetimestamp</code> attributes, and check the server responses for indications of errors. For more details, see [[mw:API:Edit|MediaWiki:API:Edit - Create& and Edit pages]].
 
Generally speaking, if an edit fails to complete the bot should check the page again before trying to make a new edit, to make sure the edit is still appropriate. Further, if a bot rechecks a page to resubmit a change, it should be careful to avoid any behavior that could lead to an infinite loop and any behavior that could even resemble [[WP:EW|edit warring]].
Line 103:
====Idea====
* The first task in creating a Wikipedia bot is extracting the requirements or coming up with an idea. If you don't have an idea of what to write a bot for, you could pick up ideas at [[Wikipedia:Bot requests|requests for work to be done by a bot]].
* Make sure an existing bot isn't already doing what you think your bot should do. To see what tasks are already being performed by a bot, see [[Wikipedia:Bots/StatusBot activity monitor|the list of currently operating bots]].
 
====Specification====
Line 113:
 
====Software architecture====
* Think about '''how''' you might create ita bot, and which programming language(s) and tools you would use. [[Software architecture|Architecture]] is concerned with making sure the software system will meet the requirements of the product as well as ensuring that future requirements can be addressed. Certain programming languages are better suited to some tasks than others, for more details see {{section link||Programming languages and libraries}}.
 
====Implementation====
Line 162:
 
====Bot Flag====
A bot's edits will be visible at [[Special:RecentChanges]], unless the edits are set to indicate a bot. Once the bot has been approved and given its bot flag permission, one can add the "bot-=True" to the API call - see [[mw:API:Edit#Parameters]] in order to hide the bot's edits in [[Special:RecentChanges]]. In Python, using either mwclient or wikitools, then adding '''{{Green|<nowiki>Bot1=bot=True</nowiki>}}''' to the edit/save command will set the edit as a bot edit - e.g. {{code|1=PageObject.edit(text=pagetext, bot=True, summary=pagesummary)}}.
 
====Monitoring the bot status====
Line 215:
==== MATLAB ====
 
* [https://github.com/JRCSTU/MatWiki MatWiki]&nbsp;– a preliminary (as of Feb 2019) MATLAB R2016b(9.1.x) client supporting just bot-logins &and semantic #ask queries.
 
====Microsoft .NET====
Line 261:
 
==== Lua ====
* During the Lua Annual Workshop 2016, {{U|Jim Carter}} and {{U|Dfavro}} started developing Lua's bot framework for Wikimedia projects. Please contact Jim Carter on their talk page to discuss about the development.
* [https://github.com/AlexanderMisel/mwtest mwtest] is an example using Lua to write a wikibot, created by [[User:Alexander Misel]], with simple API.