Golang — Twitter @ShareAsPic app using Chromedp

Ahmed Ashraf
Nov 6 · 3 min read

This blog is a translation of its Arabic version on My website.

As a Facebook user when I see any tech tweet on twitter I share it with my FB community. so I thought having a twitter account that takes a screenshot from a tweet can be a nice idea.

# Stack

- Go-lang: you can use any lang as chromedp supporting several languages but I prefer Go to get a strong knowledge.

- Go-lang Twitter API: to receive tweets through `StreamAPI` and post tweets through `Twitter API`

- Chromedp: Chrome Debugging Protocol. it allows you to use the chrome browser and interact with it to simulate real user behavior with code. also it supports headless mode -no GUI- so it can run on CI/CD pipelines to do e2e tests or any automation you need like scrapping websites and other cool stuff.

- Redis: an InMemory database we will use to store processed tweets so we don’t respond to the same tweet more than once

# Source Code

https://github.com/ahmedash95/shareaspic

# Scenario would be like:

- create a twitter account @shareAsPic
- people can mention him in a reply to any tweet with a message like @shareAsPic share this please
- use twitter stream API to listen for all these tweets
- use Chromedp to open that tweet in the browser and take a screenshot in — a -headless- mode
reply to the mention with the screenshot

# Application flow

Application flow

1 — Users always post tweets.

2 — These tweets land on Twitter’s servers which it has a lot of systems. each system has it’s own mission with that tweet (notifications, timeline, streaming API, etc..)

3 — We will use the StreamAPI to listen only to replies contain `@ShareAsPic` *our twitter app* name to process it

the below example shows how to prepare the stream params to listen only for our app’s mentions

4 — At this step our app starts to receive tweets data twitter sending to us through StreamAPI

from the above `Struct` we send `InReplyToScreenName` and `InReplyToStatusID` to chromedp to open tweet URL on twitter and take the screenshot

5 — here we realize the power of headless browsers as `chromedp` simulates real user behavior on a real browser and makes interactions.
for us, it opens `https://twitter.com/{UserName}/status/{TweetID}` and using one of the chromedp functions takes a screenshot of that tweet using element selector that contains tweet body

6 — then we reply to the user with the screenshot attached

and the result of the whole flow is like

# Conclusions

The app itself has no goal but it was just a simple idea to implement and see how can we automate browser interactions with Chromedp. it adds huge value for E2E tests especially for SPA. or scrapping websites content.

If you have any comments or ideas to share about chromedp. I’m more than interested to discuss and learn.

Ahmed Ashraf

Written by

FullStack Developer, Laravel Artisan, OpenSource Contributor

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade