Azure Playground: functions

Building a serverless twitter bot

Azure Playground: functions

I recently had a talk about what it would take to formally move to devops within the 9-5. From that talk I was told that I need more Azure experience so I now have a goal of getting comfortable in Azure like I am in AWS. Anyway I made a twitter bot using an Azure function so I'm going to explain how to get this done and link the articles I used.

P.S. Before I go forward s/o to everyone that stopped by my random zoom to chat and help me when I got stuck.

Create and Azure account

This part took the longest for me but you get $200 credits for free for the first 30days so signup (please). Once this process is complete we are going to build a bot that will retweet food to your timeline every 5mins. No worries you can delete all this when your done to save your credit.

Function Setup

This service is called Function App and once we are in we are going to hit the add button and pick the following things:

Subsctription: *your subscription name*
Resource Group: demo (or app name)
Function App Name: foodie1
Publish: Code
Runtime stack: Python
copy of my screen setup like above

Then hit Review + Create and then Create

Get Twitter Credentials

I'm not really going to walk through this but signup for a twitter api key here. Once your in you want to create an app > enter all the details they request > details > keys and tokens > generate the access token and access token secret. Keep this page open and let's move back to Azure.

Adding config variables

Now that your resource is done building click Go to resource > Configuration. At this point you need to have two windows with your twitter keys from the last step and the Application settings screen on Azure open. Enter the following Key:Value pairs

CONSUMER_KEY:*your consumer key*
CONSUMER_SECRET:*your consumer secret*
ACCESS_TOKEN:*your access token*
ACCESS_TOKEN_SECRET:*your access token secret*

Code Time

Hit the name of your function from the top bar. Then hit the New function button. This should give you a quick start option. I suggest you go through this path for VS Code for the easiest way to complete this project. This isn't really about the code so please trust me that this next part works.

Once you are setup in VS Code replace the file with the following code:

import os
import tweepy

import azure.functions as func

def main(mytimer: func.TimerRequest) -> None:
    consumer_key = os.environ['CONSUMER_KEY']
    consumer_secret = os.environ['CONSUMER_SECRET']
    access_token = os.environ['ACCESS_TOKEN']
    access_secret = os.environ['ACCESS_TOKEN_SECRET']
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_secret)
    api = tweepy.API(auth, wait_on_rate_limit=True)

    #print to personal account foodie pics
    class MyStreamListener(tweepy.StreamListener):
        def on_status(self, status):
        def on_error(self, status_code):
            if status_code == 420:
                #returning False in on_data disconnects the stream
                return False

    myStreamListener = MyStreamListener()
    myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener)

There should also be a requirements.txt file that should look like this:


Go ahead and hit that upload button. After successful upload keep an eye on your account to see the food pics! Welcome to the serverless bot world. 😎