Skip to content

Instantly share code, notes, and snippets.

@mrhalix
Created June 12, 2018 07:04
Show Gist options
  • Save mrhalix/88ff8ddc2aec14cad33803019465dda2 to your computer and use it in GitHub Desktop.
Save mrhalix/88ff8ddc2aec14cad33803019465dda2 to your computer and use it in GitHub Desktop.
a bot which tweets what it receives. JUST change the "PUT HERE" parts with your own information and run the code!
#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests, telebot, json, sys
reload(sys)
sys.setdefaultencoding('utf8')
"""
#########################
JUST change the "PUT HERE" parts with your own information and run the code!
#########################
All rights reserved - Halix
"""
# ------ CONFIG ------
API_TOKEN = "PUT HERE"
def tweet(text):
headers = {
'origin': 'https://twitter.com',
'accept-encoding': 'gzip, deflate, br',
'x-csrf-token': 'PUT HERE',
'accept-language': 'en-US,en;q=0.9',
'authorization': 'PUT HERE',
'alexatoolbar-alx_ns_ph': 'AlexaToolbar/alx-4.0.3',
'cookie': 'PUT HERE',
'x-twitter-auth-type': 'OAuth2Session',
'user-agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36',
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
'accept': 'application/json, text/javascript, */*; q=0.01',
'referer': 'https://twitter.com/',
'authority': 'api.twitter.com',
'x-twitter-active-user': 'yes',
}
data = [
('batch_mode', 'off'),
('status', text),
]
response = requests.post('https://api.twitter.com/1.1/statuses/update.json', headers=headers, data=data)
return response
bot = telebot.TeleBot(API_TOKEN)
@bot.message_handler(commands=['help', 'start'])
def send_welcome(msg):
bot.reply_to(msg, "I'm twitter bot!\njust forward a message for me in order to tweet it.")
@bot.message_handler(func=lambda message: True)
def received_message(msg):
try:
request = tweet(msg.text)
print request.text
js = json.loads(request.text)
mesg = "Your message has been tweeted successfully, Here's the link:\n\n[Link to the tweet](https://twitter.com/{}/status/{})".format(js['user']['screen_name'], js['id_str'])
bot.reply_to(msg, mesg, parse_mode="markDown")
except Exception as e:
print e
bot.reply_to(msg, "⚠️ An Error occurred".encode("utf-8"))
bot.polling()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment