【问题标题】:Attempting to bridge Discord messages to Telegram尝试将 Discord 消息桥接到 Telegram
【发布时间】:2020-04-20 11:02:21
【问题描述】:

需要一些帮助,试图在不和谐频道和电报频道之间架起一座桥梁。我收到了从 Discord 发送到 Telegram 的消息,但我很难将 Telegram 消息发送到 Discord。我收到了 Telegram 消息,但我不知道如何将它们传递给 Discord。

from discord.ext import commands
import telegram.ext
from telegram.ext import MessageHandler, Filters, Updater

telegram_token = 'str'
telegram_group = 'str'
discord_channel = 'int'


class repeater(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_message(self, message):
        homechannel = self.bot.get_channel(discord_channel)
        if message.channel == homechannel:
            Tele_Bot = telegram.Bot(token=telegram_token)
            Tele_Bot.sendMessage(chat_id=telegram_group, text=f'{message.guild.name}:{message.author}:\n{message.content}')
            print('Message Detected from Discord & Sent')


class telegram_repeater(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
    updater = Updater(token=telegram_token, use_context=True)
    dispatcher = updater.dispatcher

    def echo(update, context):
        #The Magic would Happen here.

    echo_handler = MessageHandler(Filters.text, echo)
    dispatcher.add_handler(echo_handler)
    updater.start_polling()


def setup(bot):
    bot.add_cog(repeater(bot))
    bot.add_cog(telegram_repeater(bot))

我怎样才能获得一个不和谐频道,然后向它发送电报消息?

【问题讨论】:

  • 如果您不想在处理异步同步时遇到任何大问题,请使用异步电报库。

标签: python telegram discord.py python-telegram-bot


【解决方案1】:

假设您已经导入了 discord 并运行了客户端机器人:

import discord

client = discord.Client()
client.run('your token here')

向特定频道发送消息:

channel = client.get_channel(discord_channel_id)
await channel.send_message(your_message_string)

你应该看看的链接

【讨论】:

    猜你喜欢
    • 2018-11-27
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 2021-08-27
    • 2023-01-31
    • 2018-08-06
    相关资源
    最近更新 更多