I try to create a response bot, to twits that contain a keyword or hashtag on twitter with tweepy and python

Viewed 12

I try to create a bot that responds to tweets that contain a specific keyword or my brand hashtag, i want to make this in real time, for that reason i use filtered stream.

The retweet bot works perfectly I have tried to use the retweet bot as a base to make the response bot and it does not run

the code is a bit long because I defined the filtered stream rules

I want my bot to get the data in real time and respond with a short welcome phrase, I have found references of bots that respond to a mention on twitter but this is not the case, the objective of the bot is to respond to a tweet that contains a hashtag specific

import requests
import os
import json
from os import access
from re import search
import tweepy
import time

api_key = ''
api_secret = ''
bearer_token = ''
access_token = ''
access_token_secret = ''

client = tweepy.Client(bearer_token, api_key, api_secret, access_token, access_token_secret)

auth = tweepy.OAuth1UserHandler(api_key, api_secret, access_token, access_token_secret)
api = tweepy.API(auth)

class MyStream(tweepy.StreamingClient):
    def on_tweet(self, tweet):
        print(tweet.text)
        try:
            client.retweet(tweet.id)

        except Exception as error:
            print(error)

        time.sleep(20)

def bearer_oauth(r):

    r.headers["Authorization"] = f"Bearer {bearer_token}"
    r.headers["User-Agent"] = "v2FilteredStreamPython"
    return r


def get_rules():
    response = requests.get(
        "https://api.twitter.com/2/tweets/search/stream/rules", auth=bearer_oauth
    )
    if response.status_code != 200:
        raise Exception(
            "Cannot get rules (HTTP {}): {}".format(response.status_code, response.text)
        )
    print(json.dumps(response.json()))
    return response.json()


def delete_all_rules(rules):
    if rules is None or "data" not in rules:
        return None

    ids = list(map(lambda rule: rule["id"], rules["data"]))
    payload = {"delete": {"ids": ids}}
    response = requests.post(
        "https://api.twitter.com/2/tweets/search/stream/rules",
        auth=bearer_oauth,
        json=payload
    )
    if response.status_code != 200:
        raise Exception(
            "Cannot delete rules (HTTP {}): {}".format(
                response.status_code, response.text
            )
        )
    print(json.dumps(response.json()))


def set_rules(delete):
    sample_rules = [
        {"value": "ultimatebrand has:images -is:retweet", "tag": "nft pictures"},
    ]
    payload = {"add": sample_rules}
    response = requests.post(
        "https://api.twitter.com/2/tweets/search/stream/rules",
        auth=bearer_oauth,
        json=payload,
    )
    if response.status_code != 201:
        raise Exception(
            "Cannot add rules (HTTP {}): {}".format(response.status_code, response.text)
        )
    print(json.dumps(response.json()))

def main():
    rules = get_rules()
    delete = delete_all_rules(rules)
    set = set_rules(delete)


if __name__ == "__main__":
    main()


stream = MyStream(bearer_token=bearer_token)

stream.filter()```

This is my retweet bot for specific words, what do I need to change so that it makes a reply to the tweet? thanks

0 Answers
Related