473,287 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,287 developers and data experts.

Slack Automation Sample - Scraping contact list from one channel

Background
Recently, I to monitor contact info of some slack channels, but I don't have API to invoke. So I need use UI Automation to finish this task.
I found this library recently Clicknium, besides support web automation like selenium, it also supports desktop automation, that is useful for me.
You can follow the getting started of this Visual Studio Code extension to setup environment, less than 2 minutes for me.

Slack client automation - Scraping the contact list
  • Auto select group and channel.
based on group name, auto click on the left navigation bar:



I use Clicknium Recorder to record the UI element on slack, and update it with parameter {{group}} as the following, for parametric locator, please refer to Clicknium Document



I set the title of the windows to 'Slack*', as clicknium support wild char to match, it can improve the automation robust.
And I add the tabitem name to parameter 'group', then I can sepecify group in running stage.

Expand|Select|Wrap|Line Numbers
  1. from clicknium import clicknium as cc, locator, ui
  2.  
  3. ui(locator.slack.tabitem_group, {'group':group}).click()
  • In similar way to auto click channel.
Expand|Select|Wrap|Line Numbers
  1. if not cc.is_existing(locator.slack.treeitem_channel, params):
  2.         ui(locator.slack.treeitem_parent).click()
  3.     ui(locator.slack.treeitem_channel, params).click()
As sometimes channel list is collapsed, so I check the target channel is existing or not, if not, need click the Channels to elapse the channel list first.


  • get the contact count by get the text of the info at the right-top, then parse the text.



Expand|Select|Wrap|Line Numbers
  1. text = ui(locator.slack.text_all, params).get_text()
  2. index = text.find("members")
  3. count = int("".join(re.findall("\d+",text[0:index])))
  • Show the contact list dialog by clicking the info at the right-top.


  • as the contact list is dynamic loaded, so we scrape almost 10 items, need page down the scroll bar to load new contacts

Expand|Select|Wrap|Line Numbers
  1. for i in range(1,13):
  2.     dict = {"index":i}
  3.     if not cc.is_existing(locator.slack.listitem_member, dict):
  4.         continue
  5.     elem_member = ui(locator.slack.listitem_member, dict)
  6.     name = elem_member.get_text()
  7.     if NotContains(names, name):
  8.         names.append({'name':name,'email':'', 'postfix':''})
  9.         step += 1
  10.         if step % 100 == 0:
  11.             SaveToFile(names)
  12.  
  13. ## move mouse down to trigger new data loaded for member list
  14. ui(locator.slack.edit_membername).click(by='mouse-emulation')
  15. for i in range(10):
  16.     cc.send_hotkey("{DOWN}")

I click the find members edit box and then send the hotkey 'Down' for ten times, it will trigger to load new contacts.
To scrap each member name, I use the following parametric locator:




Slack client automation - Scraping the contact detail
In above section, I show how to Scraping contact list from a channel, in this section, I show how to Scraping each contact detail information, such as email address. (If the channel don't show the email address, you can skip this section).
  • Same as the above section to auto select group and channel.
  • Show the contact list dialog by clicking the info at the right-top.
  • Input the contact name to search the contact

Expand|Select|Wrap|Line Numbers
  1. ui(locator.slack.text_all, params).click()
  2. ui(locator.slack.edit_membername).set_text(item['name'], "set-text")
  • If matched, click to show profile of the contact, and then get the text of email

To click the search result, I use the following locator,
set the name with parameter 'name' and uncheck listitem layer.


  • Error handling in this section:
- If search failed, need close the dialog and iterate to next contact.
- If cannot get the email info, need close the profile tab and iterate to next contact.

Expand|Select|Wrap|Line Numbers
  1. member1 = cc.wait_appear(locator.slack.listitem_member1, {'name':item['name']})
  2. if member1 != None:
  3.     member1.click()
  4. else:
  5.     print("failed to search the member")
  6.     item['email'] = "ignore"
  7.     item['postfix'] = ""
  8.     ui(locator.slack.image).click()
  9.     continue
  10.  
  11. try:
  12.     email = ui(locator.slack.text_email).get_text(timeout=5)
  13.     item['email'] = email
  14.     item['postfix'] = email.split('@')[1]
  15. except:
  16.     print("failed to find the email")
  17.     item['email'] = "ignore"
  18.     item['postfix'] = ""
  19.     if cc.is_existing(locator.slack.image):
  20.         ui(locator.slack.image).click()
  21.         continue
Open Source Code
I put the code in my github, if you need view full code, please refer to github.
Jul 26 '22 #1
0 8845

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Brian Henry | last post by:
I want to pull my contact list (personal one) from the exchange server into a local text file. I know how to write stuff into a text file, just getting it from an exchange server is my problem....
1
by: Robert J. Bonn | last post by:
I'm trying to set up a contact list in MS Access 97. I've looked through a reference book and the program's help screens, but the light bulb isn't quite coming on for me. If one of you could take...
1
by: rjbonn | last post by:
I'm setting up a contact list for a musician, who is about to release her first CD. She would like a contact list that can track the people she meets, who will be in various categories -- fans,...
0
by: Robert J. Bonn | last post by:
I am setting up a contact list for a musician, who is about to release her first CD. She would like a contact list that can track all the people she meets, who will be in various categories --...
2
by: swethak | last post by:
hi, i have to get the gmail contact list using jsp code. For that purpose i have to use the Google data Api <%@ page import="com.xdatasystem.contactsimporter.*" %> <% // automatically...
3
by: d3ph03n1x | last post by:
What I'm trying to do is create a Simple contact list, with a menu, which i would add contacts too, edit contacts, view them, and search them when there is a long list, maybe on the basis of contact...
0
by: clicknium | last post by:
Clicknium Automation Sample Solution - Customer Onboarding This is a sample of customer onboarding solution with clicknium web automation. For one enterprise, customer onboarding has a...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.