title: How to create a Wikipedia bot to add a new section to a talk page?
body: <p>We need to implement a bot which posts new sections on Wikipedia Talk pages.</p>
  
  <p>As a matter of efficiency, we prefer to use python HTTP POST requests using MediaWiki API rather than available MediaWiki libraries.</p>
  
  <p>We have not requested for an approval for the bot, and we are just trying to implement a trial version to test the bot on our own Talk pages.</p>
  
  <p>For this purpose, I went through the following steps:</p>
  
  <p>1- As discussed at <a href="https://en.wikipedia.org/wiki/Wikipedia:Creating_a_bot" rel="nofollow">https://en.wikipedia.org/wiki/Wikipedia:Creating_a_bot</a>:</p>
  
  <blockquote>
    <p>Create an account for your bot. Click here when logged in to create the account, linking it to yours. (If you do not create the bot account while logged in, it is likely to be blocked as a possible sockpuppet or unauthorised bot until you verify ownership)</p>
    
    <p>Create a user page for your bot. Your bot's edits must not be made under your own account. Your bot will need its own account with its own username and password.</p>
  </blockquote>
  
  <p>So, I logged in to my own Wikipedia account, and created a new account (for the bot).</p>
  
  <p>2- As discussed at "API:Login" page: (Sorry, because of having less than 10 reputation, I am not able to add more than 2 links)
  Logging in through the API requires two requests. For the first request, I wrote the following code in python:</p>
  
  <pre><code>def logInRequestToWikipedia():
  
      # Add required parameters to the request.
      request = { 'action' : 'login' }
      request['lgname'] = 'BotName'
      request['lgpassword'] = '*************'
  
      url = 'https://en.wikipedia.org/w/api.php'
  
      headers = { 'content-type' : 'application/x-www-form-urlencoded' }
  
      r = requests.post(url, data = json.dumps(request), headers=headers)
  </code></pre>
  
  <p>The response starts with an error as follows:</p>
  
  <pre><code>&lt;error code="help" info="" xml:space="preserve"&gt;
  </code></pre>
  
  <p>And continues with the API documentation.</p>
  
  <p>3- As discussed at "API:Edit_-_Create%26Edit_pages" page:</p>
  
  <blockquote>
    <p>Note: In this example, all parameters are passed in a GET request just for the sake of simplicity. However, action=edit requires POST requests; GET requests will cause an error. Do not forget to set the Content-Type header of your request to application/x-www-form-urlencoded. The token that you received is terminated with +\, this needs to be urlencoded (so it will end with %2B%5C) before it is passed back.</p>
  </blockquote>
  
  <p>I added each of the following parameters separately and both together in the request data and tried all three cases, but it returns the same response.</p>
  
  <pre><code>request['lgtoken'] = '%2B%5C'
  request['Content-Type'] = 'application/x-www-form-urlencoded'
  </code></pre>
  
  <p>4- Also I tried each of the followings in my request data, but it returns the same response:</p>
  
  <pre><code>request['format'] = 'json'
  request['format'] = 'xml'
  </code></pre>
  
  <p>5- Moreover I found the following instruction at "User-Agent_policy" page:</p>
  
  <blockquote>
    <p>User agents (browsers or scripts) that do not send a User-Agent header may now encounter an error message like this:</p>
    
    <p>Scripts should use an informative User-Agent string with contact information, or they may be IP-blocked without notice.
    User agents that send a User-Agent header that is blacklisted (for example, any User-Agent string that begins with "lwp", whether it is informative or not) may encounter a less helpful error message (lie) like this:</p>
    
    <p>Our servers are currently experiencing a technical problem. This is probably temporary and should be fixed soon. Please try again in a few minutes.</p>
    
    <p>This change is most likely to affect scripts (bots) accessing Wikimedia websites such as Wikipedia automatically, via api.php or otherwise, and command line programs.[3] If you run a bot, please send a User-Agent header identifying the bot and supplying some way of contacting you, e.g.:</p>
    
    <p>User-Agent: MyCoolTool/1.1 (<a href="http://example.com/MyCoolTool/" rel="nofollow">http://example.com/MyCoolTool/</a>; MyCoolTool@example.com) BasedOnSuperLib/1.4</p>
    
    <p>Do not copy a browser's user agent for your bot, as bot-like behavior with a browser's user agent will be assumed malicious.[4] For more information, please refer to the MediaWiki API Documentation</p>
  </blockquote>
  
  <p>That's why I also tried my script with the following parameter, but the error response did not change:</p>
  
  <pre><code>request['User-Agent'] = "MyCoolTool/1.1 (http://example.com/MyCoolTool/; MyCoolTool@example.com) BasedOnSuperLib/1.4"
  </code></pre>
  
  <p>Do you think the problem can be related to the fact that we have not requested for an approval for the bot yet? Because we are just trying to implement a trial version to test the bot on our own Talk pages, and apply for the approval after making sure everything will work.</p>