Discord Bot:仅发布包含关键字的推文

时间:2018-08-06 22:10:25

标签: twitter discord winston discord.io

我正在尝试创建一个Discord机器人,该机器人将发布来自Twitter帐户的推文,该Twitter帐户会宣布游戏警报。它会发布几种类型的警报,但是我只想向不和谐事件发布一种警报。幸运的是,每条推文都以一个区别标签开始。

我希望机器人从本质上做到这一点:

current_tweet=get.tweet(Games_Alert_Twitter_Account);
if(current_tweet.substring(0,keyword_length) =='Keyword')
post(Keyword);
else
do_nothing();

我发现了很多资源,这些资源如何使用Webhooks或API从用户那里提取推文,这些都将为您提供帮助,但是我还没有找到任何有关如何在推文上升时推文的信息,请参见如果它以某个关键字开头或包含某个关键字,则进行发布,否则进行发布。

总而言之,我需要这个:

1)检查Twitter(游戏公司)上的特定用户是否发布了帖子

2)将该帖子的内容放入表格中,我可以使用该表格查看邮件中是否包含某些关键字

3)根据步骤2中的关键字将消息发布到不和谐频道(我已经可以做这部分了)

有人知道支持该功能的API吗?我对不和谐和Twitter API相当陌生,并且对我的编码技能也很生疏,因此任何建议都将不胜感激。谢谢!

(编辑):我添加了摘要,并更改了所需的最后一步

2 个答案:

答案 0 :(得分:0)

从您已发布的代码摘录的外观来看,他们似乎想起了Python。 Tweepy的Stream API具有您所需要的。它允许您创建一个流,该流将为您提供来自指定帐户的不间断推文。这样您就可以做任何您想做的事。

来自Tweepy文档

  

在Tweepy中,tweepy.Stream的一个实例建立一个流会话并将消息路由到StreamListener实例。流侦听器的on_data方法接收所有消息,并根据消息类型调用函数。默认的StreamListener可以对大多数常见的twitter消息进行分类,并将它们路由到适当命名的方法,但是这些方法只是存根。

http://tweepy.readthedocs.io/en/v3.5.0/streaming_how_to.html

答案 1 :(得分:0)

@SRyan API不会为您做某些事情,您将负责处理它们。 我比较精通PHP,因此将使用PHP提供示例代码。

据我了解,您需要statuss / users_timeline端点。 https://developer.twitter.com/en/docs/tweets/timelines/api-reference/get-statuses-user_timeline.html

它返回由screen_name或user_id参数指示的用户发布的最新Tweet的集合。您可以每x分钟轮询一次电话。您每15分钟可以提出900个请求。要获得用户发布的最新推文,这是大量的呼叫。

请确保过滤掉转发。 每条推文将包含一组标签,user_mentions,符号,URL。您可以创建自己的BOW(单词袋)以测试可用的#标签并相应地进行操作。

我正在使用亚伯拉罕的Twitter OAuth PHP库。 下面的代码未经测试,我只是输入了它。

$params = [
    //users screen name whose tweets you want to fetch
    'screen_name' => '@sheikhazad', 
    //number of tweets to return, max 200 per request
    'count'=>100,
    //fetch tweets later than a given id, set this to blank if you want to fetch from the oldest. only the last 3200 are available
    'since_id'=> '',
    //exclude replies if you want
    'exclude_replies' => 1 
];
$statuses = $connection->get('statuses/user_timeline', $params);
$array_of_hashtags_to_match = []; //fill with key hashtags
$array_of_words = []; //fill with key words
//to keep track of latest id fetched for future calls
$max_id = $params['since_id']; 

foreach($statuses->statuses as $status){
    //skip retweets
    if($status->retweeted_status && $status->retweeted_status->text != '')
        continue;
    //skip tweets that do not not begin with a hashtag, remove if not needed
    else if(!$status->entities || !$status->entities->hashtags)
        continue;
    //skip tweets that do not have hashtags
    else if(!$status->entities || !$status->entities->hashtags)
        continue;

    //if you want to match hashtags
    foreach($status->entities->hashtags as $htag){
        //check if the hashtag
        if(in_array(strtolower($htag, $array_of_hashtags_to_match))){
             //post to discord as needed
        }
    }

    //tokenize the tweet text, i.e., $status->text
    $tokens = array_unique(explode(' ', strtolower($status->text)));
    //either use array intersection to find if there are matching keywords
    //or use a loop to iterate through each keyword, check if present
    //post to discord as needed
}