Created
April 22, 2020 09:03
-
-
Save ringate/bf06aa1e65dffe2486d74d534cd4afc6 to your computer and use it in GitHub Desktop.
Telegram bot with reddit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import telegram | |
from telegram.ext import Updater, CommandHandler | |
import praw | |
import json | |
# connect and authorize with Reddit API | |
reddit = praw.Reddit( | |
client_id='REDDIT_APP_CLIENT_ID', | |
client_secret='REDDIT_APP_CLIENT_SECRET', | |
user_agent='pytgbot1 by /u/ringate' | |
) | |
# select subreddit | |
subreddit = reddit.subreddit('HongKong') | |
# telegram bot init | |
bot_token = 'TELEGRAM_BOT_TOKEN' | |
bot = telegram.Bot(token=bot_token) | |
updater = Updater(token=bot_token, use_context=True) | |
dispatcher = updater.dispatcher | |
# for printout permissions with command: /perms | |
botinfo = { | |
"id": bot.get_me().id, | |
"is_bot": bot.get_me().is_bot, | |
"can_join_groups": bot.get_me().can_join_groups, | |
"can_read_all_group_messages": bot.get_me().can_read_all_group_messages, | |
"supports_inline_queries": bot.get_me().supports_inline_queries | |
} | |
# one command following one function | |
def start(update, context): | |
context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!") | |
def perms(update, context): | |
context.bot.send_message(chat_id=update.effective_chat.id, text=json.dumps(botinfo, indent=2)) | |
def news(update, context): | |
for submission in subreddit.top(limit=1): | |
context.bot.send_message(chat_id=update.effective_chat.id, text=submission.url) | |
# deflare and binding specific commands and functions | |
start_handler = CommandHandler('start', start) | |
dispatcher.add_handler(start_handler) | |
perms_handler = CommandHandler('perms', perms) | |
dispatcher.add_handler(perms_handler) | |
news_handler = CommandHandler('news', news) | |
dispatcher.add_handler(news_handler) | |
# telegram bot service start | |
updater.start_polling() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment