VK auto-posting bot

NOTE: Article created only for educational purposes, I do not encourage you to use the product obtained at the end of the lesson, to bring inconvenience or own benefit


What do we do



Bot for auto-posting posts on the community wall or Vk page



What for



For informational purposes



What do we need





Let's start



To work, we need a token with wall and offline permissions. To receive a token, create your Vk Standalone application . And save its ID.



Then follow the link:

oauth.vk.com/authorize?client_id=IDAPP&scope=wall , offline & redirect_uri = http: //api.vk.com/blank.html&response_type=token

And substitute your application ID instead of IDAPP. Or use the link that I have prepared especially for you.



If everything is done correctly, you will be transferred to another site, and in the URL page in the GET parameter access_token there will be a token that we need, save it.



XML work



To store the settings, we will create an .xml file with the following content:



<settings>
  <token>token</token>
  <textPost>Text post</textPost>
  <interval>120</interval>
  
  <post>
    <attachments>
      <attachment>attachment</attachment>
    </attachments>
    <copyright>copyright</copyright>
    <v>5.122</v>
  </post>
  
  <groups>
    <group>short name group</group>
  </groups>
</settings>


Replace:



  • "Token" for the token that we received above
  • "Text post" for the message that should be in the post
  • "Attachment" to the object to be attached to the post
  • "Copyright" to the source link
  • "Short name group" to the short name (without vk.com ) the community / user page where the publication will take place (the wall must be open for publication)


Let's start writing code



We import all the necessary libraries, create an instance of our module and log in to VKontakte on behalf of the user through a token.



import vk_api
import time

from modules import XML as moduleXml

XML = moduleXml.XML("settings")
VK = vk_api.VkApi(token=XML.parsingFile("token"))


Next, we get all the short addresses where the posts will be published.



import vk_api
import time

from modules import XML as moduleXml

XML = moduleXml.XML("settings")
VK = vk_api.VkApi(token=XML.parsingFile("token"))

groupsId = []
groupsShortName = ""
for child in XML.parsingFile("groups", False):
    groupsShortName += child.text + ","

for group in VK.method("groups.getById", {"group_ids": groupsShortName}):
    groupsId.append(group["id"] * -1)

del groupsShortName


Now we get the message that will be in the post, the interval with which the posts will be published and the post source.



import vk_api
import time

from modules import XML as moduleXml

XML = moduleXml.XML("settings")
VK = vk_api.VkApi(token=XML.parsingFile("token"))

groupsId = []
groupsShortName = ""
for child in XML.parsingFile("groups", False):
    groupsShortName += child.text + ","

for group in VK.method("groups.getById", {"group_ids": groupsShortName}):
    groupsId.append(group["id"] * -1)

del groupsShortName

textPost = XML.parsingFile("textPost")
intervalPost = int(XML.parsingFile("interval"))


Now we get all the objects that will be attached to the post.



import vk_api
import time

from modules import XML as moduleXml

XML = moduleXml.XML("settings")
VK = vk_api.VkApi(token=XML.parsingFile("token"))

groupsId = []
groupsShortName = ""
for child in XML.parsingFile("groups", False):
    groupsShortName += child.text + ","

for group in VK.method("groups.getById", {"group_ids": groupsShortName}):
    groupsId.append(group["id"] * -1)

del groupsShortName

textPost = XML.parsingFile("textPost")
intervalPost = int(XML.parsingFile("interval"))

attachments = [attachment.text for attachment in XML.parsingFile("attachments", False)]
copyright = XML.parsingFile("copyright")
v = XML.parsingFile("v")


We already have all the data we need to publish. All that's left is to create a function for publishing and a loop that will call the function for publishing.



First, let's loop and leave the function empty. The same code will only work if it is run from the console.



import vk_api
import time

from modules import XML as moduleXml

XML = moduleXml.XML("settings")
VK = vk_api.VkApi(token=XML.parsingFile("token"))

groupsId = []
groupsShortName = ""
for child in XML.parsingFile("groups", False):
    groupsShortName += child.text + ","

for group in VK.method("groups.getById", {"group_ids": groupsShortName}):
    groupsId.append(group["id"] * -1)

del groupsShortName

textPost = XML.parsingFile("textPost")
intervalPost = int(XML.parsingFile("interval"))

attachments = [attachment.text for attachment in XML.parsingFile("attachments", False)]
copyright = XML.parsingFile("copyright")
v = XML.parsingFile("v")

done = False

def publicPosts():
    pass

if __name__ == "__main__":
    done = True

while done:
    publicPosts()
    time.sleep(intervalPost)


To publish a post, we will call the "wall.post" API method and pass the get parameters earlier. If everything works out correctly, a corresponding message will be displayed in the console.



import vk_api
import time

from modules import XML as moduleXml

XML = moduleXml.XML("settings")
VK = vk_api.VkApi(token=XML.parsingFile("token"))

groupsId = []
groupsShortName = ""
for child in XML.parsingFile("groups", False):
    groupsShortName += child.text + ","

for group in VK.method("groups.getById", {"group_ids": groupsShortName}):
    groupsId.append(group["id"] * -1)

del groupsShortName

textPost = XML.parsingFile("textPost")
intervalPost = int(XML.parsingFile("interval"))

attachments = [attachment.text for attachment in XML.parsingFile("attachments", False)]
copyright = XML.parsingFile("copyright")
v = XML.parsingFile("v")

done = False

def publicPosts():
    for groupId in groupsId:
        for i in range(1, 5):
            result = VK.method("wall.post", {
                "owner_id": groupId,
                "message": textPost,
                "attachments": attachments,
                "copyright": copyright,
                "v": v
            })
            if result["post_id"]:
                print("Good post, id post - " + str(result["post_id"]))
            else:
                print("Error posting")

if __name__ == "__main__":
    done = True

while done:
    publicPosts()
    time.sleep(intervalPost)


Conclusion



That's it, all the code is ready. I will tell you right away that I am 13 years old. And I want to tell and share what I can do and find interesting for others. So my last publication was read by 2k + people, and 40 people were bookmarked, although there is something to improve. This motivated me, thank you very much.



Project on gitHub .



NOTE: Article created only for educational purposes, I do not encourage you to use the product obtained at the end of the lesson, to bring inconvenience or own benefit



All Articles