I'm pretty new to making bot thing so I have several questions I want to ask regarding this:
I'm using a bot to send message by
def send_message(chat_id, msg):
url = f"https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={chat_id}&text={msg}"
requests.get(url)
send_message(chat_id,msg)
By this, I can send message and I want to beautify the result by using parse_mode but I don't know where to put in in url to make it work.
I'm alternate to using Telethon. I can use it to send message to individual and group by user name and group invite link, but when I try to send it to channel by:
client.send_message(entity='my channel name', message=message)
When I try to run it, it return Cannot find any entity corresponding to "Test channel".
I also try
destination_channel_username='test_ali3'
entity=client.get_entity(destination_channel_username)
client.send_message(entity=entity,message="Hi")
but it require my channel access_hash, how can get it, or are there any other way to send message to channel.
I know Telegram bot API have function like sendMessage or bot.sendMessage that also can do the job, but somehow I can't call it, which packages should I install and/or import.
Thanks a lot!
This should do the trick:
def send_message(chat_id, msg, parse_mode):
url = f"https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={chat_id}&text={msg}&parse_mode={parse_mode}"
requests.get(url)
TBH I'm not too familiar with telethon, but that library is mainly intended for so called userbots. that means that it uses the API that also the Telegram app uses and because of that, it's often times more involved than just the plain bot api.
To use the Bot API in Python, I can recommend the package python-telegram-bot, see https://python-telegram-bot.org. Disclaimer: I'm currently the maintainer of that package. There are also other python packages for the bot api, see e.g. https://core.telegram.org/bots/samples#python
Related
I suspect it may be rather kid question – but anyway.
How to open another Telegram chat or group or channel using pyTelegramBotAPI? I want to forward the user (not message, the user himself) to another channel if he clicks certain button.
I saw content type migrate_to_chat_id in Message class declaration. Should I use it? If so, how to get an id of channel I need? It won't send any message to my bot.
I would better use "t.me/..." url.
Partly solved.
Speaking about the buttons, it is indeed easy. You just use named parameter url= in InlineKeyboardButton() method.
For other cases. You need to open another channel(s) from function depending on several conditions for instance. Still don't know. Import requests and make GET request? I suspect that something for it should already be in pyTelegramBotAPI, but searching in lib files wasn't successful.
I want to make a bot in discord that, when you send it a string of some sort, or an event happens, it'll send a message to a discord channel. However, when I tried adding an "input" function (the simplest there is), it couldn't work at all. What other ways are there to input strings into programs like this?
You can use webhooks to do this.
First, you should install the discord_webhook module by running pip install discord_webhook into the terminal.
Now, you can write your program. This is a very simple example:
from discord_webhook import DiscordWebhook
url = # Put webhook url here...
webhook = DiscordWebhook(url=f'{url}', content=input('Enter a message: '))
response = webhook.execute()
You should replace the url variable with your webhook link. In case you don't know how to get a webhook link, this is an example on how to do it:
If you don't know anything about discord bots, freeCodeCamp did an amazing video on it. Look that up. They are amazing.
I'm using the python-telegram-bot library to write a bot in Python that sends URLs into a channel where the bot is administrator.
Now, I would like to have the bot reading, let's say, the last 5 messages (I don't really care about the number as I just need to read the message on the chat) and store them into a list in the code for further elaborations.
I already have my bot working with:
bot = telegram.Bot(token='mytoken')
bot.sendMessage(chat_id='#mychatid', text=entry.link)
But I can't find a bot.getLastMessage or bot.getMessage kind of class into the python-telegram-bot library.
In case there's already no written class that does that, how can I implement it via the Telegram API as I'm a bit of a beginner when it comes to API implementation?
Thanks.
That's not possible in Bots unfortunately.
Here you can find all available methods (that python-telegram-bot invokes behind the scenes) and there's no such method available to fetch messages on demand.
The closest you can get through the api is getChat (which would return the pinned_message in that chat).
What you can do in this case is, store the messages the bot sends as well as the message updates the bot receives (by setting up a handler) in some storage (database) and fetch from there later on.
Have you tried the other type of Telegram API, Telegram [client] API and TDLib?
Using telethon library makes it easy to read channels history (see telethon docs).
For this, we need an api_id and an api_hash.
To get these parameters, we need to log in to our Telegram core
and go to the API development tools area.
There is a simple form that needs to be filled out, after which, we can receive our api_id and api_hash. See Telegram's help documentation about how to get your API credentials.
Here is an example code that gets the last 5 messages of targetChannelId:
from telethon import TelegramClient
API_ID = 123456 # See above for how to get it
API_HASH = '123abc' # See above for how to get it
client = TelegramClient('my-client', API_ID, API_HASH)
async def main():
async for message in client.iter_messages('targetChannelId', limit=5):
print(message.id, message.text)
with client:
client.loop.run_until_complete(main())
The first time you run this code it asks your phone number or bot token. Enter your phone number in the format +9912345... where 99 is your country code and the rest is your phone number.
It then may send a login code to your Telegram app; enter it in the console.
Note: Bots cannot see channel history messages (at least in telethon) but users (phone numbers) can. Bots can only listen for channel updates only if they are one of its administrators.
The client.iter_messages() accepts other parameters like min_id which can be used so we get messages only after a specific message (for example, we can save the last message id that we have processed and next time pass that id as min_id so only messages after that message are returned).
I've been trying to use outgoing webhook in Slack to export messages from the channel to my Python program but I can't find the way to do it, so I wonder if even a thing like this exists?
Slack seems to have an API you can use to retreive the messages of a given channel.
Follow this link : https://api.slack.com/messaging/retrieving
You can make a GET request to retreive the full history of the given conversation (so all the messages) :
GET https://slack.com/api/conversations.history?token=YOUR_TOKEN_HERE&channel=CONVERSATION_ID_HERE
Content-type: application/json
You can easily make GET requests wiht python using the library called 'requests'.
If you're able to have a valid API token and the ID of the conversation, you will then be able to get all the messages of the chosen conversation.
When I call
!room create #example-room
in slack, i get a response:
Computer says nooo. See logs for details:
Slack API call to channels.create failed: not_allowed_token_type
The Slack API (here: https://api.slack.com/methods/channels.create) makes it sound as if bots are not allowed to call Chatroom methods , though I assume that because of the existence of the !room create command that there must be a way.
Could someone help me with creating a chatroom in slack?
It sounds like you'll need to configure errbot to use a regular Slack user account, and not a Bot account:
You will need to have an account at Slack for the bot to use, either a bot account (recommended) or a regular user account.
See if that approach works, as Bots are indeed not allowed to create channels.